Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Universal vertebral Stack in the General data structure by C language

Shulou Source: shulou.com Published: 2022-06-02 10:21:11 09月19日 Update

Today, I will talk to you about how C language realizes the common stack in the common data structure. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.

For everyone to share the C language to achieve a common data structure of the general stack of specific code, the specific content is as follows

This is the common linked list on the basis of the implementation of the vertebral stack, on the implementation of the linked list see: C language to achieve common data structure of the common linked list.

The stack mentioned here refers to the stack.

Note that the stack stores only pointers, not actual data.

Header file:

/** * ************************ File myStack.h********************/#ifndef MYSTACK_H_INCLUDED#define MYSTACK_H_INCLUDED #include "myList.h"typedef MyList MyStack; //Create Stack MyStack * createMyStack(); //Release Stack void freeMyStack (MyStack * stack); //void myStackAdd(MyStack* const stack, void* const data); //void* myStackRemove(MyStack * const stack); //void* myStackGetTop(const MyStack * const stack); #endif // MYSTACK_H_INCLUDED

source file

/** ************************ File myStack.c***********************/#include "myStack.h" //Create Stack MyStack * createMyStack(){ return createMyList();} //free stack void freeMyStack(MyStack * stack){ freeMyList(stack);} //add void myStackAdd(MyStack* const stack, void* const data){ myListInsertDataAtFirst(stack, data);} //delete void* myStackRemove(MyStack * const stack){ return myListRemoveDataAtFirst(stack);} //get stack header void* myStackGetTop(const MyStack * const stack){ return myListGetDataAtFirst(stack);}

test file

/** ************************** File main.c*** test for MyStack**************************/#include #include #include "myStack.h" typedef struct a{ int i; char c;} A; int main(){ const int S =10; //Create and initialize data A * data= malloc(sizeof(A)*S); for (int i=0; i

< S; i++) { data[i].i=i; data[i].c=(char)('A'+ i); } //创建椎栈 MyStack * stack= createMyStack(); //插入数据 myStackAdd(stack, &data[0]); myStackAdd(stack, &data[2]); myStackAdd(stack, &data[6]); //测试删除 while(myListGetSize(stack)) { A * pp = myStackRemove(stack); printf("%d[%c] ", pp->

i, pp->c); } puts(""); //release stack freeMyStack(stack); //release data free(data); return 0;} After reading the above, do you have any further understanding of how C implements common stacks in common data structures? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Tags: Data stack data structure structure language content file code foundation actually that is the pointer is in more source file knowledge article industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information NVidia Linux OPPO Reno