数据结构--顺序表,链表,双向链表-CSDN博客
目录
栈分为顺序栈和单链栈,但是他们的本质都是线性表。而后面我们还会学到队列,栈和队列的区别是:栈只能从栈顶进行插入和删除,而队列只能从对头进行删除,从队尾进行删除,队尾插入。
(先进后出)
栈是一种特殊的线性表,只能从一端插入或删除操作。
3.1顺序栈
3.1.1栈的声明
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
3.1.2栈的初始化
void STInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void STDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
3.1.3入栈
void STPush(ST* ps, STDataType x)
{
assert(ps);
// ˣ
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
3.1.4出栈
void STPop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
ps->top--;
}
STDataType STTop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
return ps->a[ps->top - 1];
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
3.1.5读取栈顶元素
//读取栈顶元素
void Getstack(seqstack& S) {
if (S.top == -1) {
printf("栈为空,没有元素输出!");
}
{
printf("当前栈顶元素为:");
printf("%d\n", S.data[S.top]);
}
}
3.1.6读取栈中元素
、
//输出栈中元素
void Printstack(seqstack& S) {
if (S.top == -1) {
printf("栈为空,没有元素输出!");
}
else {
printf("栈中元素如下:");
for (int i = 0; i < size; i++) {
printf("%d", S.data[i]);
}
printf("\n");
}
}
3.1.7打印栈元素
void StackPrint(Stack* st)
{
assert(st);
assert(!StackEmpty(st));
for (int i = st->top - 1; i >= 0; i--)
{
printf("%d\n", st->a[i]);
}
}
3.1.8销毁栈
void StackDestroy(Stack* st)
{
assert(st);
free(st->a);
st->a = NULL;
st->top = st->capacity = 0;
}
3.1.9删除栈顶元素
//删除栈顶元素
int Deletestack(seqstack& S) {
if (S.top == -1) {
printf("栈为空!\n");
}
else
{
int e;
for (int i = 0; i < size; i++) {
e = S.data[S.top];
S.top = S.top - 1;
return e;
}
printf("所有元素已被删除!\n");
}
}
顺序栈的缺点:栈的大小不可以发生改变
3.2链栈
链栈,就是按照链式存储的栈
3.2.1声明
typedef struct SListNode
{
STDataType data;
struct SListNode* next;
}SListNode;
typedef struct Stack
{
SListNode* top;
int size;
}Stack;
3.2.2初始化
、
//初始化
void Iniststack(Linkstack *L) {
L= (LinkNode*)malloc(sizeof(LinkNode));
if (!L->data) {
printf("申请失败");
}
else
{
L->data = 0;
L->next = NULL;
}
}
3.2.3入栈
//入栈
void Pushstack(Linkstack *L) {
int e,x;
printf("请输入你要创建的链栈长度:");
scanf("%d", &x);
printf("请输入你要入栈的元素:\n");
for (int i = 0; i < x; i++) {
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
scanf("%d", &e);
s->data = e;
s->next = L->next;
L->next = s;
}
}
3.2.4出栈
//出栈
int Popstack(Linkstack* L)
{
LinkNode* s= L->next;
s->data = L->data;
L->next = s->next;
return s->data;
}
3.2.5读取栈顶元素
//读取栈顶元素
int Getstack(Linkstack* L) {
if (!L->data)
{
printf("栈为空!");
}
else {
int e = L->next->data;
return e;
}
}
3.2.6读取栈中元素
//输出栈中元素
void Printsatck(Linkstack* L) {
if (!L->data)
{
printf("栈为空!");
}
else {
LinkNode* p;
p = L;
printf("栈中元素如下:");
while (p)
{
p = p->next;
printf("%d", p->data);
}
}
}
3.2.7打印栈元素
void StackPrint(Stack* st)
{
assert(st);
assert(!StackEmpty(st));
for (SListNode* top = st->top; top!=NULL; top=top->next)
{
printf("%d\n", top->data);
}
}
3.2.8销毁栈
void StackDestroy(Stack* st)
{
assert(st);
SListNode* top = st->top;
while(top!=NULL)
{
SListNode* next = top->next;
free(top);
top = next;
}
st->size = 0;
}