Stack linked list implementation and Array Implementation

Source: Internet
Author: User

 

 

// Linked List Implementation

# Include <stdlib. h>

# Include <stdio. h>

 

# Define false 0

# Define true 1

# Define OK 1

# Define error-1

 

Typedef char datatype;

 

Struct stacknode {

Datatype data;

Struct stacknode * link;

};

 

Struct stacks {

Struct stacknode * top;

};

 

Typedef struct stacks linkstack;

 

 

Int initstack (linkstack * s)

{

S-> Top = (struct stacknode *) malloc (sizeof (struct stacknode ));

S-> top-> link = NULL;

Return OK;

}

 

Int stackempty (linkstack * s)

{

Return (S-> top-> link = NULL );

}

 

Int push (linkstack * s, datatype node)

{

Struct stacknode * newnode = NULL;

 

If (! (Newnode = (struct stacknode *) malloc (sizeof (struct stacknode ))))

Return Error;

 

Newnode-> DATA = node;

Newnode-> link = s-> top;

S-> Top = newnode;

 

Return OK;

}

 

Datatype POP (linkstack * s)

{

If (stackempty (s ))

Return Error;

Struct stacknode * topnode = NULL;

 

Topnode = s-> top;

S-> Top = s-> top-> link;

Char CH = topnode-> data;

 

Free (topnode );

 

Return ch;

}

 

Datatype getstacktop (linkstack * s)

{

If (stackempty (s ))

Return Error;

Return S-> top-> data;

}

 

 

 

Int main ()

{

Linkstack * s = (linkstack *) malloc (sizeof (linkstack ));

 

Initstack (s );

 

Push (S, 'M ');

Push (S, 'E ');

Push (S, 'J ');

Push (S, 'H ');

Push (S, 'A ');

Push (S, 'n ');

 

Printf ("% C/N", getstacktop (s ));

 

While (! Stackempty (s ))

{

Printf ("% C/N", pop (s ));

}

 

Return OK;

}

// Array Implementation # include <stdio. h> # include <stdlib. h> # define maxsize 100 # define true 1 # define false 0 # define OK 1 # define error 0 typedef int datatype; typedef struct stacknode {datatype data; int top; int maxsize; struct stacknode * Next;} stacks; void initstack (stacks * s) {S-> Top =-1; s-> maxsize = maxsize;} int stackempty (stacks * s) {return (S-> Top =-1);} int stackfull (stacks * s) {return (S-> Top = s-> maxsiz E);} int push (stacks * s, datatype data) {If (stackfull (s) Return Error; s-> top ++; s [S-> top]. data = data; Return OK;} datatype POP (stacks * s) {If (stackempty (s) Return Error; datatype temp; temp = s [S-> top]. data; s-> top --; return temp;} datatype gettop (stacks * s) {return s [S-> top]. data;} void main () {stacks * s = (stacks *) malloc (sizeof (stacks); initstack (s); int I = 0; while (I ++ <5) Push (S, I ); While (! Stackempty (s) {printf ("% d", pop (s);} printf ("/N") ;}// the above code can be directly run through compilation.

 

 

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.