从单链表入手分析就可以看到链队与链栈的本质,他俩其实是单链表的特殊形式:
链栈:指定尾部插入尾部删除的单链表
链队:指定尾部插入头部删除的单链表
明白了这两个道理之后,就可以完全使用单链表流畅的写出链栈与链队了~
(✪ω✪)
嗯?好吧好吧,再给出一个传送门吧---
--->>>点我即达单链表教程篇(^_−)☆
在我的前几篇文章中也写到了栈---
--->>>点我即达链栈(^_−)☆
--->>>点我即达顺序栈(^_−)☆
****************************************************************************************************************************************
★直入主题,直接附上源代码:
——(完全的链表版)
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;
}
☆仅仅记录日常编写代码 与 疑问(`・ω・´)
****************************************************************************************************************************************
最快的脚步不是跨越,而是继续,最慢的步伐不是小步,而是徘徊。
****************************************************************************************************************************************