一,栈
1,栈
一种特殊的受限的线性表,限制线性表只能在一端进行操作。且满足先进后出,后进先出的原则。
2,栈顶,栈底和空栈
栈顶:能够被允许操作的一端。
栈底:不能被允许操作的一端。
空栈:栈中没有任何数据时称为空栈。
注:能被操作的一定是栈的最后一个数据,即栈顶的数据。
3,入栈和出栈
入栈:从栈顶添加一个数据就称为入栈。
出栈:从栈顶删除一个数据就称为出栈。
4,栈顶指针
用于存储栈顶指针的地址,只需操作栈顶指针就能使用整个栈。
二,顺序栈
逻辑结果为线性结构(栈),存储结构为顺序存储 顺序表,限制对数据的操作只能在顺序表的⼀端进⾏。 固定栈底位置,栈顶位置从栈底位置开始移动,⽤来表⽰当前能够操作的位置
1,顺序栈的创建
在顺序表末尾操作 申请⼀段连续的空间,⽤地址来表⽰先后顺序,存储数据,限制只能在⼀端 进⾏操作
struct stack
{
int data[10]; //栈的数据大小
int top; //栈顶指针
};
struct stack * createstack() //创建一个顺序栈
{
struct stack *p = malloc(sizeof(struct stack)); //p代表顺序栈的地址。
p -> top = 0; //使得当前栈顶的位置为栈底位置,代表的含义为空栈
return p;
}
2,入栈
入栈的两个操作:一,判断栈中的数据是否存满;二,移动栈顶的位置。
//要向栈中存入一个数据首先要判断栈中的数据是否存满。
int full(struct stack * head)
{
if (head -> top==10) //顺序栈数据的大小,首先创建时是空栈,没有存储任何数据,所以下标要和顺序栈的数据的大小保持一致。
{
printf("is flull\n");
return 1; //如果栈中的数据已经存满,则返回1.
}
else
return 0; //如果栈中的数据没有存满,则返回0.
}
//入栈
void pushstack(struct stack * head , int data) //是整个栈中的元素命名为head,存入的数据为data.
{
if(full(head))
{
return ;
}
printf("push data is %d:\n",head -> data[head->top] = data); //数据的存入。
head -> top++; //每当存入一个数据,栈顶的位置也就随着变更。
}
//函数调用
//函数调用
int main()
{
struct stack * p = createstack();
pushstack(p,10); //入栈
pushstack(p,11);
pushstack(p,112);
return 0;
}
3,出栈
//进行出栈也就是删除数据时首先得判断栈中的数据是否为空。
int empty(struct stack *head)
{
if(head->top==0)
{
printf("is empty\n");
return 1;
}
else
return 0;
}
void popstack(struct stack *head)
{
if(empty(head))
{
return ;
}
head -> top--;
printf("delete data is %d:\n",head -> data[head ->top]);
}
//函数调用
int main()
{
struct stack * p = createstack();
popstack(p); //代表出栈
pushstack(p,10);
pushstack(p,11);
pushstack(p,112);
return 0;
}
栈的基本操作:
#include<stdio.h>
#include<stdlib.h>
struct stack
{
int data[10]; //栈的数据大小
int top; //栈顶指针
};
struct stack * createstack() //创建一个顺序栈
{
struct stack *p = malloc(sizeof(struct stack)); //p代表顺序栈的地址。
p -> top = 0; //使得当前栈顶的位置为栈底位置,代表的含义为空栈
return p;
}
//要向栈中存入一个数据首先要判断栈中的数据是否存满。
int full(struct stack * head)
{
if (head -> top==10) //顺序栈数据的大小,首先创建时是空栈,没有存储任何数据,所以下标要和顺序栈的数据的大小保持一致。
{
printf("is flull\n");
return 1; //如果栈中的数据已经存满,则返回1.
}
else
return 0; //如果栈中的数据没有存满,则返回0.
}
//入栈
void pushstack(struct stack * head , int data) //是整个栈中的元素命名为head,存入的数据为data.
{
if(full(head))
{
return ;
}
printf("push data is %d:\n",head -> data[head->top] = data); //数据的存入。
head -> top++; //每当存入一个数据,栈顶的位置也就随着变更。
}
//进行出栈也就是删除数据时首先得判断栈中的数据是否为空。
int empty(struct stack *head)
{
if(head->top==0)
{
printf("is empty\n");
return 1;
}
else
return 0;
}
void popstack(struct stack *head)
{
if(empty(head))
{
return ;
}
head -> top--;
printf("delete data is %d:\n",head -> data[head ->top]);
}
int main()
{
struct stack * p = createstack();
popstack(p);
pushstack(p,10);
pushstack(p,11);
pushstack(p,112);
return 0;
}
三,链式栈
逻辑结构是栈(特殊线性关系),存储结构为链式存储 链表,限制在链表的⼀端进⾏操作。
1,链式栈的创建
链式栈的创建一般在头节点后作为链表的开始位置。
struct node
{
int data;
struct nodde * next;
}
//链式栈的创建。在头节点后作为链表的开始位置。
struct node * create()
{
struct node * head = malloc(sizeof(struct node));
if(head == NULL)
{
printf("create error\n"); //判断头节点位置的数据
return NULL;
}
head ->next = NULL;
return head;
}
2,入栈
头节点始终指向栈顶的数据的地址。
//入栈
void pushstact(struct node * head ,int data)
{
struct node * p = malloc(sizeof(struct node));
p->data = data;
p->next = head->next;
head->next = p;
}
//函数调用
int main()
{
struct node * head = create();
pushstack(head,10);
pushstack(head,20);
pushstack(head,30);
pushstack(head,40);
return 0;
}
3,出栈
//判断是否有数据
int empty(struct node * head)
{
if( head->next == NULL )
{
printf("is empty\n");
return 1;
}
else
return 0;
}
//出栈
void popstact(struct node * head)
{
if(empty(head))
return ;
struct node * p = head -> next;
head->next = p->next;
printf("delete data is %d:\n",p->data);
}
int main()
{
struct node * head = create();
popstack(head);
pushstack(head,10);
pushstack(head,20);
pushstack(head,30);
pushstack(head,40);
popstack(head);
popstack(head);
return 0;
}
链式栈的综合操作
//链式栈
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
//链式栈的创建。在头节点后作为链表的开始位置。
struct node * create()
{
struct node * head = malloc(sizeof(struct node));
if(head == NULL)
{
printf("create error\n"); //判断头节点位置的数据
return NULL;
}
head ->next = NULL;
return head;
}
//入栈
void pushstack(struct node * head ,int data)
{
struct node * p = malloc(sizeof(struct node));
p->data = data;
p->next = head->next;
head->next = p;
}
//判断是否有数据
int empty(struct node * head)
{
if( head->next == NULL )
{
printf("is empty\n");
return 1;
}
else
return 0;
}
//出栈
void popstack(struct node * head)
{
if(empty(head))
return ;
struct node * p = head -> next;
head->next = p->next;
printf("delete data is %d:\n",p->data);
}
int main()
{
struct node * head = create();
pushstack(head,10);
pushstack(head,20);
pushstack(head,30);
pushstack(head,40);
popstack(head);
popstack(head);
return 0;
}