Data Structure stack and Data Structure

Source: Internet
Author: User

Data Structure stack and Data Structure

# Include <stdio. h>
# Include <malloc. h>

/*
Declare struct type struct node,
Contains the data field data and pointer field next, which belongs to a node type
*/
Typedef struct node
{
Int data;
Struct node * next; // store the address pointing to the next node
} Node, * PNode;

/*
Declare a struct type, including the top and bottom elements of the stack,
Stack replaces struct stack, and PStack replaces struct stack *
*/
Typedef struct stack
{
PNode Top;
PNode Bottom;
} Stack, * PStack;

/*
Declare the function, initialize the stack, go to the stack, and output the metadata function in the stack.
*/
Void init_stack (PStack );
Void push (PStack, int );
Bool pop (PStack, int *);
Void traverse_stack (PStack );

Int main ()
{
Int val; // defines the variable val and stores the elements of the stack.
Stack sta; // defines the struct stack variable sta
Init_stack (& sta); // initialize the stack
Push (& sta, 1); // Add elements to stack
Push (& sta, 2 );
Push (& sta, 3 );
Push (& sta, 4 );
Push (& sta, 5 );
Push (& sta, 6 );
Putchar ('\ n ');
Traverse_stack (& sta); // traverse the elements in the stack
Putchar ('\ n ');
If (pop (& sta, & val) // outputs the stack operation
{
Printf ("elements of the stack: % d \ n", val );
Putchar ('\ n ');
Traverse_stack (& sta );
}
Return 0;
}

/*
Initialize the stack, input the sta address, and apply for a memory of the struct node * type,
Its pointer field value is null, and both the stack top and stack bottom point to this memory.
*/
Void init_stack (PStack psta)
{
Psta-> Bottom = (PNode) malloc (sizeof (Node); // The stack Bottom points to the applied memory
Psta-> Top = psta-> Bottom;
Psta-> Bottom-> next = NULL; // set the pointer field of the PNode variable to NULL.
Return;
}

/*
Element stack, input the address of the sta and insert the element value,
Apply for a PNode memory, assign its pointer field to the top element of the stack, and assign the top value to the address of the memory.
*/
Void push (PStack psta, int val)
{
PNode PNew = (PNode) malloc (sizeof (Node); // defines the variable PNew of the struct node * type to point to a piece of applied memory
PNew-> data = val; // assign the PNew data field to val
PNew-> next = psta-> Top; // the pointer field value of PNew is the Top element of the stack.
Psta-> Top = PNew; // The Top element of the stack points to PNew.
Printf ("Stack entry element: % d \ n", val );
Return;
}

/*
Traverse the elements in the output stack and input the sta address,
First, judge whether the stack is empty. If it is empty, no traversal is required,
Define a PNode Variable p pointing to the memory at the top of the stack, while traversing the elements in the stack, knowing that p is equal to the value at the bottom of the stack
*/
Void traverse_stack (PStack psta)
{
If (psta-> Top = psta-> Bottom) // judge whether the stack is empty
{
Printf ("there are no elements in the stack. How can I traverse it! \ N ");
}
Else
{
PNode p = psta-> Top; // defines a struct node * Variable p pointing to the Top of the stack and pointing to the memory.
Printf ("intra-stack element traversal: \ n ");
While (p! = Psta-> Bottom) // The loop condition. p is not equal to the Bottom element of the stack.
{
Printf ("% d \ t", p-> data );
P = p-> next; // p points to the next memory
}
Printf ("\ n ");
}
Return;
}

/*
Output stack, input the sta address and val address,
First, judge whether the stack is empty. If it is empty, the outbound stack operation cannot be performed,
Define a PNode Variable p pointing to the memory at the top of the stack, assign the top element of the stack to the address of the next element, and release the memory of p.
*/
Bool pop (PStack psta, int * val)
{
If (psta-> Top = psta-> Bottom)
{
Printf ("there are no elements in the stack, so the stack cannot go out! \ N ");
Return false;
}
Else
{
PNode p = psta-> Top; // defines a struct node * Variable p pointing to the Top of the stack and pointing to the memory.
* Val = p-> data; // assign the value of the data field pointed to by p to * val
Psta-> Top = p-> next; // the Top of the stack points to the next element.
Free (p); // release the memory pointed to by p
Return true;
}
}

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.