Data structure--the implementation of the stack

Source: Internet
Author: User

The node structure in the data structure is struct inv{    struct inv*prev;    int i;    struct inv*next;};   

Data structure for managing stacks struct stackmanage{    int count;    struct inv*head;    struct inv*Topp;    struct inv*bottomp;};

Creates a struct that manages an array structure and initializes member variables struct stackmanage* createstack () {    struct stackmanage* stackmanagevalue= (struct stackmanage*) malloc (sizeof (struct stackmanage));    Stackmanagevalue->count=0;    stackmanagevalue->head= (struct inv*) malloc (sizeof (struct inv));    Stackmanagevalue->head->i=0; stackmanagevalue->head->next=NULL; stackmanagevalue->head- >prev=null; stackmanagevalue->bottomp=null; stackmanagevalue->topp=null; return  Stackmanagevalue;}         

Press stack operation void push (int i,struct stackmanage** stackmanagevalue) {    if (!stack_is_valid (*stackmanagevalue)) return ;    struct inv*newp= (struct inv*) malloc (sizeof (struct inv));    newp->i=i;    newp->next=NULL, newp->prev= (*stackmanagevalue),Topp, if (is_empty (*stackmanagevalue)) {(* Stackmanagevalue)->head->next=newp; (*stackmanagevalue)->bottomp=newp;} else {(*stackmanagevalue)->topp->next=NEWP;} (*stackmanagevalue)->topp=newp; (*stackmanagevalue)->count++;}            

  stack operation, print error when stack is empty and return 0int pop (struct stackmanage**  stackmanagevalue) {int  i; if (!stack_is_valid (*  stackmanagevalue)) {} if (is_empty (*  stackmanagevalue)) {printf ("Error: The stack is empty>>>>>\n "); return 0 ;} struct inv*topelement= (*stackmanagevalue)->  Topp; i= (*stackmanagevalue)->topp->  i; if ((*stackmanagevalue)->count==1 ) {free  (topelement); (*stackmanagevalue)->head->next=  NULL; (*stackmanagevalue)->bottomp=  NULL; (*stackmanagevalue)->topp=  NULL;} else  {(*stackmanagevalue)->topp=topelement->  prev; Topelement=topelement->  prev; topelement->next=  NULL; free (topelement->  next);} (*stackmanagevalue)->count--; return  i;}          

Only out of the stack, not overflow the top element of the stack, when the stack is empty is also printed and returned 0int peek (struct stackmanage** stackmanagevalue) {    if (!stack_is_valid (*  Stackmanagevalue)    {        printf ("The stack is not create>>>>>\n");        return 0;    }    if (is_empty (*stackmanagevalue)) {printf ("The Stack is empty>>>>>\n"); return 0;} return (*stackmanagevalue)->topp->i;}       

Whether the query stack contains elements iint contain (int i,struct stackmanage* stackmanagevalue) {    if (!stack_is_valid (Stackmanagevalue) ) return 0;    struct inv*headp=stackmanagevalue->bottomp;    while (headp!=NULL)    {        if (headp->i==i) {return 1;} headp=headp->next;} return 0 ;}

Empties the stack and releases the structure of the management stack from void clear (struct stackmanage** stackmanagevalue) {    if (!stack_is_valid (*stackmanagevalue )) return;    struct inv*head= (*stackmanagevalue),head;    struct inv*temp=NULL;    while (head!=NULL) {temp=head, head=head->Next, Free (temp), Free ((*stackmanagevalue)); Stackmanagevalue=NULL;}         
Scan the entire stack and print void print (struct stackmanage*stackmanagevalue) {    if (!  Stack_is_valid (Stackmanagevalue))        return;    printf ("**print start from the bottom**\n");    struct inv* head=stackmanagevalue->bottomp; while (head!=NULL) {printf ("%d\n",head->i); head= Head->Next;} printf ("*************end***************\n\n");}       

Check if the stack is empty int is_empty (struct stackmanage* stackmanagevalue) {    if (!  Stack_is_valid (Stackmanagevalue))        {        printf ("Error:the stack is not create>>>>>\n");        return 1;    }    Return! (stackmanagevalue->count);//when is zero return true} checks whether pointers to the stack management struct are valid int stack_is_valid (struct stackmanage* stackmanagevalue) {return stackmanagevalue!=NULL;}      

CPP source file: Http://pan.baidu.com/s/1bodkQRX

Stack.cpp file.

Data structure--the implementation of the stack

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.