#include <stdio.h>

#include <stdlib.h>

#include <string.h>


typedef struct node

{

int number;

struct node *next;

}Node,*pNode;


typedef struct stack

{

pNode top;

pNode bottom;

}Stack,*pStack;


void InitStack(pStack p)

{

p->top = (pNode*)malloc(sizeof(Node));


if ( !p )

{

printf("内存分配失败......");

exit(-1);

}


p->bottom = p->top;

p->top->next = NULL;


}



int Push(pStack p,int data)

{

pNode pNew = (pNode*)malloc(sizeof(Node));


if ( !pNew )

{

printf("内存分配失败......");

exit(-1);

}


pNew->number = data;

pNew->next = p->top;


p->top = pNew;


return 1;

}


int Pop(pStack p)

{

pNode pSwap = NULL;

int val;


if ( Empty(p) )

{

exit(-1);

}


val = p->top->number;

pSwap = p->top;

p->top = p->top->next;


free(pSwap);


return val;

}



int Empty(pStack p)

{

if ( p->top == p->bottom )

{

return 1;

}

else

{

return 0;

}

}



void TravseStack(pStack p)

{

pNode pNew;

pNew = p->top;


while ( pNew != p->bottom )

{

printf("%d==>",pNew->number);

pNew = pNew->next;

}

printf("END\n");

}


int GetLength(pStack p)

{

int count=0;

pNode pNew;

pNew = p->top;


while ( pNew != p->bottom )

{

pNew = pNew->next;

count++;

}

return count;

}


int GetTop(pStack p)

{

if ( Empty(p) )

{

return -1;

}


return p->top->number;

}



void DestoryStack(pStack p)

{

pNode pSwap = NULL;


while ( p->top != p->bottom )

{

pSwap = p->top;

p->top = p->top->next;


free(pSwap);

}

}




int main(void)

{

    Stack s;                        //    定义一个栈

int k=1,choose;

    int i,r,r1,r2;

    int num;

    int data;                        //    临时保存用户输入的数据

    int re_num; //    保存Pop函数的返回值


while ( k == 1 )

{

puts("++++++++++++++++++++++++++++++++++++++++++++++++");

puts("++   主菜单:\n++");

puts("++   1. 初始化栈\n++");

puts("++   2. 元素入栈\n++");

puts("++   3. 元素出栈\n++");

puts("++   4. 获取栈顶元素\n++");

puts("++   5. 遍历栈元素\n++");

puts("++   6. 栈长度\n++");

puts("++   7. 销毁栈\n++");

puts("++   0. 退出\n++");

puts("++++++++++++++++++++++++++++++++++++++++++++++++");

scanf("%d",&choose);

flushall();

if ( choose>=0 && choose<=7 )

{

switch(choose)

{

case 1:

InitStack(&s);

printf("\n");

break;

case 2:

do{

printf("你想输入几个数据啊:");

r = scanf("%d",&num);

flushall();


if ( r == 1 )

{

for ( i =0 ; i<num ; i++ )

{

do

{

printf("第 %d 个数:",i+1);

r1 = scanf("%d",&data);

flushall();

if ( r1 == 1 )

{

if (Push (&s,data))     // 调用Push函数

{

continue;

}

else

{

printf("进行进栈操作失败!\n");

exit(-1);

}

}

}while ( !(r1 == 1) );

}

}

}while ( !(r == 1) );

printf("\n");

break;

case 3:

do{

printf("你想去掉几个数啊: ");

r2 = scanf("%d",&data);

flushall();

if ( r2 == 1 && data <= num)

{

printf("你去掉的数字是:");

for (i = 0; i < data;i++)

{

re_num = Pop (&s); //调用Pop函数,并把返回值赋给re_num;

printf("%d ",re_num);

}

}

}while ( !( r2 == 1 && data <= num) );

printf("\n");

break;


case 4:

printf("栈顶元素是:%d\n",GetTop(&s));

printf("\n");

break;


case 5:

TravseStack(&s);                //    调用遍历函数

printf("\n");

break;


case 6:

printf("栈的长度为:%d\n",GetLength(&s));

printf("\n");

break;


case 7:

DestoryStack(&s);                        //    调用清空栈函数

printf("遍历下看看栈清空没····\n");

TravseStack(&s);

printf("\n");

break;


case 0:

DestoryStack(&s);

k = 0;

break;

}

}

}


getch();

    return 0;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章