链栈的实现代码:
主要实现链存储栈的各种常见操作,后续,在文章结尾目录里面继续更新源代码,可以收藏一波!
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef struct LinkNode {
int data;
struct LinkNode *next;
}LinkNode, * LinkStack;
void initStack(LinkStack &s) {
s = (LinkStack)malloc(sizeof(LinkNode));
s->next = NULL;
}
bool isEmpty(LinkStack s) {
if (s->next == NULL) {
return true;
}
else
{
return false;
}
}
void push(LinkStack &s, int x) {
LinkStack p = s;
LinkStack q = (LinkStack)malloc(sizeof(LinkNode));
q->next = p->next;
p->next = q;
q->data = x;
}
void pop(LinkStack &s) {
LinkStack p = s->next;
s->next = p->next;
p->next = NULL;
free(p);
}
void showStack(LinkStack s) {
LinkStack p = s;
while (!isEmpty(p)){
p = p->next;
printf("%d\n",p->data);
}
}
int main() {
LinkStack s;
initStack(s);
push(s, 1);
push(s, 2);
push(s, 3);
push(s, 4);
push(s, 5);
push(s, 6);
pop(s);
pop(s);
pop(s);
pop(s);
pop(s);
showStack(s);
}
查看考研数据结构目录:
https://www.acwing.com/file_system/file/content/whole/index/content/5394132/