Title requirements:
Define a stack, which requires a linked list to be implemented, so that it has the following functions:
(1) The stack contains 7 elements, which are 67, 3, 88, 6, 1, 7, and 0 in sequence. The stack is created by the tail insertion method. The name of the stack is s1, and two pointers are set for the stack, a bottom and a top pointer;
(2) Design a function push, which completes the function of inserting elements into the stack, and uses the push function to insert the number -9 into the stack. Go to s1, and print out the elements in the stack;
(3) Design a function pop, which completes the function of deleting elements from the stack, use the pop function to delete the 3 elements in the stack at this time, and print out the stack. ;
(4) Design a function length to find the number of elements in the stack at this time.
Reference Code:
#include <stdio.h>
#include <stdlib.h>
//定义结点
struct node{
int data;
struct node *next;
};
//定义双指针
struct stack{
struct node *top;
struct node *bottom;
};
//初始化
struct stack* init(){
struct stack* s1=(struct stack*)malloc(sizeof(struct stack));
s1->top=NULL;
s1->bottom=NULL;
struct node* link_node;
int arr[]={
67,3,88,6,1,7,0};
int i;
for(i=0;i<7;i++){
link_node = (struct node*)malloc(sizeof(struct node));
link_node->data=arr[i];
link_node->next=NULL;
if(i==0){
s1->bottom=link_node;
}else{
s1->top->next=link_node;
}
s1->top=link_node;
}
link_node = (struct node*)malloc(sizeof(struct node));
link_node->data=NULL;
link_node->next=NULL;
s1->top->next=link_node;
s1->top=link_node;
return s1;
};
//进栈
struct stack* push(struct stack *s, int e){
s->top->data=e;
struct node* link_node = (struct node*)malloc(sizeof(struct node));
link_node->data=NULL;
link_node->next=NULL;
s->top->next=link_node;
s->top=link_node;
return s;
}
//出栈
struct stack* pop(struct stack* s_pop){
struct node *temp=s_pop->bottom;
if(s_pop->bottom==s_pop->top){
printf("此时栈已经为空!\n");
return s_pop;
}else{
for(temp;temp->next!=NULL;temp=temp->next){
if(temp->next->next==NULL){
free(temp->next);
temp->next=NULL;
temp->data=NULL;
s_pop->top=temp;
return s_pop;
}
}
}
}
//统计总数
void length(struct stack* s_length){
int i=0;
struct node *temp=s_length->bottom;
if(s_length->bottom==s_length->top){
printf("此时栈中元素个数为0\n");
}else{
for(temp;temp->next!=NULL;temp=temp->next){
i++;
}
printf("此时栈中元素个数为%d个!\n",i);
}
}
//打印
void print_stack(struct stack* s_print){
struct node *temp=s_print->bottom;
if(s_print->bottom==s_print->top){
printf("此时栈已经为空!\n");
}else{
printf("栈中的元素:");
for(temp;temp->next!=NULL;temp=temp->next){
if(temp->next->next==NULL)
printf("%d \n",temp->data);
else
printf("%d,",temp->data);
}
}
}
//main函数
int main() {
struct stack* result;
//初始化
result=init();
//打印
print_stack(result);
//进栈
result=push(result,-9);
//打印
print_stack(result);
//出栈,调用三次
result=pop(result);
result=pop(result);
result=pop(result);
//打印
print_stack(result);
//长度
length(result);
system("pause");
return 0;
}
Experimental results:
1. Initialize the stack:
2. Call the push function to insert -9 into the stack:
3. Call the pop function 3 times to pop 3 elements from the stack:
4. Call the length function to scan the length of the stack: