隊列是一種特殊的線性表,特殊之處在於它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作,和棧一樣,隊列是一種操作受限制的線性表。進行插入操作的端稱爲隊尾,進行刪除操作的端稱爲隊頭。隊列的數據元素又稱爲隊列元素。在隊列中插入一個隊列元素稱爲入隊,從隊列中刪除一個隊列元素稱爲出隊。因爲隊列只允許在一端插入,在另一端刪除,所以只有最早進入隊列的元素才能最先從隊列中刪除,故隊列又稱爲先進先出(FIFO—first in first out)線性表。
順序隊列代碼實現:
#include <stdio.h>
#define MAX 10
typedef struct Queue
{
int date[MAX];
int front;
int tial;
}queue;
queue a;
void IniQueue() //初始化
{
a.tial = 0;
a.front = 0;
}
int isEmpty()
{
if(a.front == 0) //隊列爲空
{
return 1;
}
else
{
return 0;
}
}
int push_Queue(int data) //入隊
{
if(a.front == MAX-1) //隊列已滿
{
return 1;
}
else
{
a.date[a.front] = data;
}
a.front++;
return 0;
}
int pop_Queue() //出隊
{
if(!isEmpty()) //不爲空
{
a.date[a.front--];
// printf("%d\n",a.front);
}
return 0;
}
void show() //遍歷
{
int i = 0;
for(i = 0; i < a.front;i++ )
{
printf("%d\n",a.date[i]);
}
}
int main(void)
{
printf("Hello World!\n");
push_Queue(1);
push_Queue(2);
push_Queue(3);
push_Queue(4);
push_Queue(5);
push_Queue(6);
push_Queue(7);
show();
puts("==================");
pop_Queue();
show();
return 0;
}
循環隊列
#include <stdio.h>
#define MAX 1000
typedef struct Queues
{
int date[MAX];
int front;//頭
int tail;//尾
}Queue;
Queue queue;
void CreatQueue()
{
queue.front = 0;
queue.tail = 0;
}
void push_back(int vaule) //入隊
{
if((queue.tail +1)%MAX != queue.front)//隊列未滿
{
queue.tail = (queue.tail+1)%MAX;//因爲少用一個,加1,纔到尾;//%MAX是爲了提高準確度
queue.date[queue.tail] = vaule;
}
else
{
printf("full\n");
}
}
int pop_Queue() //出隊
{
if(queue.front != queue.tail)//隊列不爲空
{
int vaule = queue.date[queue.front+1];//先進先出
queue.front = (queue.front+1)%MAX;
return vaule;
}
return 0;
}
int main()
{
CreatQueue();
int i = 0;
for(i = 1;i <= 5;i++)
{
push_back(i);
printf("%d\n",queue.date[i]);
}
printf("==============\n");
for(i = 1;i <= 5;i++)
{
printf("%d\n",pop_Queue());
}
return 0;
}
棧作爲一種數據結構,是一種只能在一端進行插入和刪除操作的特殊線性表。它按照先進後出的原則存儲數據,先進入的數據被壓入棧底,最後的數據在棧頂,需要讀數據的時候從棧頂開始彈出數據(最後一個數據被第一個讀出來)。棧具有記憶作用,對棧的插入與刪除操作中,不需要改變棧底指針。
棧是允許在同一端進行插入和刪除操作的特殊線性表。允許進行插入和刪除操作的一端稱爲棧頂(top),另一端爲棧底(bottom);棧底固定,而棧頂浮動;棧中元素個數爲零時稱爲空棧。插入一般稱爲進棧(PUSH),刪除則稱爲退棧(POP)。棧也稱爲後進先出表。
#include <stdio.h>
#include <stdlib.h>
typedef struct node //節點
{
int member;
struct node* next;
}Node,*pNode;
typedef struct stack
{
pNode Top; //棧頂
pNode Buttom; //棧底
}Stack,*pStack;
void InitStack(pStack ps) //初始化棧
{
ps->Top = (pNode)malloc(sizeof(Node));
if(NULL == ps)
{
printf("malloc error\n");
exit(-1);
}
else
{
ps->Buttom = ps->Top;
ps->Top->next = NULL;
}
}
int push_stack(pStack ps,int data) //壓棧
{
pNode New = (pNode)malloc(sizeof(Node));
if(NULL == New)
{
printf("push_stack error\n");
return 0;
}
else
{
New->member = data;
New->next = ps->Top;
ps->Top = New;
return 1;
}
}
void ShowStack(pStack ps) //遍歷棧
{
pNode New = ps->Top;
while(New != ps->Buttom)
{
printf("%d\n",New->member);
New = New->next;
}
}
int Empty(pStack ps) //判斷棧是否爲空
{
if(ps->Top == ps->Buttom)
{
printf("stack is NULL\n");
return 1;
}
else
{
printf("stack no NULL\n");
return 0;
}
}
int pop_stack(pStack ps) //出棧
{
pNode swap = NULL;
int vaule = 0;
if(1 == Empty(ps))
{
exit(-1);
}
else
{
vaule = ps->Top->member;
swap = ps->Top;
ps->Top = ps->Top->next;
free(swap);
swap = NULL;
return vaule;
}
}
void clear(pStack ps) //清空棧
{
pNode cnode = NULL;
while(ps->Top != ps->Buttom) //棧不爲空
{
cnode = ps->Top;
ps->Top = ps->Top->next;
free(cnode);
cnode = NULL;
}
}
int main()
{
Stack s;
int i = 0;
int num = 0;
int data = 0;
int re_num = 0;
InitStack(&s);
printf("Do you want how num?\n");
scanf("%d",&num);
for(i = 0; i < num;i++)
{
printf("at %d num:",i+1);
scanf("%d",&data);
if(push_stack(&s,data))
{
continue;
}
else
{
printf("push_stack error\n");
exit(-1);
}
}
ShowStack(&s);
return 0;
}
編譯環境:Qt5.3.2