寫在前面:在家玩了好久,實在是不知道干嘛了,突然想找些事做,現在是時候做些什麼了。這些東西不見得多高深,也可能很簡單,但很基礎,也無法忽視。同時,也是自己學習走過的一條路。
這是開頭,就寫寫C的隊列和棧的一些算法吧。
首先是棧的一些基礎功能的實現,先貼代碼:
#include<stdlib.h>
#include<stdio.h>
typedef int SElemType; //聲明棧元素類型為int
typedef int Status; //函數返回值的類型為int
#define MAXSIZE 20 //棧的容量
typedef struct
{
SElemType data[MAXSIZE];
int top;
}SqStock ;
Status InitStock(SqStock *); //棧初始化函數聲明
Status push(SqStock *,SElemType); //入棧函數聲明
Status pop(SqStock *,SElemType *); //出棧函數聲明
void Conversion(); //這是當時測驗的一個轉換函數,順帶貼了
/************函數區*******/
//0.初始化
Status InitStock(SqStock *s){
int StockDataArray[MAXSIZE];
for(int i=0;i<MAXSIZE;i++){
s->data[i]='\0';
}
s->top=-1;
return 1;
}
//入棧
Status push(SqStock *s,SElemType e){
if(s->top==MAXSIZE-1){ //已達最大容量
return 0;
};
s->top++;
s->data[s->top]=e;
return 1;
}
//出棧
Status pop(SqStock *s,SElemType *e){
if(s->top==-1){//已到棧底
return 0;
}
*e=s->data[s->top--];
return 1;
}
//轉化為8進制
void Conversion(){
int N;
SqStock *s;
s=(SqStock *)malloc( sizeof( SqStock)); //申請內存空間
InitStock(s);
printf("請輸入一個十進制整數:");
scanf("%d",&N);
while(N){
push(s,N%8);
N=N/8;
}
int e;
while(s->top!=-1){
pop(s,&e);
printf("%d",e);
}
}
/************函數區*******/
上述代碼思路:利用數組來實現棧的功能,平時用的時候完全直接使用數組和一個int型的top,其值為當前棧頂元素所在的下標。至於如何處理棧溢出的問題,要麼將容量設置大一點,要麼在快要溢出的時候動態申請一些內存空間。當然,肯定還有其他的方法,自己要有探索精神哦。
接著是隊列的一些基本功能的算法:
1 #include <stdio.h>
2 #include <tchar.h>
3 #include<stdlib.h>
4 #include<math.h>
5 #include<time.h>
6
7
8 #define MAXSIZE 20
9
10 typedef int QElemType;
11 typedef int Status;
12 //使用循環隊列
13 typedef struct {
14
15 QElemType Data[MAXSIZE];
16 int front;
17 int rear;
18 }XHQueue;
19
20 int getRand(int ,int );//當時測試隨機數的,兩個變量是上下限
21 void InitQueue(XHQueue *); //初始化隊列
22 Status EnQueue(XHQueue *,QElemType ); //入隊
23 Status DeQueue(XHQueue *,QElemType *);//出隊
24 Status IsEmpty(XHQueue *); //判斷是否為空
25
26 /*****函數區***/
27 //產生隨機函數
28 int getRand(int MinSize,int MMaxSize){
29 time_t t;
30 int r;
31 srand((unsigned)time(&t));
32 r=rand()%(MMaxSize-MinSize+1)+MinSize;
33 return r;
34 }
35
36
37 //隊列初始化
38 void InitQueue(XHQueue *Q){
39 Q->front=0;
40 Q->rear=0;
41
42
43 }
44
45 //入隊
46
47 Status EnQueue(XHQueue *Q,QElemType E){
48
49 if((Q->rear+1)%MAXSIZE ==Q->front)//隊列已滿
50 {
51 return 0;
52 }
53 Q->Data[Q->rear]=E;
54 Q->rear=(Q->rear+1)%MAXSIZE;
55 return 1;
56 }
57
58 //出隊
59 Status DeQueue(XHQueue *Q,QElemType *E){ //至於為什麼是*E,去看看c的傳值與傳址,在使用的時候要寫成DeQueue(Q ,&變量),傳址符不能少
60 if(Q->front==Q->rear){//隊列已空
61 return 0;
62 }
63 *E=Q->Data[Q->front];
64 Q->front=(Q->front+1)%MAXSIZE;
65
66 return 1;
67
68 }
69
70 //判斷是否為空
71 Status IsEmpty(XHQueue *Q){
72 if(Q->front==Q->rear){
73 return 1;
74 }
75 return 0;
76
77 }
78 /*****函數區***/
79
80 int _tmain(int argc, _TCHAR* argv[])
81 {
82 /*int r,r1;
83 r=getRand(20,35);
84 r1=getRand(20,40);
85 printf("%d %d\n",r,r1);*/
86
87 int Re;
88 XHQueue *Q;
89 Q=(XHQueue *)malloc(sizeof(XHQueue));
90 InitQueue(Q);
91 EnQueue(Q,2);
92 EnQueue(Q,0);
93 EnQueue(Q,1);
94 EnQueue(Q,5);
95 while(!IsEmpty(Q)){
96 DeQueue(Q,&Re);
97 printf("%d",Re);
98 }
99
100 system("pause");
101 return 0;
102 }
代碼說明:原諒我比較懶。。好多都原封不動的貼上來了= =!從思路上來說,現實生活中的隊列都是走一個,所有人往前移,但在程序中也寫成這樣,也不是不可以,但在隊列比較長的時候,光移動這些元素就會花費大量的時間,是非常不劃算的,所以,為了避免這樣大規模的移動,使用循環隊列的思想,即隊頭走了就走了,我們把隊頭指針往後移一個,隊尾來人了,發現已經站不了了,那就去前面看看,前面說不定就能站呢。
恩暫時就貼這麼多啦~改天有空再貼些其他的~