Chain-type stack __c language implemented in C language

Source: Internet
Author: User

Stack

chain-type storage structure of stacks

 

Source code Download URL

http://lijingronghcit.download.csdn.net/

 

The chain-type storage structure of the stack is the same as the chain-type storage structure of the linear table, which is realized by the single linked list composed of nodes. It is convenient for us to use single linked list with headless nodes. At this time the top of the stack is a single linked list of the first node, the entire single link table for a chain stack. 1. type definition for chain stacks :

Type definition for chain stacks

typedef struct NODE

{

datatype data; /* Data field * *

struct node * NEXT; /* Pointer field * *

}linkstack; /* Chain Stack node type * *

Top is a stack, and it uniquely identifies a stack. Null stack when empty. Because the chain stack dynamic allocation of space, so the operation does not need to consider the benefits of the problem.

The following is the basic operation of the chain stack section:

1. Judging empty stacks

Identify empty stacks

int Stackempty (Linkstack *top)

{

return (TOP?0:1);

}

Returns 0, it is not null.

2. take the top element of the stack

Take the top element of the stack

DataType GetTop (Linkstack *top)

{

if (!top)

{

printf ("/n-linked list is empty!");

return 0;

}

Return top->data;

}

3. into the stack

Into the stack

Linkstack *push (linkstack *top,datatype x)

{

Linkstack *p;

p= (Linkstack *) malloc (sizeof (linkstack));/Allocate space

p->data=x; /* Set the value of the new node * *

p->next=top; /* Inserts a new element into the stack * *

Top=p; /* Set the new element to the top of the stack * *

return top;

}

4. out Stack

Out Stack

Linkstack *pop (Linkstack *top)

{

Linkstack *p;

if (!top)

{

printf ("N/a chain stack is empty!");

return NULL;

}//Determine if empty stack n

P=top; Point to the top of the stack that was deleted

top=top->next; Modify stack top pointer

Free (p);

return top;

}

5. Main function Test

Main ()

{

int a[5]={1,2,3,4,5},i,isemtpy;

Linkstack *linkstack;

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

linkstack->data=1;

linkstack->next=null;

for (i=1;i<5;i++)

{

Linkstack=push (Linkstack,a[i]);

}

Take the top element of the stack as

printf ("Top of Stack element:");

printf ("%d/n", GetTop (Linkstack));

Stack top elements out of the stack

Linkstack=pop (Linkstack);

The top element of the stack after the stack is

printf ("Stack top element after stack is:");

printf ("%d", GetTop (Linkstack));

printf ("n");

Determine if the stack is empty

Isemtpy = Stackempty (Linkstack);

if (isemtpy==0)

printf ("Linkstack is not empty chain stack!/n");

Else

printf ("Linkstack is an empty chain stack!/n");

}

Note : 1 The chain stack does not need to set the head node, because the stack top operation is frequent. 2 The chain stack will not appear stack full situation, unless there is insufficient space, resulting in malloc allocation failure. 3 The chain stack of the stack, the stack operation is the top of the stack insert and delete operations, modify the pointer can be. 4 The advantage of the chain stack is: You can use a number of stack sharing space, when the number of elements in the stack changes large, and there are multiple stacks, the chain stack is the preferred storage mode of the stack.

Source code Download URL

http://lijingronghcit.download.csdn.net/

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.