数据结构实验程序参考 联系客服

发布时间 : 星期三 文章数据结构实验程序参考更新完毕开始阅读7b06cd0c763231126edb11c9

else {pre=q; q=q->next;}

} p=p->next; } }

3.栈操作

(1)顺序栈 typedef int elemtype; #include \

/* ========menu ======== */ char menu(void) { char ch;

clrscr(); printf(\ printf(\

printf(\ MENU \\n\ printf(\ ===========================\\n\ printf(\ Seqential stack operations\\n\ printf(\ 1. push\\n\ printf(\ 2. pop\\n\ printf(\ 3. get top\\n\ printf(\ 0. exit\\n\

printf(\ ===========================\\n\ printf(\ Choice(0,1,2,3):\ ch=getchar();

return(ch); } main() { SqStack st; int i,flag=1,n,k; char choice; InitStack(&st); do{ choice=menu(); clrscr(); switch (choice)

{ case '1': printf(\ push data=?\

k=Push(&st, n); if(k) printf(\else printf(\getch();break;

case '2': k=Pop(&st,&n);

if(k) printf(\ pop data=%d\\n\else printf(\ The stack is empty.\getch( );break;

case '3': k=GetTop(st, &n);

if(k)printf(\ top data=%d\\n\else printf(\ The stack is empty.\getch( ); break;

case '0': flag=0; }

}while(flag==1);} (2)链栈 #define Max 100 #define elementtype int typedef struct

{ elementtype data[Max]; int top; }stacktype;

void Push(stacktype *s,elementtype x) { if(s->top==Max-1)

{ printf(\ s->top++; s->data[s->top]=x; }

int stackempty(stacktype s) {if(s.top==-1) return(1); else return(0); }

void Pop(stacktype *s,elementtype *e) { if(stackempty(*s))

{ printf(\ else

{ *e=s->data[s->top];

s->top--; } }

void MENU()

{printf(\ printf(\ printf(\ printf(\

printf(\ printf(\} main() { stacktype s; int c; char x, e; s.top=-1; do { MENU(); scanf(\ switch(c)

{case 1:printf(\

scanf(\ Push(&s,x); break; case 2:Pop(&s,&e);