Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report