資料結構——入棧,出棧,佇列相關操作(C語言實現)
阿新 • • 發佈:2018-12-20
閱讀過程之中可能會花費比較多的時間:建議直接翻到最後,有完整的程式碼可以使用
程式準備工作
#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include<process.h> #define MaxSize 100 //最大元素個數 typedef int ElemType; typedef struct //順序棧的型別定義 { ElemType data[MaxSize]; //棧元素儲存空間 int top; //棧頂指標 }SeqStack; typedef struct //迴圈順序隊的型別定義 { ElemType data[MaxSize]; //佇列元素儲存空間 int front; //隊頭指標 int rear; //隊尾指標 }CircSeqQueue;
先準備工作看看程式執行的結果
棧表具有先進後出,後進後出的功能:
下面看看功能的實現
下面看看入棧,出棧,讀取棧頂元素,棧置空的函式的實現
void StackInitial(SeqStack *pS) //建立一個由指標pS所指向的空棧 { pS->top= -1; } int isEmpty(SeqStack *pS) //順序棧為空時返回1,否則返回。 { return pS->top== -1; } int isFull (SeqStack * pS) //棧為滿時返回1,否則返回0 { return pS->top >=MaxSize-1; } void Push(SeqStack *pS, ElemType e) //若棧不滿,則元素e進棧 { if(pS->top==MaxSize-1) { printf("棧滿\n"); return ; } else { pS->top++; pS->data[pS->top]=e; return ; } } ElemType Pop(SeqStack *pS) //若棧不為空,則刪除棧頂元素,並返回它的值 { if(isEmpty(pS)) return -1; else{ return pS->data[pS->top--]; } } ElemType GetTop(SeqStack *pS) //若棧不為空,則返回棧頂元素的值 { if(isEmpty(pS)) return -1; else return pS->data[pS->top]; } void MakeEmpty(SeqStack *pS) //將由指標pS所指向的棧變為空棧 { return pS->top= -1; }
佇列操作,這次的佇列我用的是迴圈佇列,佇列的主要功能是先進先出,後進後出,也就可以類比排隊
int IsEmpty(CircSeqQueue *pQ) //迴圈順序佇列為空時返回1,否則返回0 { return pQ->front==pQ->rear; } int IsFull(CircSeqQueue *pQ) //迴圈佇列為滿時返回1,否則返回。 { return(pQ->rear+1) %MaxSize==pQ->front; } void EnQueue(CircSeqQueue *pQ,ElemType e) { //若佇列不滿,則元素e進隊 if((pQ->rear+1)%MaxSize==pQ->front) { printf("隊滿!\n"); return ; } else{ pQ->data[pQ->rear]=e; pQ->rear=(pQ->rear+1)%MaxSize; return ; } } ElemType DeQueue(CircSeqQueue *pQ) { //若迴圈佇列不為空,則刪除隊頭元素,並返回它的值 if(pQ->rear==pQ->front) { printf("空隊!\n"); return 0; } else{ return pQ->data[pQ->front++]; } } ElemType GetFront(CircSeqQueue *pQ) //若佇列不為空,則返回隊頭元素值 { if (IsEmpty(pQ)) { printf("空佇列! \n"); exit(1); } return pQ->data[(pQ->front)%MaxSize]; } void MakeEmpty2(CircSeqQueue *pQ) //將指標pQ所指向的佇列變為空 { pQ->front=pQ->rear=0; }
複製進C編輯器即可使用
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include<process.h>
#define MaxSize 100 //最大元素個數
typedef int ElemType;
typedef struct //順序棧的型別定義
{
ElemType data[MaxSize]; //棧元素儲存空間
int top; //棧頂指標
}SeqStack;
typedef struct //迴圈順序隊的型別定義
{
ElemType data[MaxSize]; //佇列元素儲存空間
int front; //隊頭指標
int rear; //隊尾指標
}CircSeqQueue;
void StackInitial(SeqStack *pS) //建立一個由指標pS所指向的空棧
{
pS->top= -1;
}
int isEmpty(SeqStack *pS) //順序棧為空時返回1,否則返回。
{
return pS->top== -1;
}
int isFull (SeqStack * pS) //棧為滿時返回1,否則返回0
{
return pS->top >=MaxSize-1;
}
void Push(SeqStack *pS, ElemType e) //若棧不滿,則元素e進棧
{
if(pS->top==MaxSize-1)
{
printf("棧滿\n");
return ;
}
else {
pS->top++;
pS->data[pS->top]=e;
return ;
}
}
ElemType Pop(SeqStack *pS) //若棧不為空,則刪除棧頂元素,並返回它的值
{
if(isEmpty(pS))
return -1;
else{
return pS->data[pS->top--];
}
}
ElemType GetTop(SeqStack *pS) //若棧不為空,則返回棧頂元素的值
{
if(isEmpty(pS))
return -1;
else
return pS->data[pS->top];
}
void MakeEmpty(SeqStack *pS) //將由指標pS所指向的棧變為空棧
{
return pS->top= -1;
}
void QueueInitial(CircSeqQueue *pQ)
{
//建立一個由指標PQ所指向的空順序迴圈佇列
pQ->front=pQ->rear=0;
}
int IsEmpty(CircSeqQueue *pQ) //迴圈順序佇列為空時返回1,否則返回0
{
return pQ->front==pQ->rear;
}
int IsFull(CircSeqQueue *pQ) //迴圈佇列為滿時返回1,否則返回。
{
return(pQ->rear+1) %MaxSize==pQ->front;
}
void EnQueue(CircSeqQueue *pQ,ElemType e)
{
//若佇列不滿,則元素e進隊
if((pQ->rear+1)%MaxSize==pQ->front)
{
printf("隊滿!\n");
return ;
}
else{
pQ->data[pQ->rear]=e;
pQ->rear=(pQ->rear+1)%MaxSize;
return ;
}
}
ElemType DeQueue(CircSeqQueue *pQ)
{
//若迴圈佇列不為空,則刪除隊頭元素,並返回它的值
if(pQ->rear==pQ->front)
{
printf("空隊!\n");
return 0;
}
else{
return pQ->data[pQ->front++];
}
}
ElemType GetFront(CircSeqQueue *pQ) //若佇列不為空,則返回隊頭元素值
{
if (IsEmpty(pQ))
{
printf("空佇列! \n");
exit(1);
}
return pQ->data[(pQ->front)%MaxSize];
}
void MakeEmpty2(CircSeqQueue *pQ) //將指標pQ所指向的佇列變為空
{
pQ->front=pQ->rear=0;
}
void output()
{
int i;
for (i=0;i<10; i++)
printf(" ");
for (i=0; i< 32; i++)
printf("*");
printf("\n");
}
void main1()
{
int i;
output();
for (i=0; i< 10; i++)printf(" ");
printf("* ");
printf("1.入棧一個元素");
for (i=0; i< 16; i++) printf(" ");
printf("*");
printf("\n");
for (i=0; i< 10; i++)printf(" ");
printf("* ");
printf("2.出棧一個元素");
for (i=0; i< 16; i++) printf(" ");
printf("*");
printf("\n");
for (i=0; i< 10; i++)printf(" ");
printf("* ");
printf("3.讀棧頂元素");
for (i=0; i< 16; i++) printf(" ");
printf("*");
printf("\n");
for (i=0; i< 10; i++)printf(" ");
printf("* ");
printf("4.置棧空");
for (i=0; i< 16; i++) printf(" ");
printf("*");
printf("\n");
for (i=0; i< 10; i++)printf(" ");
printf("* ");
printf("5.入隊一個元素");
for (i=0; i< 16; i++) printf(" ");
printf("*");
printf("\n");
for (i=0; i< 10; i++)printf(" ");
printf("* ");
printf("6.出隊一個元素");
for (i=0; i< 16; i++) printf(" ");
printf("*");
printf("\n");
for (i=0; i< 10; i++)printf(" ");
printf("* ");
printf("7.讀隊首元素");
for (i=0; i< 14; i++) printf(" ");
printf("*");
printf("\n");
for (i=0; i< 10; i++)printf(" ");
printf("* ");
printf("8.置隊空");
for (i=0; i< 18; i++) printf(" ");
printf("*");
printf("\n");
for (i=0; i< 10; i++)printf(" ");
printf("* ");
printf("0.退 出");
for (i=0; i< 8; i++) printf(" ");
printf("*");
printf("\n");
output();
}
void main () //主函式
{
SeqStack *pS;
CircSeqQueue*pQ;
ElemType e;
int k=1,m,i,n,c,d;
pS=(SeqStack *)malloc(sizeof(SeqStack));
StackInitial(pS);
pQ=(CircSeqQueue *)malloc(sizeof(CircSeqQueue));
QueueInitial(pQ);
main1();
while (k)
{
printf("請選擇0一8:");
scanf ("%d", &m);
switch (m)
{
case 0:
return;
case 1:
{
printf("輸入數n,再輸入n個元素,入棧:");
scanf ("%d", &n);
for (i=0;i<n;i++)
{
scanf ("%d", &e);
Push(pS,e);
}
break;
}
case 2:
{
e=Pop(pS);
printf("%d\n", e);
printf("\n");
break;
}
case 3:
{
printf ("取出棧頂元素: ");
e=GetTop(pS);
printf("%d\n", e);
printf ("\n");
break;
}
case 4:
{
printf("置棧空:\n");
MakeEmpty(pS);
printf ("\n");
break;
}
case 5:
{
printf("輸入入隊元素個數:");
scanf ("%d", &c);
printf("輸入入隊元素:\n");
for (i=0;i<c;i++)
{
scanf ("%d", &e);
EnQueue(pQ,e);
}
break;
}
case 6:
{
printf("請輸入出隊元素的個數:");
scanf ("%d", &c);
for (i=0;i<c;i++)
{
e=DeQueue (pQ);
printf("%d\n", e);
}
break;
}
case 7:
{
printf ("取出隊首元素:\n");
e=GetFront(pQ);
printf ("%d\n",e);
break;
}
case 8:
{
printf("置隊空:\n");
MakeEmpty2(pQ);
printf ("\n");
break;
}
default:
return;
}
printf ("繼續執行嗎Y /N");
scanf("%d",&k);
if (k=='n'||k=='N') return;
fflush(stdin);
main1();
}
}