Stack Chain Structure Representation and implementation-write data structure by yourself

Source: Internet
Author: User

Stack Chain Structure Representation and implementation-write data structure by yourself

Today, we will introduce the stack's chain structure and use dev-c ++ 4.9.9.2 for debugging, so that you can directly use the code below:

The data structure stores the file stacklist. h as follows:

#ifndef _STACKLIST_H_#define _STACKLIST_H_typedef struct _Node{    int data;        struct _Node *pre;    struct _Node *next;}Node,*pNode;typedef struct _Stack_Header{    struct _Node *botton;    struct _Node *top;    int  size;    }Stack_Header,*pStack_Header;pStack_Header init_stack_list(void);pNode push_node(pStack_Header plist,int data);pNode pop_node(pStack_Header plist);int print_stack_list(pStack_Header plist);#endif


Function stores file stacklist. c

/****************************** Time: 4.12.12 Author: XIAO_PING_PING content: stack chain data structure function: learn about the data structure ********************************/# include
 
  
# Include
  
   
# Include
   
    
# Include "stacklist. h "/* initialize a stack linked list */pStack_Header init_stack_list (void) {pStack_Header plist; pNode p; plist = (Stack_Header *) malloc (sizeof (Stack_Header )); plist-> botton = NULL; plist-> top = NULL; plist-> size = 0; return plist;}/* add nodes to the stack, data */pNode push_node (pStack_Header plist, int data) {pNode p; p = (Node *) malloc (sizeof (Node); p-> data = data; p-> next = NULL; p-> pre = plist-> top; if (plist -> Top = NULL) & (plist-> botton = NULL) {plist-> top = p; plist-> botton = p; plist-> size = 1; printf ("1st nodes in the stack \ n"); return p;} plist-> top-> next = p; plist-> top = p; plist-> size + = 1; printf ("Number of % d nodes in the stack \ n", plist-> size ); return plist-> top;}/* remove a node from the stack */pNode pop_node (pStack_Header plist) {pNode p; if (0 = plist-> size) {printf ("no node in the stack area, unable to complete the output stack \ n"); return plist-> top;} p = plist-> top; plist-> top = plist -> Top-> pre; if (NULL! = Plist-> top) {plist-> top-> next = NULL;} else {plist-> botton = NULL;} free (p ); plist-> size-= 1; printf ("output stack node % d \ n", plist-> size + 1); return plist-> top ;} /* print data in the stack */int print_stack_list (pStack_Header plist) {pNode p; if (plist-> top = plist-> botton) & (0 = plist-> size) {printf ("no node \ n" in the stack area); return-1;} p = plist-> botton; printf ("print data from the bottom of the stack \ n"); while (plist-> top! = P) {printf ("% d", p-> data); p = p-> next;} printf ("% d", p-> data ); printf ("\ n printed \ n"); return 0 ;}
   
  
 


Test. c

#include 
 
  #include 
  
   #include 
   
    #include "stacklist.h" int main(){    pStack_Header plist;    plist = init_stack_list();        push_node(plist,13);    push_node(plist,1);    push_node(plist,232);    push_node(plist,143);    print_stack_list(plist);          pop_node(plist);      pop_node(plist);      pop_node(plist);      //pop_node(plist);      //pop_node(plist);     printf("\n");    print_stack_list(plist);           getch();}
   
  
 


The running result is as follows:

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.