Using a single-chain table with leading nodes in C language to simulate the stack structure

Source: Internet
Author: User

Using a single-chain table with leading nodes in C language to simulate the stack structure

In the previous two blogs, I used static arrays and dynamic arrays to construct stacks, which is convenient to implement. However, I always feel that flexibility is not enough, because no matter what, we must specify the length of the array. In this blog, we will use the single-chain table of the leading node to simulate the stack. Why is a single-chain table used? For stacks, pop-up and press-in operations are performed on the top of stacks. The operations on the first node in a single-chain table are the most convenient. The two match exactly. Upload the code to https://github.com/chenyufeng1991/stack_javaslist.

(1) Declare node types and functions

 

typedef int elemType;typedef struct NodeList{    int element;    struct NodeList *next;}Node;void createStack(Node **pNode);void destroyStack(Node *pNode);void push(Node *pNode,int value);void pop(Node *pNode);void top(Node *pNode);int isEmpty(Node *pNode);int isFull(Node *pNode);void printStack(Node *pNode);

(2) initialize a single-chain table

 

 

// Initialize the void createStack (Node ** pNode) {* pNode = (Node *) malloc (sizeof (Node); if (* pNode = NULL) {printf ("% s FUNCTION execution, memory allocation failed, single-chain table initialization failed \ n" ,__ FUNCTION _);} else {(* pNode)-> next = NULL; printf ("% s FUNCTION execution, leading node single-chain table initialization completed \ n" ,__ FUNCTION __);}}

(3) press an element

 

 

// Press a void push (Node * pNode, int value) {Node * pInsert; pInsert = (Node *) malloc (sizeof (Node )); // check whether the allocated memory is successfully pInsert = NULL? Memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; pInsert-> element = value; pInsert-> next = pNode-> next; pNode-> next = pInsert ;}

(4) An element is displayed.

 

 

// Pop up an element void pop (Node * pNode) {if (! IsEmpty (pNode) {Node * pNext; pNext = pNode-> next; pNode-> next = pNext-> next; free (pNext); pNext = NULL ;}}

(5) print stack Elements

 

 

// Print the stack element void printStack (Node * pNode) {if (! IsEmpty (pNode) {Node * pMove; pMove = pNode-> next; while (pMove! = NULL) {printf ("% d", pMove-> element); pMove = pMove-> next;} printf ("\ n ");} else {printf ("Stack is empty, print failed \ n ");}}

(6) Clear stack Elements

 

 

// Clear the stack element void destroyStack (Node * pNode) {Node * pMove; pMove = pNode-> next; while (pMove! = NULL) {pNode-> next = pMove-> next; free (pMove); pMove = pNode-> next ;}}

(7) judge whether the stack is empty

 

 

// Determine whether the stack is empty. int isEmpty (Node * pNode) {/*** when there is only one header Node, this linked list is blank */if (pNode-> next = NULL) {return 1;} return 0 ;}

(8) taking the top element of the stack

 

 

// Obtain the top element of the stack void top (Node * pNode) {if (! IsEmpty (pNode) {printf ("the top element of the stack is % d \ n", pNode-> next-> element );}}

(8) test code

 

 

Int main (int argc, const char * argv []) {Node * pList; createStack (& pList); push (pList, 3); push (pList, 1 ); push (pList, 9); push (pList, 4); push (pList, 7); printStack (pList); pop (pList ); printf ("the elements of the pop Stack are \ n"); printStack (pList); top (pList); destroyStack (pList); printStack (pList); return 0 ;}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.