今天一上午就在琢磨栈的结构和特点,后来开始编码,因为栈的队列的操作特性比线性表复杂一些,所以今天花了一个上午
和半个下午才做好,不过还是挺高兴的,最后还是做出来了。
好啦,还是和以前一样和大家一起来分享一下代码把:
如果代码中有什么错误,希望您不吝赐教,您可以把您的意见发送到yijiyong100@163.com,我会尽快给您回复的。如果代码中有什么错误,希望您不吝赐教,您可以把您的意见发送到yijiyong100@163.com,我会尽快给您回复的。
C语言 栈的线性实现和表示 栈的实现和表示
/****************************************//*Description: Sequence Stack*//*Email:yijiyong100@163.com*//*Author:yi_landry Haerbin Normal University Computer Science*//*Date:2008-5-1*//*Copyright:HNU2008.cop*//*Environment:turbo c 2.01 English Version*//****************************************/# include<stdlib.h># include<stdio.h># define STACK_INIT_SIZE 200# define STACK_INCREMENT 20# define OVERFLOW 0# define OK 1/****************************************//*The struct of sequence stack *//****************************************/struct SqStack{ int * base;/*the base pointer of the sequence stack*/ int * top;/*the top pointer of the sequence stack*/ int stacksize;/*the size of the sequence stack*/ int length;/*the total number of the elements in the stack*/}S;/****************************************//*Initial the stack*//****************************************/InitStack(struct SqStack * S){ S->base=S->top=(int *)malloc(STACK_INIT_SIZE*sizeof(int)); if (!S->base && !S->top) exit(OVERFLOW); S->stacksize = STACK_INIT_SIZE; S->length = 0; printf("Initial the sequence stack successfully!/n");}/****************************************//*Push a element into the stack*//****************************************/StackPush(struct SqStack * S,int elm){ if(S->length >= S->stacksize) { S->base = (int *)realloc(S->base,(S->stacksize+STACK_INCREMENT)*(sizeof(int))); if (!S->base) exit(OVERFLOW); S->top = S->base+S->length; S->stacksize += STACK_INCREMENT; } if (S->length == 0) { *(S->base) = elm; S->top ++; S->length ++; printf("The new element %d was pushed into the Stack successfully!/n",elm); } else { *(S->top) = elm; S->top++; S->length++; printf("The new element %d was pushed into the Stack successfully!/n",elm); }}/****************************************//*Pop the element at the top in the stack*//****************************************/StackPop(struct SqStack * S){ int PopElm; PopElm = * (S->top-1); if (S->length == 0) { printf("The stack now is empty,you can not pop again!/n"); } if (S->length == 1) { S->top--; *(S->top)=*(S->base)=NULL; S->length--; printf("Pop the element %d successfully!/n",PopElm); } else { S->top--; *(S->top)=NULL; S->length--; printf("Pop the element %d successfully!/n",PopElm); }}/****************************************//*Judge the stack is empty or not*//****************************************/StackEmpty(struct SqStack * S){ if (S->length==0) { printf("The stack now is empty!/n"); } else { printf("The stack now is not empty!/n"); PrintStack(S); }}/****************************************//*Get the length of the stack*//****************************************/StackLength(struct SqStack * S){ printf("The length of current stack is %d/n",S->length);}/****************************************//*Destroy the stack and free the memory distributed at first*//****************************************/DestroyStack(struct SqStack * S){ free(S->base); free(S->top); S->length = 0; printf("Destroy the stack successfully!/n");}/****************************************//*Clear all the elements in the stack*//****************************************/ClearStack(struct SqStack * S){ int *p,count =0; p = S->base; while(1) { if (count == S->length) break; *p =NULL; p++; } S->length = 0; S->top=S->base; printf("Clear the stack successfully!/n");}/****************************************//*Get the element at tbe location i *//****************************************/GetElement(struct SqStack * S,int i){ int *p,count = 1; p = S->base; if (i<=0 || i>S->length) { printf("The location %d you input not valid!/n",i); } else { while (1) { if (count == i) break; p++; count++; } printf("The element at the location %d is %d/n",i,*p); }}/****************************************//*Find a number if or not in the stack*//****************************************/FindElement(struct SqStack * S,int elm){ int *p,count=0,totalFind=0; p = S->base; while (1) { if(count == S->length) break; if (*p == elm) { totalFind++; printf("We find %d at the location of %d in the stack!/n",elm,count+1); } p++; count++; } if (totalFind ==0) { printf("We can not find %d in the stack!/n",elm); } else { printf("We totally find %d <%d> in the stack/n",elm,totalFind); }}/****************************************//*Get prior element of some element in the stack*//****************************************/PriorElement(struct SqStack * S,int elm){ int *p,count=0,totalFind=0; p = S->base; while (1) { if(count == S->length) break; if (*p == elm) { if (p == S->base) { printf("We can not find the prior of %d in the stack!/n",elm); } else { printf("We find prior of %d is %d at the location of %d in the stack!/n",elm,*(p-1),count); totalFind++; } } p++; count++; } if(totalFind == 0) { printf("We can not find the prior of %d in the stack!/n",elm); }}/****************************************//*Get next element of some element in the stack*//****************************************/NextElement(struct SqStack * S,int elm){ int *p,count=0,totalFind=0; p = S->base; while (1) { if(count == S->length) break; if (*p == elm) { if (p == S->top-1) { printf("We can not find the next of %d in the stack!/n",elm); } else { printf("We find next of %d is %d at the location of %d in the stack!/n",elm,*(p+1),count+2); totalFind++; } } p++; count++; } if(totalFind == 0) { printf("We can not find the next of %d in the stack!/n",elm); }}/****************************************//*Print the stack *//****************************************/PrintStack(struct SqStack * S){ int *p,count=0; p = S->top; if (S->length==0) { printf("The stack now is empty!"); } else if (S->length == 1) { p--; printf("The current stack:/n"); printf(" _______ /n"); printf(" | |/n"); printf("top -->|_______|/n"); printf(" | %3d |/n",*p); printf("base-->|_______|/n"); } else { printf("The current stack:/n"); while(1) { if (count==0) { p--; printf(" _______ /n"); printf(" | |/n"); printf("top -->|_______|/n"); printf(" | %3d |/n",*p); printf(" |_______|/n"); count++; } else if (count==S->length-1) { p--; printf(" | %3d |/n",*p); printf("base-->|_______|/n"); break; } else { p--; printf(" | %3d |/n",*p); printf(" |_______|/n"); count++; } } }}/****************************************//*Show a example to the user*//****************************************/StackExample(){ InitStack(&S); StackPush(&S,1); StackPush(&S,2); StackPush(&S,3); StackPush(&S,4); StackPush(&S,5); StackPush(&S,6); StackPush(&S,5); StackPush(&S,6); StackPush(&S,60); StackPush(&S,50); StackPush(&S,7); StackPush(&S,8); StackPush(&S,9); FindElement(&S,6); FindElement(&S,7); FindElement(&S,10); PriorElement(&S,6); PriorElement(&S,10); GetElement(&S,3); PrintStack(&S); StackPop(&S); PrintStack(&S); StackPush(&S,18); StackLength(&S); DestroyStack(&S); StackLength(&S); return 0;}/****************************************//*Show the help information to the user*//****************************************/void help(){ printf(" /*****************************************************************//n"); printf(" /*****************************************************************//n"); printf(" /* The sequence stack Operation Version 1.0 *//n"); printf(" /* View the example information 1 *//n"); printf(" /* Push a number into the stack 2 *//n"); printf(" /* Pop a number from the stack 3 *//n"); printf(" /* Find a number if or not in the stack 4 *//n"); printf(" /* Get the prior of some number from the stack 5 *//n"); printf(" /* Get the next of some number from the stack 6 *//n"); printf(" /* Get the size of stack 7 *//n"); printf(" /* Show the help information 8 *//n"); printf(" /* View current stack information 9 *//n"); printf(" /* Exit sequence stack Q *//n"); printf(" /*****************************************************************//n");}main(){ char userInput; int insertItem,findItem,priorItem,nextItem; help(); InitStack(&S); while(1) { printf("Slect the operation you want to do: input the correspond number!you can enter 1 to view the example ,and enter 8 to view the help./n"); scanf("%c",&userInput); switch(userInput) { case '1':StackExample();break; case '2': for(;;) { printf("Please input a integer you want to push into the stack:/n"); printf("Forexample 1 . if you do not want to push a number into the stack again,you can input -1/n"); scanf("%d",&insertItem); if (insertItem == -1) break; StackPush(&S,insertItem); PrintStack(&S); } break; case '3': StackPop(&S); PrintStack(&S); break; case '4': for(;;) { printf("Please input a integer you want to find from the stack:/n"); printf("Forexample 1 . if you do not want to find a number from the stack again,you can input -1/n"); scanf("%d",&findItem); if (findItem == -1) break; FindElement(&S,findItem); PrintStack(&S); } break; case '5': for(;;) { printf("Please input a integer you want to get the prior from the stack:/n"); printf("Forexample 1. if you do not want to get the prior form the stack again,you can input -1/n"); scanf("%d",&priorItem); if (priorItem == -1) break; PriorElement(&S,priorItem); PrintStack(&S); } break; case '6': for(;;) { printf("Please input a integer you want to get the next from the stack:/n"); printf("Forexample 1. if you do not want to get the next form the stack again,you can input -1/n"); scanf("%d",&nextItem); if (nextItem == -1) break; NextElement(&S,nextItem); PrintStack(&S); } break; case '7':StackLength(&S);break; case '8':help();break; case '9':PrintStack(&S);break; case 'Q':break; case 'q':break; } if (userInput == 'q'|| userInput == 'Q') break; } return 0;}如果代码中有什么错误,希望您不吝赐教,您可以把您的意见发送到yijiyong100@163.com,我会尽快给您回复的。