1、栈的定义
栈是一种线性表,只允许在表尾(栈顶)进行插入(进栈)或删除(出栈)操作。即一种可以实现 “先进后出” 的存储结构。
1.1栈的也有两中存储方式,一种顺序栈,另一种链栈
1)静态栈 (顺序栈)
2)动态栈(链栈)
2、顺序栈的定义与操作
定义: 栈的顺序存储结构称为顺序栈,它利用一组地址连续的存储单元存放自栈底到栈顶的数据元素(如第一张图所示)。在顺序栈中,通过top变量来表示当前栈顶的位置。
1)顺序栈的搭建
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
typedef struct stack
{
int data[MAX];
int top;
}SSK;
2)创建一个空的顺序栈
SSK *CreateEmptyStack()
{
SSK *head = (SSK *)malloc(sizeof(SSK));
head->top= 0;
return head;
}
3)进栈(插入元素)
int Push(SSK *head,int Insertdata)
{
if(head->top == MAX)
{
printf("栈满了,没有位置可以插入\n");
return 0;
}
head->data[head->top] = Insertdata;
head->top++;
return 1;
}
4)出栈(即删除元素,是删除栈顶第一个元素)
int Delelist(SSK *head ,int *x)
{
if(head->top==0)
{
printf("栈为空,没有值可删除\n");
return 0;
}
head->top=head->top -1;
*x = head->data[head->top]; //把第一个栈顶的值取出,即删除了这个元素
return 1;
int main()
{
int x;
Delelist(SSK *head ,&x);
return 0;
}
5)打印顺序栈函数
void printstack(SSK *head)
{
SSK *pmove =head;
int top =pmove->top;
int i;
if(pmove==0 || pmove->top==0)
{
exit(0);
}
for(i=0;i<top;i++)
{
printf("data[%d]=%d\n",i,pmove->data[i]);
}
}
简单使用(完整代码)
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
typedef struct stack
{
int data[MAX];
int top;
}SSK;
SSK *CreateEmptyStack()
{
SSK *head = (SSK *)malloc(sizeof(SSK));
head->top= 0;
return head;
}
int Push(SSK *head,int Insertdata)
{
if(head->top == MAX)
{
printf("栈满了,没有位置可以插入\n");
return 0;
}
head->data[head->top] = Insertdata;
head->top++;
return 1;
}
void printstack(SSK *head)
{
SSK *pmove =head;
int top =pmove->top;
int i;
if(pmove==0 || pmove->top==0)
{
exit(0);
}
for(i=0;i<top;i++)
{
printf("data[%d]=%d\n",i,pmove->data[i]);
}
}
int Delelist(SSK *head ,int *x)
{
if(head->top==0)
{
printf("栈为空,没有值可删除\n");
return 0;
}
head->top=head->top -1;
*x = head->data[head->top]; //把第一个栈顶的值取出,即删除了这个元素
return 1;
}
int main()
{
int x;
SSK* sqlink=CreateEmptyStack();
Push(sqlink,2);
Push(sqlink,4);
Push(sqlink,6);
Push(sqlink,8);
printstack(sqlink); //打印栈里面的元素
printf("******************\n");
Delelist(sqlink ,&x); //删除栈顶一个元素
printstack(sqlink); //打印栈里面的元素
return 0;
}
运行结果
data[0]=2 data[1]=4 data[2]=6 data[3]=8 ****************** data[0]=2 data[1]=4 data[2]=6
3、链栈的定义与操作(常用)
栈的链式存储结构称为链栈,通常采用单链表实现,并规定所有操作都是按单链表的表头进行,这里的链栈没有头结点,如下图所示
1)链栈的搭建
#include<stdio.h>
typedef struct Node
{
int data;
struct Node *next;
}NODE;
typedef struct Stack
{
NODE * pTop;
NODE * pBottom;
}STACK;
2)创建一个空的链栈
void CreateEmptyStack( STACK * head)
{
head->ptop = (NODE *)malloc(sizeof(NODE));
if(NULL == head )
{
printf("创建链栈失败\n");
exit(0);
}
else
{
head-> pBottom = head->ptop;
head->ptop->next=NULL;
}
}
3)进栈(插入元素)
void InsertNode(STACK *head, int InsertData)
{
NODE * pnewNode = (NODE *)malloc(sizeof(NODE));
pnewNode->data = InsertData;
pnewNode->next =head->ptop;
head->ptop = pnewNode;
}
4、打印链栈数据
void PrintfStack(STACK *head)
{
NODE * p =head->ptop;
while( p != head->pBottom )
{
printf("data=%d \n",p->data);
p = p->next;
}
}
4.1简单插入元素和打印链栈的演示(完整代码)
#include <stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}NODE;
typedef struct Stack
{
NODE * ptop;
NODE * pBottom;
}STACK;
void CreateEmptyStack( STACK * head)
{
head->ptop = (NODE *)malloc(sizeof(NODE));
if(NULL == head )
{
printf("创建链栈失败\n");
exit(0);
}
else
{
head-> pBottom = head->ptop;
head->ptop->next=NULL;
}
}
void InsertNode(STACK *head, int InsertData)
{
NODE * pnewNode = (NODE *)malloc(sizeof(NODE ));
pnewNode->data = InsertData;
pnewNode->next =head->ptop;
head->ptop = pnewNode;
}
void PrintfStack(STACK *head)
{
NODE * p =head->ptop;
while( p != head->pBottom )
{
printf("data=%d \n",p->data);
p = p->next;
}
}
int main()
{
STACK * newstack=(STACK *)malloc(sizeof(STACK)); //创建一个名为newstac链栈
CreateEmptyStack(newstack);
InsertNode(newstack, 5);
InsertNode(newstack, 8);
InsertNode(newstack, 4);
PrintfStack(newstack);
return 0;
}
运行结果
data=4
data=8
data=5
5、链栈的出栈(即删除栈顶第一个元素)
int DeleteStacklist(STACK *head, int *e)
{
if(head->ptop == head->pBottom)
{
printf("栈为空\n");
exit(0);
}
else
{
NODE * DeleteNode = head->ptop;
*e = DeleteNode->data;
head->ptop = DeleteNode->next;
free(DeleteNode);
DeleteNode=NULL;
return 1;
}
5.1简单的出栈演示(完整代码)
#include <stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}NODE;
typedef struct Stack
{
NODE * ptop;
NODE * pBottom;
}STACK;
void CreateEmptyStack( STACK * head)
{
head->ptop = (NODE *)malloc(sizeof(NODE));
if(NULL == head )
{
printf("创建链栈失败\n");
exit(0);
}
else
{
head-> pBottom = head->ptop;
head->ptop->next=NULL;
}
}
void InsertNode(STACK *head, int InsertData)
{
NODE * pnewNode = (NODE *)malloc(sizeof(NODE ));
pnewNode->data = InsertData;
pnewNode->next =head->ptop;
head->ptop = pnewNode;
}
void PrintfStack(STACK *head)
{
NODE * p =head->ptop;
while( p != head->pBottom )
{
printf("data=%d \n",p->data);
p = p->next;
}
}
int DeleteStacklist(STACK *head, int *e)
{
if(head->ptop == head->pBottom)
{
printf("栈为空\n");
exit(0);
}
else
{
NODE * DeleteNode = head->ptop;
*e = DeleteNode->data;
head->ptop = DeleteNode->next;
free(DeleteNode);
DeleteNode=NULL;
return 1;
}
}
int main()
{
STACK * newstack=(STACK *)malloc(sizeof(STACK)); //创建一个名为newstac链栈
int e;
CreateEmptyStack(newstack);
InsertNode(newstack, 5);
InsertNode(newstack, 8);
InsertNode(newstack, 4);
PrintfStack(newstack);
printf("删除栈顶元素\n");
DeleteStacklist(newstack, &e);
PrintfStack(newstack);
return 0;
}
运行结果
data=4
data=8
data=5
删除栈顶元素
data=8
data=5
6、清空栈
void clear(STACK * head)
{
if(head->ptop == head->pBottom)
{
printf("栈为空\n");
exit(0);
}
else
{
NODE *p = head->ptop;
NODE *q =NULL;
while(p != head->pBottom)
{
q = p->next;
free(p);
p = q;
}
head->ptop = head->pBottom;
}
}
6.1简单的清空使用
#include <stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}NODE;
typedef struct Stack
{
NODE * ptop;
NODE * pBottom;
}STACK;
void CreateEmptyStack( STACK * head)
{
head->ptop = (NODE *)malloc(sizeof(NODE));
if(NULL == head )
{
printf("创建链栈失败\n");
exit(0);
}
else
{
head-> pBottom = head->ptop;
head->ptop->next=NULL;
}
}
void InsertNode(STACK *head, int InsertData)
{
NODE * pnewNode = (NODE *)malloc(sizeof(NODE ));
pnewNode->data = InsertData;
pnewNode->next =head->ptop;
head->ptop = pnewNode;
}
void PrintfStack(STACK *head)
{
NODE * p =head->ptop;
while( p != head->pBottom )
{
printf("data=%d \n",p->data);
p = p->next;
}
}
int DeleteStacklist(STACK *head, int *e)
{
if(head->ptop == head->pBottom)
{
printf("栈为空\n");
exit(0);
}
else
{
NODE * DeleteNode = head->ptop;
*e = DeleteNode->data;
head->ptop = DeleteNode->next;
free(DeleteNode);
DeleteNode=NULL;
return 1;
}
}
void clear(STACK * head)
{
if(head->ptop == head->pBottom)
{
printf("栈为空\n");
exit(0);
}
else
{
NODE *p = head->ptop;
NODE *q =NULL;
while(p != head->pBottom)
{
q = p->next;
free(p);
p = q;
}
head->ptop = head->pBottom;
}
}
int main()
{
STACK * newstack=(STACK *)malloc(sizeof(STACK)); //创建一个名为newstac链栈
int e;
CreateEmptyStack(newstack);
InsertNode(newstack, 5);
InsertNode(newstack, 8);
InsertNode(newstack, 4);
PrintfStack(newstack);
printf("删除栈顶元素\n");
DeleteStacklist(newstack, &e);
PrintfStack(newstack);
clear(newstack);
printf("清空栈\n");
PrintfStack(newstack);
return 0;
}
运行结果
data=4
data=8
data=5
删除栈顶元素
data=8
data=5
清空栈