Stack creation and basic operations

Stack (LIFO): A linear list that is limited to insert and delete operations only at the end of the list. Simply put, the last entry is the earliest to come out.

sequential stack

Implemented with an array, one end with subscript 0 is used as the bottom of the stack, the other end is the top of the stack, and top is used as the pointer to the top of the stack

When we define an empty stack top=-1

stack structure :

typedef struct SqStack
{
	int data[MAX];
	int top;	
}SqStack;

 

Basic operation :

//初始化栈 
void InitStack(SqStack *s)
{
	s->top=-1;
}

//判断栈是否为空 
int Stackempty(SqStack *s)
{
	if(s->top==-1)
		return 0;
	else
		return 1;	
} 

//进栈
int push(SqStack *s,int e)
{
	if(s->top==MAX-1)
	{	
		printf("栈满\n");
		return 0;
	}
	s->data[++s->top]=e;
	return 1;		
} 

//出栈
int pop(SqStack *s,int *e)
{
	if(s->top==-1)
	{
		printf("栈空\n");
		return 0;
	}
	*e=s->data[s->top--];
	return 1;		
} 

 

Test run:

#include<stdio.h>
#include<stdlib.h>
 
#define MAX 10

typedef struct SqStack
{
	int data[MAX];
	int top;	
}SqStack;

//初始化栈 
void InitStack(SqStack *s)
{
	s->top=-1;
}

//判断栈是否为空 
int Stackempty(SqStack *s)
{
	if(s->top==-1)
		return 0;
	else
		return 1;	
} 

//进栈
int push(SqStack *s,int e)
{
	if(s->top==MAX-1)
	{	
		printf("栈满\n");
		return 0;
	}
	s->data[++s->top]=e;
	return 1;		
} 

//出栈
int pop(SqStack *s,int *e)
{
	if(s->top==-1)
	{
		printf("栈空\n");
		return 0;
	}
	*e=s->data[s->top--];
	return 1;		
} 

//打印栈元素
void printStack(SqStack *s)
{
    int topp = s->top;
	while(s->top!=-1)
	{
		printf("栈元素:%d\n",s->data[s->top--]);
	}
    s->top = topp;
} 
 
int main()
{
	int a;
	SqStack s;
	s={
   
   {1,2,3,4,5},4};    //赋值数组和栈顶指针
	push(&s,6); 
	pop(&s,&a);
	printf("出栈的元素是:%d\n",a);
	printStack(&s);
	return 0;
 } 

 

 

chain stack

The top of the stack is placed at the head of the singly linked list, and no head node is required. When the stack is empty, top=NULL

Chain stack structure definition :

typedef struct StackNode     //结点 
{
	int data;
	struct StackNode *next; 
}StackNode,*LinkStackPtr;

typedef struct Node
{
	LinkStackPtr top;
	int count;
}LinkStack;

 

Basic operation :

//进栈
int push(LinkStack *s,int e)
{
	LinkStackPtr p;  //插入p结点作为新栈顶 
	p=(LinkStackPtr)malloc(sizeof(StackNode));   //注意是用结点结构申请空间的 
	p->next=NULL;
	p->data=e;
	p->next=s->top;   //s->top为原栈顶 
	s->top=p;
	s->count++;
	return 1;		
} 

//出栈
int pop(LinkStack *s,int *e)
{
	LinkStackPtr p;    //定义一个变量存储需要删除的栈顶元素 
	if(!s)
	{
		printf("栈已空\n");
		return 0;
	}
	*e=s->top->data;
	p=s->top;
	s->top=s->top->next;
	free(p);
	s->count--;    
	return 1;	
} 

//判断是否为空
int Stackempty(LinkStack *s)
{
	if(s->top==NULL)
		return 0;
	else
		return 1;		
} 

//初始化链栈
void InitStack(LinkStack *s)
{
	 
	s=(LinkStack *)malloc(sizeof(LinkStack));   
	s->top=NULL;
} 

 

test:

#include<stdio.h>
#include<stdlib.h> 


typedef struct StackNode     //结点 
{
	int data;
	struct StackNode *next; 
}StackNode,*LinkStackPtr;

typedef struct Node
{
	LinkStackPtr top;
	int count;
}LinkStack;


//进栈
int push(LinkStack *s,int e)
{
	LinkStackPtr p;  //插入p结点作为新栈顶 
	p=(LinkStackPtr)malloc(sizeof(StackNode));   //注意是用结点结构申请空间的 
	p->next=NULL;
	p->data=e;
	p->next=s->top;   //s->top为原栈顶 
	s->top=p;
	s->count++;
	return 1;		
} 

//出栈
int pop(LinkStack *s,int *e)
{
	LinkStackPtr p;    //定义一个变量存储需要删除的栈顶元素 
	if(!s)
	{
		printf("栈已空\n");
		return 0;
	}
	*e=s->top->data;
	p=s->top;
	s->top=s->top->next;
	free(p);
	s->count--;    
	return 1;	
} 

//判断是否为空
int Stackempty(LinkStack *s)
{
	if(s->top==NULL)
		return 0;
	else
		return 1;		
} 

//初始化链栈
void InitStack(LinkStack *s)
{
	 
	s=(LinkStack *)malloc(sizeof(LinkStack));   
	s->top=NULL;
} 

//打印栈元素
void printStack(LinkStack *s)
{
	while(s->top!=NULL)
	{
		printf("栈元素:%d\n",s->top->data);
		s->top=s->top->next;	
	}	
} 


int main()
{
	int a;
	LinkStack s;
	InitStack(&s);
	push(&s,1);
	push(&s,2);
	push(&s,3);
	push(&s,4);
	push(&s,5);
	pop(&s,&a);
	printStack(&s);
	return 0;
}

//为啥出问题了 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326312921&siteId=291194637