當前位置:
首頁 >
数据结构快速回顾——栈
發布時間:2025/4/16
19
豆豆
生活随笔
收集整理的這篇文章主要介紹了
数据结构快速回顾——栈
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
堆棧,也可直接稱棧,是一種特殊的串行形式的數據結構,它的特殊之處在于只能允許在鏈結串行或陣列的一端進行加入資料和輸出資料的運算。另外堆棧也可以用一維陣列或連結串行的形式來完成。
1 #define STACK_INIT_SIZE 100 2 #define STACKINCREMENT 10 3 4 typedef struct 5 { int* top; 6 int* base; 7 int stacksize; 8 }SqStack; 9 10 int InitStack(SqStack &S) 11 { 12 S.base = (int*)malloc(sizeof(int)*STACK_INIT_SIZE); 13 if(!S.base)exit(1);//分配內存失敗 14 S.stacksize = STACK_INIT_SIZE; 15 S.top = S.base; 16 return 1; 17 } 18 //棧頂插入 19 int Push(SqStack &S,int e) 20 { 21 if(S.top - S.base >= S.stacksize) 22 { 23 S.base = (int*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int)); 24 if(!S.base)exit(1);//分配內存失敗 25 S.top = S.base+S.stacksize; 26 S.stacksize += STACKINCREMENT; 27 } 28 *S.top++ = e; 29 return 1; 30 } 31 int Pop(SqStack &S,int &e) 32 { 33 if(S.top == S.base) 34 return 0; 35 e = *--S.top; 36 return 1; 37 }?
迷宮求解問題實現:
1 // mazepath.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 #include "stdio.h" 6 #include "stack.h" 7 #include "malloc.h" 8 9 #define RANGE 10 //設置數組的最大長度 10 11 12 typedef int DirectType; 13 14 typedef struct { 15 int r,c; //迷宮中r行c列的位置 16 }PosType; 17 18 typedef struct { 19 int row,col; 20 char arr[RANGE][RANGE];//各位置取值' ','#','@'或'*' 其中#表示墻壁 @表示走不通的節點 *表示走過的節點 21 }MazeType; 22 23 24 typedef struct { 25 int step; //當前位置在路徑上的“序號” 26 PosType seat; //當前的坐標位置 27 DirectType di; //往下一坐標位置的方向 28 }ElemType; //棧的元素類型 29 30 31 typedef struct NodeType { 32 ElemType data; 33 struct NodeType *next; 34 }NodeType,*LinkType; //結點類型,指針類型 35 36 typedef struct { 37 LinkType top; 38 int size; 39 }Stack; //棧類型 40 41 42 void Initialization(){ 43 printf("CreatMaze-c\n"); 44 printf("MazePsth-c\n"); 45 printf("PrintMaze-c\n"); 46 } 47 48 void MakeNode(LinkType &p,ElemType e){ 49 p=(LinkType)malloc(sizeof(NodeType)); 50 p->data.step=e.step; 51 p->data.seat.r=e.seat.r; 52 p->data.seat.c=e.seat.c; 53 p->data.di=e.di; 54 } 55 //標記走過的節點 56 void FootPrint(MazeType &Maze,PosType curpos){ 57 Maze.arr[curpos.r][curpos.c]='*'; 58 } 59 60 int Pass(MazeType &Maze,PosType curpos){ 61 if(Maze.arr[curpos.r][curpos.c]=='#'||Maze.arr[curpos.r][curpos.c]=='@') 62 return 0; 63 else return 1; 64 } 65 66 67 68 void CreateMaze(MazeType &Maze){ 69 int i,j; 70 printf("產生迷宮:輸入迷宮的大小\n"); 71 printf("行:\n"); 72 scanf("%d",&Maze.row); 73 printf("列:\n"); 74 scanf("%d",&Maze.col); 75 //給迷宮四周加障礙 76 for(i=0;i<=Maze.row+1;i++) 77 for(j=0;j<=Maze.col+1;j++) 78 if(i==0||i==Maze.row+1)Maze.arr[i][j]='#'; 79 else if(j==0||j==Maze.col+1)Maze.arr[i][j]='#'; 80 else Maze.arr[i][j]=' '; 81 //接收障礙位置 82 printf("如果設置障礙結束,請輸入“q”,否則輸入障礙所在行\n"); 83 scanf("%d",&i); 84 while(i!='q'){ 85 printf("輸入障礙所在列\n"); 86 scanf("%d",&j); 87 Maze.arr[i][j]='#'; 88 printf("如果設置障礙結束,請輸入“q”,否則輸入障礙所在行\n"); 89 scanf("%d",&i); 90 } 91 } 92 //標記四個方向都走不通的節點 93 void MakePrint(MazeType &Maze,PosType curpos){ 94 Maze.arr[curpos.r][curpos.c]='@'; 95 } 96 97 98 void NextPos(PosType &curpos,DirectType di){ 99 switch(di){ 100 case 1:curpos.c++;break; 101 case 2:curpos.r++;break; 102 case 3:curpos.c--;break; 103 case 4:curpos.r--;break; 104 } 105 } 106 107 108 109 void PrintMaze(MazeType Maze){ 110 int i,j; 111 for(i=1;i<=Maze.row;i++){ 112 for(j=1;j<=Maze.col;j++){ 113 printf("%c",Maze.arr[i][j]); 114 } 115 printf("\n"); 116 } 117 118 119 120 int MazePath(MazeType Maze,PosType start,PosType end){ 121 PosType curpos; 122 ElemTpye e; 123 int curstep=1,found=0; 124 InitStack(S); 125 curpos=start; 126 do{ 127 if(Pass(Maze,curpos)){ 128 FootPrint(Maze,curpos); 129 e.step=curstep; 130 e.seat=curpos; 131 e.di=1; 132 Push(S,e); 133 if(curpos==end)found=1; 134 else { 135 curpos=NextPos(curpos,1); 136 curstep++: 137 }//else 138 }//if 139 else 140 if (!StackEmpty(S)){ 141 Pop(S,e); 142 while(e.di==4&&!StackEmpty(S)){ 143 MarkPrint(Maze,e.seat); 144 Pop(S,e); 145 curstep--; 146 }//while 147 if(e.di<4){ 148 e.di++; 149 Push(S,e); 150 curpos=NextPos(e.seat,e.di); 151 }//if 152 }//if 153 }while(!StackEmpty(S)&&!found); 154 return found; 155 }//Mazepath?
?
總結
以上是生活随笔為你收集整理的数据结构快速回顾——栈的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle 11gR2学习之三(创建用
- 下一篇: Python 自带IDLE中调试程序