3.20周记:栈和队列
生活随笔
收集整理的這篇文章主要介紹了
3.20周记:栈和队列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
棧
-棧的定義
????????棧也稱堆棧,是一種先進后出,刪除和插入都在棧頂操作的線性表。
-棧的特性
????????先進后出,后進先出。基本操作只有兩個:出棧(push)和進棧(pop)
-基本運算
????????初始化棧、判斷空、入棧、出棧、讀棧頂元素。
-棧種類
????????棧分為順序棧與鏈式棧兩種:順序棧類似線性表,是數組的形式;而鏈式棧類似鏈表,用指針鏈接每個部分。
-順序棧
#include <stdio.h> #include <stdlib.h> #define MAX 1024 //定義棧能容納的大小 typedef int DataType;//存儲數據類型為int //順序棧儲存結構 typedef struct {DataType data[MAX];int top; } Stack;//定義全局變量 Stack *s;//初始化棧 Stack * init() {Stack * s;//申請指定空間的棧s = (Stack *)malloc(sizeof(Stack));s->top = -1;return s; }//判斷空 int empty(Stack *s){//如果為空返回1,否則返回0if (s->top == -1) return 1;else return 0; }//入棧 int push(Stack *s,DataType x){if (s->top == MAX-1){printf("棧將溢出!");return 0;}s->top++;s->data[s->top] = x;return 1; }//出棧 void pop(Stack *s){if (empty(s)){printf("棧已空!\n");}else{s->top--;} } //讀棧頂元素 void readtop(Stack *s){if (!empty(s)){printf("%d\n", s->data[s->top]);}else{printf("棧已空!");return;} }int main(){s = init();push(s, 1);//1入棧 push(s, 2);//2入棧 push(s, 3);//3入棧 readtop(s);//讀取棧頂數據->3pop(s);//3出棧 readtop(s);//讀取棧頂數據->2pop(s);//2出棧readtop(s);//讀取棧頂數據->1pop(s);//1出棧 readtop(s);//此時棧已空,跳出提示詞 pop(s);//此時棧已空,跳出提示詞 }運行結果:
?-鏈式棧
#include <stdio.h> #include <stdlib.h> typedef int DataType;//存儲數據類型為int //定義棧結構的結點 typedef struct stack_node {DataType element;struct stack_node *next; } node;typedef struct {node *top; } stack;//進棧函數 void push(stack *sp, DataType element) {node *np;//創建新節點np = (node *)malloc(sizeof(node));np->element = element;//修改棧頂指針np->next = sp->top;sp->top = np; }//出棧函數 DataType pop(stack *sp) {node *np;//將要出棧的結點指針np = sp->top;DataType c;//棧頂元素值c = np->element;sp->top = np->next;free(np);np = NULL;return c; }//遍歷輸出函數 void read (stack sp) {stack temstack = {NULL};DataType tem;while (sp.top != NULL) {tem = pop(&sp);printf("%d\n", tem);push(&temstack, tem);}while (temstack.top != NULL) {tem = pop(&temstack);push(&sp, tem);} } int main() {stack s = {NULL};push(&s, 1);push(&s, 2);push(&s, 3);read(s);return 0; }運行結果:
隊列
????????隊列基本操作與棧類似,區別是刪除元素時根據先進先出原則。
-隊列的特性
?????????先進先出,后進后出。允許進行插入操作的一端稱為隊尾(rear),允許進行刪除操作的一段稱為隊頭(front)。
-代碼
#include <stdio.h> #include <stdlib.h> #define MAX 50//隊列類型的定義 typedef struct {ElemType data[MAX];int front,rear;//front為隊首指針 rear為隊尾指針 }SqQueue; typedef int ElemType; typedef SqQueue CSqQueue;//初始化運算 /*初始化運算得到一個空隊列*/ int InitQueue(CSqQueue *Q) {(*Q).front=0;(*Q).rear=0;return 0; } //判空運算,隊列Q為空返回1,否則返回0 int QueueEmpty(CSqQueue Q) {if(Q.front==Q.rear)return 1;return 0; }//判滿運算 int QueueFull(CSqQueue Q) {if((Q.rear+1)%MAX==q.front)return 1;return 0; }//創建一個隊列,創建成功返回1,創建失敗返回0 int CreatQueue(CSqQueue *Q) {int i,n;ElemType temp_e;printf("請輸入你想要創建的隊列的長度:\n");scanf("%d",&n);if(n>MAX)return -1;for(i=1;i<=n,i++){printf("請輸入第%d個元素:\n",i); scanf("%d",&temp_e);EnQueue(Q,temp_e); }return 0; }//返回隊列長度 int QueueLength(CSqQueue Q) {return (Q.rear-Q.front+MAX)%MAX; }//返回隊首元素的值 int GetHead(CSQqueue Q,ElemType *e) {if(QueueEmpty(Q)){return-1;}*e=Q.data[Q.front];return 0; }//入隊的實現-隊尾插入新元素 int EnQueue(CSqQueue *Q,ElemType *e) {if(QueueEmpty(*Q)){return-1;}(*Q).data[(*Q).rear]=e;(*Q).rear=((*Q).rear+1)%MAX;return 0; }//出隊的實現-刪除隊首,并用變量e返回被刪除元素 int DeQueue(CSqQueue *Q,ElemType *e) {if(QueueEmpty(*Q)){return-1;}e=(*Q).data[(*Q).front)];(*Q).front=((*Q).front+1)%MAX;return 0; }//打印運算的實現-輸出隊列Q void ShowQueue(CSqQueue Q) {ElemType temp_e;while (!=QueueEmpty(Q)){DeQueue(&Q,&temp_e);printf("%d",temp_e)}printf("\n"); }總結
以上是生活随笔為你收集整理的3.20周记:栈和队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哈希表(散列表)的介绍,代码实现
- 下一篇: 第一篇博客——用来写自己