自己结合平时用的链表的代码和书上的链式栈的代码,写了一个简易版的链式栈,只有很简单的五个功能:
运行之后会先提示创建一个链式栈(带头节点),输入节点数量和每个节点的数据即可;之后根据提示输入对应的操作标号即可。
代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node * next;
}N,*P;
P createTopNode()
{
P top = (P)malloc(sizeof(N));
if(!top)
exit(-1);
top->next=NULL;
return top;
}
void printStack(P top)
{
P p =top->next;
if(!p)
{
printf("This Stack is Empty!\n");
return;
}
printf("The Elements in Stack are as follows:\n");
while(p)
{
printf("%d\n",p->data);
p=p->next;
}
}
void Push(P top,int data)
{
P newNode=(P)malloc(sizeof(N));
if(!newNode)
exit(-1);
newNode->data=data;
newNode->next=top->next;
top->next=newNode;
}
void Pop(P top,int *data)
{
P p =top->next;
if(!p)
{
printf("This Stack is Empty!\n");
return;
}
*data=p->data;
top->next=p->next;
free(p);
}
int GetTop(P top,int *data)
{
P p = top->next;
if(!p)
{
printf("This Stack is Empty!\n");
return -1;
}
else
{
*data=p->data;
return *data;
}
}
int StackLength(P top)
{
P p =top->next;
if(!p)
{
printf("This Stack is Empty!\n");
return -1;
}
else
{
int i = 0;
while(p)
{
i++;
p=p->next;
}
return i;
}
}
int main()
{
P top = createTopNode();
int i,n,s;
printf("Please Enter the amount of Nodes you want to create:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("The data of %d Node is:",i);
scanf("%d",&s);
Push(top,s);
}
while(1)
{
printf("1:Push\n");
printf("2:Pop\n");
printf("3:Print\n");
printf("4:GetTop\n");
printf("5:Length\n");
printf("6:Exit\n");
printf("Please Enter the Number of the operation:");
int n;
scanf("%d",&n);
switch(n)
{
case 1:
{
int s;
printf("Please Enter the data you want to push:");
scanf("%d",&s);
Push(top,s);
break;
}
case 2:
{
int s;
printf("Now Pop a Node.\n");
Pop(top,&s);
break;
}
case 3:
{
putchar('\n');
printStack(top);
putchar('\n');
break;
}
case 4:
{
int s;
printf("The top of the Stack is:%d\n",GetTop(top,&s));
break;
}
case 5:
{
printf("The Length of the Stack is:%d\n",StackLength(top));
break;
}
case 6:
return 0;
}
}
}
以下是不带头节点的版本:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node * next;
}N,*P;
void InitStack(P &top)
{
top=NULL;
}
void Push(P &top,int data)
{
P newNode=(P)malloc(sizeof(N));
newNode->data=data;
newNode->next=top;
top=newNode;
}
void Pop(P &top,int &data)
{
if(!top)
{
printf("This Stack is Empty!\n");
return;
}
data=top->data;
P p =top;
top=top->next;
free(p);
}
int GetTop(P top)
{
if(!top)
{
printf("This Stack is Empty!\n");
return 0;
}
else
return top->data;
}
int StackLength(P top)
{
if(!top)
{
printf("This Stack is Empty!\n");
return 0;
}
else
{
int i=0;
while(top)
{
i++;
top=top->next;
}
return i;
}
}
void printStack(P top)
{
if(!top)
{
printf("This Stack is Empty!\n");
return;
}
else
{
printf("The Elements in Stack are as follows:\n");
while(top)
{
printf("%d\n",top->data);
top=top->next;
}
}
}
void ClearStack(P &top)
{
P p =top;
while(top)
{
p=top;
top=top->next;
free(p);
}
}
int main()
{
P top;
InitStack(top);
int i,n,s;
printf("Please Enter the amount of Nodes you want to create:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("The data of %d Node is:",i);
scanf("%d",&s);
Push(top,s);
}
while(1)
{
printf("1:Push\n");
printf("2:Pop\n");
printf("3:Print\n");
printf("4:GetTop\n");
printf("5:Length\n");
printf("6:ClearStack\n");
printf("7:Exit\n");
printf("Please Enter the Number of the operation:");
int n;
scanf("%d",&n);
switch(n)
{
case 1:
{
int s;
printf("Please Enter the data you want to push:");
scanf("%d",&s);
Push(top,s);
break;
}
case 2:
{
int s;
printf("Now Pop a Node.\n");
Pop(top,s);
break;
}
case 3:
{
putchar('\n');
printStack(top);
putchar('\n');
break;
}
case 4:
{
printf("The top of the Stack is:%d\n",GetTop(top));
break;
}
case 5:
{
printf("The Stack has %d Nodes.\n",StackLength(top));
break;
}
case 6:
{
printf("Has Cleared your Stack.\n");
ClearStack(top);
break;
}
case 7:
return 0;
}
}
}