(数据结构)C语言实现顺序栈和链栈

参考1:https://blog.csdn.net/lpp0900320123/article/details/20615529?utm_source=blogxgwz4

参考2:https://www.cnblogs.com/yellowgg/p/6735374.html

参考3:https://blog.csdn.net/liujiuxiaoshitou/article/details/53394888

顺序栈,链栈,队列的区别;

1,顺序栈是静态的,就像数组一样,申请就申请一大块,链栈是动态的,就像链表一样,需要多少申请多少;

2,栈是先进后出,队列是先进先出;

3,链栈是后申请的节点的“屁股”对着原有的栈元素,队列是先申请的节点的“屁股”对着新申请的节点。

4,顺序栈的空间可以等元素都弹出去,然后单独用一个函数去销毁它,但是链表栈是弹出一个元素就把上面那个节点空间给释放了比较好。

顺序栈代码:

/***************************************************
##filename      : sequence.c
##author        : GYZ                               
##e-mail        : 1746902011@qq.com                 
##create time   : 2018-10-15 16:24:29
##last modified : 2018-10-16 14:33:30
##description   : NA                                
***************************************************/
#include <stdio.h>                                  
#include <stdlib.h>                                 
#include <string.h>                                 

#define SIZE  10                               
                     
typedef struct Stack
{
	int *top;
	int *bottom;
	int stacksize;
} Stack;
        
void createStack(Stack *p_st,int size)
{
	p_st->bottom = (int *)malloc(size * sizeof(int));
	if(NULL == p_st->bottom)
	{
		perror("malloc"),exit(-1);
	}
	p_st->top = p_st->bottom;
	p_st->stacksize = SIZE;
	printf("create a stack successfully!\n");
}

void pushStack(Stack *p_st,int ele)
{
	*(p_st->top) = ele;
	p_st->top ++;
	printf("push the element 'ele' : %d\n",ele);
}

void traverseStack(Stack *p_st)
{
	if(p_st->bottom == p_st->top)
	{
		printf("this is an empty stack!\n");
	}
	else
	{
		int *temp = p_st->top;
		while(temp > p_st->bottom)
		{
			temp --;
			printf("elements of stack : %d\n",*temp);
		}
	}
}

void popStack(Stack *p_st)
{
	if(p_st->top == p_st->bottom)
	{
		printf("this is an empty stack\n");
	}
	else
	{
		p_st->top--;
		printf("pop an element: %d\n",*(p_st->top));
	}
}
void destroyStack(Stack *p_st)
{
	free(p_st->bottom);
	p_st->bottom = NULL;
	p_st->top = NULL;
	p_st->stacksize = 0;
	printf("destroy stack successfully!\n");
}
                                            
int main(int argc,char *argv[])                     
{      
	Stack ST;                                             
	createStack(&ST,SIZE);
	pushStack(&ST,1);
	pushStack(&ST,2);
	pushStack(&ST,3);
	pushStack(&ST,4);
	pushStack(&ST,5);
	pushStack(&ST,6);
	pushStack(&ST,7);

    traverseStack(&ST);

	popStack(&ST);                                                
	popStack(&ST);                                                
	popStack(&ST);                                                
	popStack(&ST);                                                
	popStack(&ST);                                                
	popStack(&ST);                                                
	popStack(&ST);   
                                             
	destroyStack(&ST);
	return 0;                                   
}                                                   



                                                    
                                                    

链栈代码:

/***************************************************
##filename      : link.c
##author        : GYZ                               
##e-mail        : 1746902011@qq.com                 
##create time   : 2018-10-16 13:04:14
##last modified : 2018-10-16 15:43:49
##description   : NA                                
***************************************************/
#include <stdio.h>                                  
#include <stdlib.h>                                 
#include <string.h>                                 
                                                    
typedef struct Node
{
	int data;
	struct Node *next;
} Node;

typedef struct LinkStack
{
	Node *top;
	int count;
} LinkStack;

void initLink(LinkStack *ls)
{
	ls->top = NULL;
	ls->count = 0;
	printf("init an new link stack !\n");
}
void pushLink(LinkStack *ls,int ele)
{
	Node *temp = (Node*)malloc(sizeof(Node));
	if(NULL == temp)
	{
		perror("malloc"),exit(-1);
	}
	else
	{
		temp->data = ele;
		temp->next = ls->top;
		ls->top = temp;
		ls->count ++;
		printf("push an element : %d\n",ele);
	}
}
void traverseLink(LinkStack *ls)
{
	Node *temp = ls->top;
	while(NULL != temp)
	{
		printf("from top to bottom : %d\n",temp->data);
		temp = temp->next;
	}
}
void popLink(LinkStack *ls)
{
	if(NULL == ls->top)
	{
		printf("this is an empty!\n");
	}
	else	
	{
		Node *temp = ls->top;
		int data = temp->data;
		ls->top =  temp->next;
		printf("get the top element : %d\n",data);

		printf("destroy the top node!\n");
		free(temp);
		temp = NULL;
	}
}
                                                   
int main(int argc,char *argv[])                     
{              
	LinkStack LS;                               
	initLink(&LS);

	pushLink(&LS,1);
	pushLink(&LS,2);
	pushLink(&LS,3);
	pushLink(&LS,4);
	pushLink(&LS,5);
	pushLink(&LS,6);
	pushLink(&LS,7);

	traverseLink(&LS);

	popLink(&LS);    
	popLink(&LS);    
	popLink(&LS);    
	popLink(&LS);    
	popLink(&LS);    
	popLink(&LS);    
	popLink(&LS);    
	return 0;                                   
}                                                   
                                                    
                                                    

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值