linktostack.txt
来自「用链表实现栈的操作.该程序有栈的进栈和出栈操作.很简单.这是本人第一次上传程序源」· 文本 代码 · 共 82 行
TXT
82 行
#include "stdio.h"
#include "stdlib.h"
typedef struct node
{//链表结构
char data;
struct node *next;
}stnode;
void init(stnode *stacklink)
{//初始化链表
stacklink->next=NULL;
}
void push(stnode *stacklink)
{//进行进链表操作
char ch;
stnode *mode;
mode=(stnode *)malloc(sizeof(stnode));
printf("please input one char:");
ch=getch();
printf("%c",ch);
printf("\n");
if(ch!='\n')
{ mode->data=ch;
mode->next=NULL;
if(stacklink->next==NULL)
{ stacklink->next=mode;
}
else
{ mode->next=stacklink->next;
stacklink->next=mode;
} } }
void pop(stnode *stacklink)
{//出链表操作
stnode *dele;
dele=(stnode *)malloc(sizeof(stnode));
dele=stacklink->next;
if(dele!=NULL)
{
stacklink->next=dele->next;
dele->next=NULL;
printf("output stack char:%c\n",dele->data);
}
else
printf("the stack is empty.\n");
free(dele);
}
void linktostack()
{//用链表模拟栈的操作函数
char ch;
stnode *stacklink;
stacklink=(stnode *) malloc(sizeof(stnode));
init(stacklink);
printf("\t1.push\t2.pop\t0.exit\n");
printf("please input your opretor:");
scanf("%c",&ch);
printf("\n");
while(1)
{
if(ch=='1')
{
printf("push\n");
push(stacklink);
}
else if(ch=='2')
{
printf("pop\n");
pop(stacklink);
}
else
exit(0);
printf("-----------------------------
--------\n"); main()
printf("\t1.push\t2.pop\t0.exit\n"); {
printf("please input your opertor:"); clrscr();
ch=getch(); printf("the star:\n");
printf("%c",ch); linktostack();
printf("\n"); return 0;
} }
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?