数据结构 栈的顺序存储与链式存储

//栈的顺序存储
#include<stdlib.h>
#include<stdio.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int status;

typedef struct{
	int *base;
	int *top;
	int stacksize;
}sqstack;


//initstack 初始化 
status initstack(sqstack &s){
    s.base = (int *) malloc(STACK_INIT_SIZE * sizeof (int));
    if (!s.base) exit (OVERFLOW); 
    s.top = s.base;
    s.stacksize = STACK_INIT_SIZE;
    return OK;
}


//push 进栈 
status push(sqstack &s,int e){
    if(s.top - s.base >= s.stacksize){   
      s.base = (int*) realloc(s.base, (s.stacksize+STACKINCREMENT) * sizeof(int));
      if (!s.base) exit (OVERFLOW);   
      s.top = s.base + s.stacksize; 
      s.stacksize += STACKINCREMENT; 
    }
    *s.top++ = e;   
    return OK;
}



// gettop
status gettop(sqstack &s,int &e){    
     if(s.top == s.base) return ERROR; 
     e = *(s.top-1);       
     return OK;
}


//pop 出 
status pop(sqstack &s,int &e){
     if(s.top == s.base)  return ERROR; 
     e = * -- s.top;       
     return OK;
}


//printstack
status printstack(sqstack s){
	if(s.base == s.top)
	{
		printf("空栈\n");
		return OK;
	}
	else
	{
		printf("从栈底到栈顶,栈内容是:\n");
	    for( ; s.base != s.top; s.base ++)
		   printf(" %d ", *s.base);
	}
	printf("\n");
	return OK;
}

		
int main(){
	sqstack s;
	if(initstack(s))
		printf("初始化栈成功\n");
	do{
		printf("   请选择要进行的操作:          \n");
		printf("   1 进栈   2 出栈   3 获得栈顶元素    4 打印   0 退出       \n");
		int select;
		scanf("%d", &select);
		if(select == 0)
			break;
		switch(select){
		case 0:
			break;
		case 1:
			int pushelem;
			printf("输入要进栈的元素:    ");
			scanf("%d", &pushelem);
			if(push(s,pushelem))
				printf("进栈成功\n");
			else
				printf("进栈失败\n");
			break;
		case 2:
			int e;
			if(pop(s,e))
				printf("元素 %d 出栈\n",e);
			else
				printf("出栈失败\n");
			break;
		case 3:
			if(gettop(s,e))	
				printf("栈顶元素是%d\n",e);
			else
				printf("获得栈顶元素失败\n");
			break; 
		case 4:
			if(printstack(s))
			    printf("打印完毕\n");
			break;		
		default:
			printf("你进行了误操作,请重新选择\n:");
			break;
		}
	}
	while(1);
	return OK;
}
//栈的链式存储
#include<stdlib.h>
#include<stdio.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int status;

typedef struct StackNode{
	int data;			//数据域 
	struct StackNode *next;	        //指针域 
}StackNode,*LinkStack;

status initstack(LinkStack &S)
{
	S=NULL;
	return true;
}

status push(LinkStack &S,int e)
{
	StackNode *p=new StackNode;	//生成新结点 
	p->data=e;			//将结点的数据域赋值为e
	p->next=S;			//将新结点插入栈顶
	S=p;				//修改栈顶指针为p
	return true; 
}

status pop(LinkStack &S,int &e)
{
	if(S==NULL)
		return false;
	e=S->data;		//将栈顶赋给e
	StackNode *p=S;		//用p临时保存栈顶元素空间,以备释放 
	S=S->next;		//修改栈顶指针
	delete p;		//释放原栈顶元素空间
	return true; 
}

int gettop(LinkStack S)
{
	if(S) return S->data;
	else return ERROR;
}

void printstack(LinkStack S)
{
	StackNode *p=S;
	while(p)
	{
		printf("%d\n",p->data);
		p=p->next;
	}
}

int main(){
	LinkStack s;
	if(initstack(s))
		printf("初始化栈成功\n");
	do{
		printf("   请选择要进行的操作:          \n");
		printf("   1 进栈   2 出栈   3 获得栈顶元素    4 打印   0 退出       \n");
		int select;
		scanf("%d", &select);
		if(select == 0)
			break;
		switch(select){
		case 0:
			break;
		case 1:
			int pushelem;
			printf("输入要进栈的元素:    ");
			scanf("%d", &pushelem);
			if(push(s,pushelem))
				printf("进栈成功\n");
			else
				printf("进栈失败\n");
			break;
		case 2:
			int e;
			if(pop(s,e))
				printf("元素 %d 出栈\n",e);
			else
				printf("出栈失败\n");
			break;
		case 3:
			if(gettop(s))	
				printf("栈顶元素是%d\n",gettop(s));
			else
				printf("获得栈顶元素失败\n");
			break; 
		case 4:
			printstack(s);
			break;		
		default:
			printf("你进行了误操作,请重新选择\n:");
			break;
		}
	}
	while(1);
	return OK;
}

posted @ 2022-10-08 01:20  昔痕  阅读(4)  评论(0编辑  收藏  举报