In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to realize stack in C language". In daily operation, I believe many people have doubts about how to realize stack in C language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how to realize stack in C language"! Next, please follow the editor to study!
What is a data structure?
What is the data structure? To understand the data structure, we need to understand the data and structure, data is some variables like int char, these are data, if you are a basketball enthusiast, then your sneakers are your data, structure is how to arrange and combine these data, how to put the data in order to facilitate you to find these data, the data and structure together to understand is the so-called data structure, simple point Is the way to deal with data.
Usually at home, do you put your shoes casually, and then it takes a lot of time to find shoes? maybe your wife is also very angry. messing with shoes every day makes it very troublesome for her to clean up, and then one day, you buy a very cool shoe rack, and with this shoe rack, your shoes finally have a home, and this shoe rack is used to deal with shoes.
What is a stack?
Stack can be understood as a kind of data structure, which is characterized by the "data" of the person who goes in first and then comes out, just like the picture below. if the stack is a hole, the person's "data" can only enter through one mouth of the hole. then out can only come out of one mouth, and the width of the hole can only hold one person's "data". Well, the person who goes in first has the most stupid "data". Be sure to wait for the "data" from behind to go out before you can go out.
Implement a stack in C language
I am very good at writing code. Before, a classmate wrote a stack for me to check. I looked at it. It seemed that my ability to write code was better than him, and the code was relatively simple. Then I talked about some more important functions. I hope everyone will throw out a stack and kill the interviewer during the interview. Haha.
# include "stdio.h"
# include "stdlib.h"
Struct List {
Int data
Struct List * next
}
Struct Stack {
Struct List * head
Int size
}
Struct Stack * StackInit (void)
{
Struct Stack * stack = NULL
Stack = (struct Stack*) malloc (sizeof (struct Stack))
Stack- > head = (struct List *) malloc (sizeof (struct List))
Stack- > head- > next = NULL
Stack- > size = 0
Return stack
}
Int StackPush (struct Stack * stack,int data)
{
Struct List * tmp = (struct List *) malloc (sizeof (struct List))
Tmp- > data = data
Tmp- > next = stack- > head- > next
Stack- > head- > next = tmp
Stack- > size++
/ / printf ("push:%d\ n", data)
Return 0
}
Int IsStackEmpty (struct Stack * stack)
{
/ * if the header pointer to the next one is empty, the stack is empty * /
If (stack- > head- > next = = NULL)
Return 1
Else
Return 0
}
Int StackPop (struct Stack * stack,int * data)
{
Struct List * tmp = NULL
If (IsStackEmpty (stack))
Return-1
Tmp = stack- > head- > next
* data = tmp- > data
Stack- > head- > next = tmp- > next
Stack- > size--
Free (tmp)
/ / printf ("pop:%d\ n", * data)
Return 0
}
Int main (void)
{
Int I = 0
Struct Stack * stack = NULL
Stack = StackInit ()
For (I = 0 position inext)
* data = tmp- > data
Stack- > head- > next = tmp- > next
Stack- > size--
Free (tmp)
/ / printf ("pop:%d\ n", * data)
Return 0
}
The first way to determine whether the stack is empty or not is by determining whether the pointer to head- > next is empty.
Then take out the data of the position head- > next, and point the pointer of head- > next to the next location of the location taken out.
And then remember free. Just Ok.
3-Stack
The operation of entering the stack is the opposite of the operation of getting out of the stack, which is to change the position and the direction of the pointer.
Int StackPush (struct Stack * stack,int data)
{
Struct List * tmp = (struct List *) malloc (sizeof (struct List))
Tmp- > data = data
Tmp- > next = stack- > head- > next
Stack- > head- > next = tmp
Stack- > size++
/ / printf ("push:%d\ n", data)
Return 0
}
Implement a stack with an array
Array itself is a kind of data structure, using array to achieve a stack is also very simple and convenient, please see.
# include "stdio.h"
# include "stdlib.h"
/ * Stack size * /
# define LENGHT (100)
Struct Stack {
Int stack_ array[LENGHT]
Dynamic length of unsigned int size;// stack
}
Struct Stack * StackInit (void)
{
Struct Stack * stack = NULL
Stack = (struct Stack*) malloc (sizeof (struct Stack))
Stack- > size = 0
Return stack
}
Int StackPush (struct Stack * stack,int data)
{
If (stack- > size > = LENGHT)
{
Printf ("stack is full\ n")
Return (- 1)
}
Stack- > stack_ Array [stack-> size] = data
Stack- > size++
/ / printf ("push:%d size:%d\ n", data,stack- > size)
Return 0
}
Int IsStackEmpty (struct Stack * stack)
{
/ * if the header pointer to the next one is empty, the stack is empty * /
If (stack- > size = = 0)
Return 1
Else
Return 0
}
Int StackPop (struct Stack * stack,int * data)
{
Stack- > size--
If (IsStackEmpty (stack))
Return-1
* data = stack- > stack_ Array [stack-> size]
/ / printf ("pop:%d size:%d\ n", * data,stack- > size)
Return 0
}
Int main (void)
{
Int I = 0
Struct Stack * stack = NULL
Stack = StackInit ()
For (I = 0 position I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.