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

📄 4-4-10.c

📁 2005软件工程师考试下午编程题源代码
💻 C
字号:
/*中国系统分析员顾问团,http://www.csai.cn*/
/*程序员下午考试指南书籍源码*/

#include <stdio.h>
#include <malloc.h>
typedef struct node{
int value;
struct node *next;
}NODE, *PNODE;

int push(PNODE *top, int e){
PNODE p = (PNODE)malloc(sizeof(NODE));
if(!p)  return -1;
p->value = e;
p->next = *top;
*top = p;
return 0;
}

int pop(PNODE *top,int *e){
PNODE p = *top;
if(p == NULL) return -1;
*e = p->value;
*top = p->next;
free(p);
return 0;
}

int enQueue(PNODE *tail,int e){ 
PNODE p, t;
t = *tail;
p = (PNODE)malloc(sizeof(NODE));
if(!p) return -1;
p->value = e;
p->next = t->next;
t->next = p;
*tail = p;
return 0;
}

int deQueue(PNODE *tail,int *e){
PNODE p,q;
if((*tail)->next == *tail)return -1;
p = (*tail)->next;
q = p->next;
*e = q->value;
p->next = q->next;
if(*tail = q) *tail = p;
free(q);
return 0;
}

main()
{
  PNODE *stackhead,queuehead;
  int temp=0;
  stackhead=(PNODE *)malloc(sizeof(PNODE));
  push(stackhead,1);
  push(stackhead,2);
  push(stackhead,3);
  pop(stackhead,&temp);
  printf("1:POP()---%d\n",temp);
  pop(stackhead,&temp);
  printf("2:POP()---%d\n",temp);
  pop(stackhead,&temp);
  printf("3:POP()---%d\n",temp);
  
  queuehead=(PNODE )malloc(sizeof(PNODE));
  queuehead->next=queuehead;
  enQueue(&queuehead,1);
  enQueue(&queuehead,2);
  enQueue(&queuehead,3);
  deQueue(&queuehead,&temp);
  printf("1:deQueue()---%d\n",temp);
  deQueue(&queuehead,&temp);
  printf("2:deQueue()---%d\n",temp);
  deQueue(&queuehead,&temp);
  printf("3:deQueue()---%d\n",temp);

}

⌨️ 快捷键说明

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