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

📄 lianshiduizhan.c

📁 用链表建立堆栈的简单代码
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct outnode{
   int i;
   char *string;
   struct outnode *next;
   }outnode;

int getkey ( )
{
    union  star
    {
       unsigned int x;
       unsigned char y[2];
    }key1;
    do
    {
        key1.x = bioskey( 0 );
        return ( key1.y[1] );
    }while ( 1 );
}

char * pop(struct outnode *top,struct outnode * head)
{
  top=top->next;
  if(top->next->next->next==head)
  {
    printf("Empty stack.");
    getch();
    exit(0);
  }
  return((*top).string);
}

struct outnode * jianzhan(struct outnode *head)
{
  int t=0,key;
  struct outnode *top,*p;
  p=(struct outnode *)malloc(sizeof(struct outnode));
  (*p).i=0;
  (*p).string=NULL;
  head=p;
  p=(struct outnode *)malloc(sizeof(struct outnode));
  p->next=head;
  top=p;
  printf("Please inpput the information you want to restore:\n");
  do{
    gets((*top).string);
    (*top).i=++t;
    (*head).i+=1;
    p=(struct outnode *)malloc(sizeof(struct outnode ));
    p->next=top;
    top=p;
    printf("\nDo you want to continue to input more information?(y/n)\n");
    key=getkey();
    }while(key!=49);
  return(top);
}

void main()
{
  int key;
  struct outnode *top,*head=NULL;
  top=jianzhan(head);
  printf("\nDo you want to get the top elem of the stack?(y/n)\n");
  key=getkey();
  while(key!=49)
  {
    puts(pop(top,head));
    top=top->next;
    printf("\nCONTINUE?(y/n)\n");
    key=getkey();
    if(key==49)
       break;
  }
  getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -