目录
链栈
相关小知识
代码实现
初始化链栈
向链栈中添加结点
入栈
出栈
取栈顶元素
遍历栈
销毁链栈
总代码实现
链栈是运算受限的单链表,只能在链表头部进行操作
链栈的头指针就是栈顶
不需要头结点
基本不存在栈满的情况
空栈相当于头指针指向空
插入和删除仅在栈顶处执行
int creatstack(Linkstack* S) //初始化链栈
{
(*S)= NULL;
printf("链栈初始化成功!\n");
return ok;
}
int addstack(Linkstack* S,int n) //向链栈中添加结点
{
int i;
Linkstack p;
for (i = 0; i < n; i++) {
p = (Linkstack)malloc(sizeof(Stacklode));
if (!p) {
printf("分配空间失败!\n");
}
printf("请输入第%d个结点的数据域:\n",i+1);
scanf_s("%d", &(p->data));
p->next = (*S);
(*S) = p;
}
return ok;
}
int enterstack(Linkstack* S, int e) //入栈
{
Linkstack p;
p = (Linkstack)malloc(sizeof(Stacklode));
p->data = e;
p->next = (*S);
(*S) = p;
printf("入栈成功!\n");
return error;
}
int gostack(Linkstack* S) //出栈
{
int e;
if (!(*S)) {
printf("栈已空无法出栈!\n");
return error;
}
Linkstack p;
p = (*S);
e = (p->data);
(*S) = (*S)->next;
free(p);
printf("出栈成功!\n");
printf("被删除的数据是:%d\n",e);
return ok;
}
int getstack(Linkstack S) //取栈顶元素
{
int* e;
if (!S) {
printf("栈为空操作失败!\n");
return error;
}
e = &(S->data);
return *e;
}
int printstack(Linkstack S,int n) //遍历栈
{
if (!S) {
printf("链栈为空!\n");
return error;
}
while (S) {
printf("链栈中第%d个结点的数据域为:%d \n",n--,S->data);
S = S->next;
}
return ok;
}
int destroystack(Linkstack* S) //销毁链栈
{
while (*S) {
Linkstack p;
p = (*S);
(*S) = (*S)->next;
free(p);
}
free(*S);
printf("链栈已销毁! \n");
return ok;
}
#include
#include
#define ok 1
#define error 0
typedef struct {
int data;
struct Stacklode* next;
}Stacklode,*Linkstack;
int creatstack(Linkstack* S); //初始化链栈
int addstack(Linkstack* S, int n); //向链栈中添加结点
int enterstack(Linkstack* S,int e); //入栈
int gostack(Linkstack* S); //出栈
int getstack(Linkstack S); //取栈顶元素
int printstack(Linkstack S,int n); //遍历栈
int destroystack(Linkstack* S); //销毁链栈
int main()
{
Linkstack L;
int s,k,i;
creatstack(&L);
printf("请输入链栈中需要的结点个数:\n");
scanf_s("%d", &k);
addstack(&L,k);
printstack(L,k);
printf("此时栈顶元素是:%d \n",getstack(L));
printf("请输入入栈的数据:\n");
scanf_s("%d",&s);
enterstack(&L, s);
printstack(L, k+1);
printf("此时栈顶元素是:%d \n", getstack(L));
gostack(&L);
printstack(L, k-1);
printf("此时栈顶元素是:%d \n", getstack(L));
destroystack(&L);
return 0;
}
int creatstack(Linkstack* S) //初始化链栈
{
(*S)= NULL;
printf("链栈初始化成功!\n");
return ok;
}
int addstack(Linkstack* S,int n) //向链栈中添加结点
{
int i;
Linkstack p;
for (i = 0; i < n; i++) {
p = (Linkstack)malloc(sizeof(Stacklode));
if (!p) {
printf("分配空间失败!\n");
}
printf("请输入第%d个结点的数据域:\n",i+1);
scanf_s("%d", &(p->data));
p->next = (*S);
(*S) = p;
}
return ok;
}
int printstack(Linkstack S,int n) //遍历栈
{
if (!S) {
printf("链栈为空!\n");
return error;
}
while (S) {
printf("链栈中第%d个结点的数据域为:%d \n",n--,S->data);
S = S->next;
}
return ok;
}
int enterstack(Linkstack* S, int e) //入栈
{
Linkstack p;
p = (Linkstack)malloc(sizeof(Stacklode));
p->data = e;
p->next = (*S);
(*S) = p;
printf("入栈成功!\n");
return error;
}
int gostack(Linkstack* S) //出栈
{
int e;
if (!(*S)) {
printf("栈已空无法出栈!\n");
return error;
}
Linkstack p;
p = (*S);
e = (p->data);
(*S) = (*S)->next;
free(p);
printf("出栈成功!\n");
printf("被删除的数据是:%d\n",e);
return ok;
}
int getstack(Linkstack S) //取栈顶元素
{
int* e;
if (!S) {
printf("栈为空操作失败!\n");
return error;
}
e = &(S->data);
return *e;
}
int destroystack(Linkstack* S) //销毁链栈
{
while (*S) {
Linkstack p;
p = (*S);
(*S) = (*S)->next;
free(p);
}
free(*S);
printf("链栈已销毁! \n");
return ok;
}