☆ C/C++中链栈与链队

从单链表入手分析就可以看到链队与链栈的本质,他俩其实是单链表的特殊形式:

链栈:指定尾部插入尾部删除的单链表

链队:指定尾部插入头部删除的单链表

明白了这两个道理之后,就可以完全使用单链表流畅的写出链栈与链队了~

(✪ω✪)

嗯?好吧好吧,再给出一个传送门吧---

--->>>点我即达单链表教程篇(^_−)☆

 

在我的前几篇文章中也写到了栈---

--->>>点我即达链栈(^_−)☆

--->>>点我即达顺序栈(^_−)☆

 

****************************************************************************************************************************************

 

★直入主题,直接附上源代码:

——(完全的链表版)

1:链队:

#include <iostream>
#include <windows.h>
#include <cstring>
#include <malloc.h>
#define N 3
using namespace std;

typedef struct node
{
	int data;
	struct node * link;
}LinkStack;

LinkStack * InitLinkStack()
{
	LinkStack * h;
	if(!(h=(LinkStack*)malloc(sizeof(LinkStack))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	h->link = NULL;
	return h;
}

void PushStack(LinkStack *h,int number)    //入栈
{
	LinkStack *p,*s;
	p = h;
	if(!(s=(LinkStack*)malloc(sizeof(LinkStack))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	s->data = number;
	s->link = NULL;
	p->link = s;
}

void PopStack(LinkStack *h)
{
	int temp_number = 0;
	if(h->link == NULL)
	{
		cout << "LinkStack is Blank!" << endl;
		system("pause");
		cout << "Error:1" << endl;
	}
	LinkStack *p,*temp;
	p = h;
	if(!(temp=(LinkStack*)malloc(sizeof(LinkStack))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	while(p->link->link)
	{
		p = p->link;
	}
	temp = p->link;
	temp_number = p->link->data;
	p->link = p->link->link;
	free(temp);
	cout << "Pop " << temp_number << " Successful!";
}

void TraverseStack(LinkStack *h)
{
	LinkStack *p;
	p = h;
	while(p->link)
	{
		cout << p->link->data << endl;
		p = p->link;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	LinkStack *h,*p;
	h = InitLinkStack();
	p = h;
	int number;
	cout << "☆Push Stack: " << endl;
	cout << "Please enter the data(3个): " << endl;
	for(int i=0;i<N;i++)
	{
		cin >> number;
		PushStack(p,number);
		p = p->link;
	}
	cout << endl;
	cout << "☆PopStack: "<< endl;
	PopStack(h);
	cout << endl << endl;
	cout << "☆TraverseStack" << endl;
	TraverseStack(h);
	cout << endl;
	return 0;
}

 

2:链栈:  

#include <iostream>
#include <windows.h>
#include <cstring>
#include <malloc.h>
#define N 3
using namespace std;

typedef struct node
{
	int data;
	struct node * link;
}LinkQueue;

LinkQueue * InitLinkQueue()
{
	LinkQueue *h;
	if(!(h=(LinkQueue*)malloc(sizeof(LinkQueue))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	h->link = NULL;
	return h;
}

void PushQueue(LinkQueue *h,int number)    //入栈
{
	LinkQueue *p,*s;
	p = h;
	if(!(s=(LinkQueue*)malloc(sizeof(LinkQueue))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	s->data = number;
	s->link = NULL;
	p->link = s;
}

void PopQueue(LinkQueue *h)
{
	int temp_number = 0;
	if(h->link == NULL)
	{
		cout << "LinkQueue is Blank!" << endl;
		system("pause");
		cout << "Error:1" << endl;
	}
	LinkQueue *p,*temp;
	p = h;
	if(!(temp=(LinkQueue*)malloc(sizeof(LinkQueue))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	temp = p->link;
	temp_number = p->link->data;
	p->link = p->link->link;
	free(temp);
	cout << "Pop " << temp_number << " Successful!";
}

void TraverseQueue(LinkQueue *h)
{
	LinkQueue *p;
	p = h;
	while(p->link)
	{
		cout << p->link->data << endl;
		p = p->link;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	LinkQueue *h,*p;
	h = InitLinkQueue();
	p = h;
	int number;
	cout << "☆Push Queue: " << endl;
	cout << "Please enter the data(3个): " << endl;
	for(int i=0;i<N;i++)
	{
		cin >> number;
		PushQueue(p,number);
		p = p->link;
	}
	cout << endl;
	cout << "☆PopQueue: "<< endl;
	PopQueue(h);
	cout << endl << endl;
	cout << "☆TraverseQueue" << endl;
	TraverseQueue(h);
	cout << endl;
	return 0;
}

 

 

 

 

☆仅仅记录日常编写代码 与 疑问(`・ω・´)

 

****************************************************************************************************************************************

 

             最快的脚步不是跨越,而是继续,最慢的步伐不是小步,而是徘徊。

 

****************************************************************************************************************************************

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值