04 -- 栈

顺序栈

sqstack.h

#ifndef __LINKLIST_H__
#define __LINKLIST_H__

#include <stdio.h>
#include <stdlib.h>

#define MAX 100
typedef struct NODE{
	int data[MAX];
	int top;
}sqstack;

sqstack* stack_create();
int stack_push(sqstack* s,int value);
int stack_pop(sqstack* s);
#endif

sqstack.c

#include "sqstack.h"


sqstack* stack_create(int len)
{
	sqstack*S = NULL;
	S = (sqstack*)malloc(sizeof(sqstack));
	S->top = -1;
	return S;
}

int stack_push(sqstack* s,int value)
{
	s->top = s->top+1; 	
	s->data[s->top] = value;
	return 1;
}
int stack_pop(sqstack* s)
{
	int value = s->data[s->top];
	s->top = s->top-1;
	return value;
}

sqstack_text.c

#include "sqstack.h"

void main()
{
        sqstack *S = NULL;
        S = stack_create(10);
        stack_push(S,1);
        stack_push(S,2);
        stack_push(S,3);
        stack_push(S,4);
        stack_push(S,5);
        printf("%d\t",stack_pop(S));
        printf("%d\t",stack_pop(S));
        printf("%d\t",stack_pop(S));
        printf("%d\t",stack_pop(S));
        printf("%d\t",stack_pop(S));
}

Makefile

sqstack_text:sqstack.o sqstack_text.c
	gcc sqstack.o sqstack_text.c -o sqstack_text

sqstack.o:sqstack.c sqstack.h
	gcc sqstack.c -c -o sqstack.o

.PHONY:clean
clean:
	rm -rf sqstack.o sqstack_text

Makefile

root@ubuntu:/home/yqj/desktop/顺序栈# ./sqstack_text 
5	4	3	2	1	

链式栈

建立栈底
寻找栈顶 --> push / pop

linkstack.h

#ifndef __LINKLIST_H__
#define __LINKLIST_H__

#include <stdio.h>
#include <stdlib.h>
typedef struct NODE{
	int data;
	struct NODE*next;
}liststack;

liststack * stack_create();
void stack_push(liststack*LS,int value);
int stack_pop(liststack*LS);
#endif

linkstack.c

#include "liststack.h"

liststack * stack_create()
{
	liststack*LS = NULL;
	LS = (liststack*)malloc(sizeof(liststack));
	LS->data = 0;
	LS->next = NULL;
	return LS;
}
void stack_push(liststack*LS,int value)
{
	
	liststack*P=LS;
	liststack*Q=NULL;
	Q = (liststack*)malloc(sizeof(liststack));
	while((P->next)!=NULL) //查找末尾节点
	{
		P = P->next;
	}
	Q->data = value;
	Q->next = NULL;
	P->next = Q;
}

int stack_pop(liststack*LS)
{
	//出栈Q,不能直接free掉Q,需要断开P->next = Q
	int value;
	liststack*P=LS;
	liststack*Q=LS;
	while((P->next->next)!=NULL)//查找末尾节点
	{
		P = P->next;
	}
	Q = P->next;
	P->next = NULL;
	value = Q->data;
	
	free(Q);
	return value;
}

liststack_test.c

#include "liststack.h"

void main()
{

	liststack *LS = NULL;
	LS = stack_create();

	stack_push(LS,10);
	stack_push(LS,20);
	stack_push(LS,30);
	stack_push(LS,40);
	stack_push(LS,50);

	printf("%d\t",stack_pop(LS));
	printf("%d\t",stack_pop(LS));
	printf("%d\t",stack_pop(LS));
	printf("%d\t",stack_pop(LS));
	printf("%d\t",stack_pop(LS));
}

Makefile

liststack_text:liststack.o liststack_text.c
	gcc -g liststack.o liststack_text.c -o liststack_text

liststack.o:liststack.c liststack.h
	gcc -g liststack.c -c -o liststack.o

.PHONY:clean
clean:
	rm -rf liststack.o liststack_text

运行结果

root@ubuntu:/home/yqj/desktop/链式栈# ./liststack_text 
50	40	30	20	10	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值