佇列queue 棧stack入門
阿新 • • 發佈:2018-11-09
1 隊 queue 這是啊哈演算法的小方法 入門在後面
#include <stdio.h> //佇列 queue struct queue{//利用結構體 建立隊 = 陣列*1 + 頭head + 尾tail int data[100];//創造 queue隊 的資料型別 int head; int tail; }; int main(){ struct queue qu; /*產生一個 名為qu隊, */ int i,n; scanf("%d",&n); qu.head=0; qu.tail=0; for(i=0;i<n;i++){ scanf("%d",&qu.data[i]); qu.tail++; } while(qu.head<qu.tail){ printf("%d ",qu.data[qu.head]); qu.head++; qu.data[qu.tail]=qu.data[qu.head]; qu.tail++; qu.head++; } return 0; }
2 棧入門,這一段是C語言形式
#include <stdio.h>// 基本創立棧 初始化 入棧 出棧 #include <stdlib.h> #define M 10 #define OK 1 #define ERROR 0 #define TRUE 1 // 用來TRUE FALSE在入棧出棧方法中返回 #define FALSE 0 // 入出棧方法可以用bool型別代替 typedef struct{ int data[M]; int top; }Sqstack; /* 藉助結構體 typedey struct 建立結構體資料型別 Sqstack 包括 一個數組data【M】 一個棧頂top */ void InitSqstack(Sqstack *s){ s=(Sqstack *)malloc(sizeof(Sqstack));// malloc分配空間函式需要 stdlib.h標頭檔案 //初始化 分配一個順序站空間,首地址存在s中,棧頂top=-1為空 s->top=-1; } /* 初始化棧 分配空間,將棧頂置為-1 地下室層 初始化 Initialization */ /* 【】 (4層) 【 】 【】 【】 (3) 【d】 top=3 【】 top-- 【】 (2) —> 【c】 -> 【c】 top=3 【】 (1) 【b】 【b】 【】 (0層,底層) 【a】 top++ 【a】 初始 top=-1 地下層 棧可以看成一個木桶 FILO先進後出原則 abcd輸入入棧 先輸入的位於底層 隨著top++ 變化 後輸入的在頂層 出棧 top-- 先把頂層先輸出 後輸出底層 即 first in last out */ int SqstackPush(Sqstack *s,int x){ if(s->top==M-1){// 入棧先檢查棧 滿了嗎? return FALSE; }/* top++;棧頂向上移動讀入元素 入棧 */ s->top++; s->data[s->top]=x; return TRUE; } int SqstackPop(Sqstack *s,int *x){ if(s->top==-1){// 出棧檢查 空棧無可輸出內容 return FALSE; }else{ /* 賦x的值為棧頂元素,棧頂元素出棧 top-- */ *x=s->data[s->top]; s->top--; return TRUE; } } int main(){ Sqstack s1; int i=0,n,m; InitSqstack(&s1); printf("請輸入棧元素:\n"); while(i<M){ scanf("%d",&n); SqstackPush(&s1,n); i++; } /*while((scanf("%d",&n)!=EOF)){ //此處錯誤,輸入棧元素會卡住 SqstackPush(&s1,n); }*/ printf("輸出棧內元素:\n"); for(i=0;i<M;i++){ if(SqstackPop(&s1,&m)) printf("%d ",m); }//printf("\n"); return 0; } //部分程式碼參看屬豬結構 和 csdn博主_muauma_ //https://blog.csdn.net/qq1032796097/article/details/78666768
3這段程式碼用c一隻沒有寫出來,後面用c++ 結合兩個檔案完成
#include <stdio.h> #include <stdlib.h> #define M 10 typedef struct{ int data[M]; int head; int tail; }SqQueuee; void InitQueue(SqQueuee *q){ q=(SqQueuee * )malloc(sizeof(SqQueuee)); q->head=q->tail=-1; } int QueueEnter(SqQueuee *q,int x){ if(q->tail==M-1){ return 0; } q->tail++; q->data[q->tail]=x; return 1; } int QueueExit(SqQueuee *q,int *x){ if(q->head==q->tail){ return 0; } q->head++; *x=q->data[q->head]; return 1; } int main(){ SqQueuee q1; int i=0,j=0,n=0,m=0; InitQueue(&q1); printf("請輸入佇列元素:\n"); while(i<M){ scanf("%d",&n); if(QueueEnter(&q1,n)) i++; } printf("請輸出佇列元素:\n"); /*while(j<M){ if(QueueExit(&q1,&m))//有錯誤輸出不了 printf("%d ",m); j++; }*/ return 0; }
4下面是利用c++寫的 兩個檔案(1++.cpp 和 11++.cpp位於同一資料夾下)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define M 50
typedef struct{
int data[M];
int head;
int tail;
}SqQueue;/*
順序儲存佇列 包括 一個數組+頭head和+尾tail
滿足先入先出FIFO原則,讀入資料,向尾巴後面加 tail++
讀出資料 位於頭上的先出去 head++
佇列如同一個隊伍
見下行:
頭|先輸入先輸出【0】->【1】 ->【2】-> 【3】-> 【4】-> 【5】|尾
head++向右移動負責輸出 tail++向右移動將 排隊 把資料輸入
*/
void InitQueue(SqQueue *&q){
//初始化這個隊,分配一個隊空間
//首地址賦給q,將 head和tail都置為-1
q=(SqQueue *)malloc(sizeof(SqQueue));
q->head=q->tail=-1;
}
bool EnterQueue(SqQueue *&q,int e){
if(q->tail == M-1){
return false;
}//如果隊滿,達到M-1,不可輸入Entet
q->tail++;
q->data[q->tail]=e;
return true;
}
bool ExitQueue(SqQueue *&q,int &e){
if(q->head == q->tail){
return false;
}
//如果隊的頭尾在一起 說明隊中沒有資料,不能讀出
q->head++;
e=q->data[q->head];
return true;
}
#include "1++.cpp"//1++.cpp 和 11++兩個檔案必須在同一資料夾下
//1++。cpp寫了隊的基本演算法,11++.cpp負責使用
int main(){
SqQueue *q;//建立 SqQueue型別的指標
int x,n,i;
InitQueue(q);//初始化佇列
/*
讀入十個元素,讀入到隊
讀出十個隊中元素,讀出到N中
*/
for(i=0;i<10;i++){
scanf("%d",&x);
EnterQueue(q,x);
}
for(i=0;i<10;i++){
ExitQueue(q,n);
printf("%d ",n);
}
return 0;
}
5棧的C++寫法
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define M 50
using namespace std;
typedef char ElemType;
typedef struct{
ElemType data[M];
int top;
}SqStack;
void InitStack(SqStack *&s){
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s){
free(s);
}
bool StackEmpty(SqStack *s){
return (s->top == -1);
}
bool Push(SqStack *&s,ElemType e){
if(s->top == M-1){
return false;
}
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType &e){
if(s->top == -1){
return false;
}
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *&s,ElemType &e){
if(s->top == -1){
return false;
}
e=s->data[s->top];
return true;
}