?? 4-4-10.c
字號:
/*中國系統(tǒng)分析員顧問團,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 + -