Stack operation-stack chain and Operation

Source: Internet
Author: User

Stack operation-stack chain and Operation

Data structure:

Stack is a special linear table that allows insertion and deletion at the same end. One end that allows the insert and delete operations is called the top, and the other end is the bottom of the stack. The bottom of the stack is fixed, while the top of the stack is floating; when the number of elements in the stack is zero, it is called an empty stack. Insert is generally called PUSH and delete is called POP ). Stack is also known as a forward, forward, and forward table.

Operating System:

The compiler automatically assigns release, stores function parameter values, and local variable values. The operation method is similar to the stack in the data structure.

The stack uses a level-1 cache, which is usually in a bucket When called, and is immediately released after the call is completed.

Stack chain template code:

# Define TRUE 1 # define FALSE 0 typedef struct node {StackElementType data; struct node * next;} LinkStackNode; typedef LinkStackNode * LinkStack;/* stack operations. */Int Push (LinkStack top, StackElementType x)/* press Data Element x into the top stack */{LinkStackNode * temp; temp = (LinkStackNode *) malloc (sizeof (LinkStackNode); if (temp = NULL) return (FALSE);/* space application failed */temp-> data = x; temp-> next = top-> next; top-> next = temp;/* modify the top pointer of the current stack */return (TRUE);}/* stack operation. */Int Pop (LinkStack top, StackElementType * x) {/* Pop up the top element of the stack, and place it in the bucket x. */LinkStackNode * temp; temp = top-> next; if (temp = NULL)/* stack is empty */return (FALSE); top-> next = temp-> next; * x = temp-> data; free (temp);/* release the bucket */return (TRUE );}


Basic Question: I don't know how many code operations are performed on the inbound and outbound stacks of a chain stack.

P is a new node you have created. copy the data you have given to e. Then, its next point points to the top pointer of the stack, and then points to p at the top of the stack, if you use p = p-> next, you are directed to yourself. Delete p is to delete the node referred to by p. Therefore, it cannot be deleted.

How do I write the stack-based operations of chained storage only by writing the primary function?

# Include <stdio. h>
# Include <stdil. h>
Struct st // structure of the chain Stack
{
Int data;
Struct st * next;
} Node, * link;
Int kong (link top) // empty stack Determination
{
If (top = NULL)
Return 1;
Else
Return 0;

}
Link push (link top, int x) // algorithm of the inbound Stack
{
Node * p;
P = (node *) malloc (sizeof (node ));
P-> data = x;
P-> next = top;
Top = p;
Return (p );
}
Int main () // main Function
{
Int x
Node * p;
Kong (top );
Printf ("Enter the number of stacks to be written \ n ");
Scanf ("% d", & x );
Push (top, x );
Return (0 );
}
It's just a number on the inbound stack. If you want to make a large program, you have to change a lot, but it's all based on this. It's not hard to debug and think about it. I hope it will help you ..

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.