⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linktostack.txt

📁 用链表实现栈的操作.该程序有栈的进栈和出栈操作.很简单.这是本人第一次上传程序源代码,如有什么不足之处还望各位多多海涵.
💻 TXT
字号:

#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -