數(shù)組下標),棧頂指針存放棧頂元素,初始化時棧頂為-1。下面給出順序棧的算法。
#include<stdio.h>#include<stdlib.h>#define maxsize 10typedef struct stacknode * stack;typedef int elementype; //方便一處改,處處改struct stacknode{ elementype *data; elementype top;};stack Creatstack() //初始化{ stack S=(stack)malloc(sizeof(struct stacknode)); S->data=(elementype*)malloc(maxsize*sizeof(elementype)); S->top=-1; return S;}int Iffull(stack S){ if(S->top==maxsize-1) return 1; else return 0;}int Ifempty(stack S){ if(S->top==-1) return 1; else return 0;}int Push(stack S,elementype x){ if(Iffull(S)) return 0; else { S->data[++S->top]=x; return 1; }}elementype Pop(stack S) { if(Ifempty(S)) return 0; else return(S->data[S->top--]);}int main(){ int i; elementype a; stack S1=Creatstack(); for(i=0;i<maxsize;i++) { scanf("%d",&a); Push(S1,a); } for(i=0;i<maxsize;i++) PRintf("%d ",Pop(S1)); printf("/n"); return 0;}這是個簡單的入棧和出棧的小算法,上面有些地方用int,有些地方用elementype,在這里是一樣的,但是用elementype是為了和結(jié)構(gòu)體里面數(shù)組元素保持一致,做到一處改動可以處處改動。鏈棧:鏈棧與順序棧不同,沒有長度限制(除了申請空間失敗),又由于入棧和出棧順序不同,在鏈表的處理上要做好。棧有一個棧頂指針,在我的程序里棧頂指針不指向數(shù)據(jù),下面給出一些基本的算法。
#include<stdio.h>#include<stdlib.h>typedef struct snode * stack;typedef int elementype;struct snode{ elementype data; stack next;};stack Creatstack(){ stack S; S=(stack)malloc(sizeof(struct snode)); S->next=NULL; return S;}int Ifempty(stack S) //S是棧頂指針{ return S->next==NULL;}int Push(stack S,elementype x){ stack temp; temp=(stack)malloc(sizeof(struct snode)); if(!temp) return 0; else { temp->data=x; temp->next=S->next; S->next=temp; return 1; }}elementype Pop(stack S){ stack T; elementype m; T=S->next; m=T->data; S->next=T->next; free(T); return m;}int main(){ stack S1; int i,k; S1=Creatstack(); for(i=0;i<5;i++) { scanf("%d",&k); Push(S1,k); } for(i=0;i<5;i++) printf("%d ",Pop(S1)); printf("/n"); return 0;}
3.隊列順序隊列/循環(huán)隊列:順序隊列與循環(huán)隊列基本一致,都是利用數(shù)組存放數(shù)據(jù),但是循環(huán)隊列在利用空間上要更高效一些。順序隊列用一個向量空間(一般使用一維數(shù)組)來存放當前隊列中的元素。由于隊列的隊頭和隊尾的位置是變化的,設(shè)置兩個指針front和rear分別指示隊頭元素和隊尾元素在向量空間中的位置,它們的初值在隊列初始化時均應(yīng)置為0。二者判斷隊空隊滿條件:順序隊列中q.front = q.rear 表示隊空,q.rear = maxsize表示隊滿.而在循環(huán)隊列中q.front=q.rear表示隊空,而無法用q.rear=maxsize表示隊滿.(有兩種方法,一種是設(shè)置標志位,另外一種是放棄一個空間,隊尾指針不指向任何元素,用這兩種狀態(tài)把隊空隊滿區(qū)分開。第二種較為常用。下面給出循環(huán)隊列的常見算法及c代碼。
#include<stdio.h>#include<stdlib.h>#define maxsize 8 //隊列最大容量,但是對于循環(huán)隊列可以存放的元素個數(shù)為maxsize-1個typedef int elementype;typedef struct qnode * Queue;struct qnode{ elementype * data; //存放數(shù)據(jù)的數(shù)組,這里用指針形式,data[maxsize]也可 int front,rear;};Queue Create(){ Queue Q; Q=(Queue)malloc(sizeof(struct qnode)); Q->data=(elementype *)malloc(maxsize*sizeof(elementype)); Q->front=Q->rear=0; return Q;}int ifempty(Queue Q){ return Q->front==Q->rear;}int iffull(Queue Q){ return (Q->rear+1)%maxsize==Q->front;}int Insert(Queue Q,elementype x){ if(iffull(Q)) return 0; else { Q->data[Q->rear]=x; Q->rear=(Q->rear+1)%maxsize; return 1; }}elementype Delete(Queue Q){ int m; if(ifempty(Q)) return 0; else { m=Q->data[Q->front]; Q->front=(Q->front+1)%maxsize; return m; }}int length(Queue Q){ return (Q->rear-Q->front+maxsize)%maxsize; //求隊列元素個數(shù),也就是長度}int main(){ Queue Q1; int i,k; Q1=Create(); for(i=0;i<maxsize-1;i++) { scanf("%d",&k); Insert(Q1,k); } printf("%d個元素/n",length(Q1)); for(i=0;i<maxsize-1;i++) printf("%d ",Delete(Q1)); printf("/n"); return 0;}在這個程序里,先入隊,然后出隊,int型的函數(shù)也可以用bool型,返回true or false,來表明是否入隊成功或者是否隊空或隊滿,(不支持bool的可以用define定義)bool型其實就是一種特殊的整型。
鏈隊列:鏈隊列就是用一個線性鏈表來表示一個隊列,隊列中每個元素對應(yīng)鏈表中一個鏈
接點,隊頭指針front指向線性鏈表的第1個鏈接點,隊尾指針rear指向鏈表的最后一個鏈
接點,與順序存儲結(jié)構(gòu)的隊列不同的是,隊頭指針和隊尾指針都是指向?qū)嶋H隊頭元素和隊尾元素所在的鏈接點。測試鏈接隊列為空的條件是front為NULL。在判斷隊空時1.假設(shè)不帶頭結(jié)點,隊頭隊尾指針分別為f和r,則f和r都指向真正的節(jié)點,那么f==r表示f和r指向同一個節(jié)點,這時,隊列中只有一個節(jié)點,只有f==r==null時表示隊列為空。2. 假設(shè)隊列有頭節(jié)點,所以f不可能為null(若f==null,則這個隊列在內(nèi)存中丟失),f始終指向頭結(jié)點,因此當r也指向頭結(jié)點時表示隊列為空。在我的代碼中取無頭節(jié)點的情況。
#include<stdio.h>#include<stdlib.h>#define maxsize 6 //自己定義,也可不定義typedef struct queue * Queue;typedef struct queuenode * Ptrtnode;typedef int elementype;struct queue{ elementype data; Queue next;};struct queuenode{ Queue front; Queue rear;};void Create(Ptrtnode Q) //初始化鏈隊列{ Q->front=Q->rear=NULL;}int Insert(Ptrtnode Q,elementype x) { Queue temp=(Queue)malloc(sizeof(struct queue)); if(temp==NULL) //申請空間失敗 return 0; if(Q->rear==NULL) //如果隊列為空 Q->front=Q->rear=temp; else { temp->data=x; Q->rear->next=temp; Q->rear=temp; } return 1;}elementype Delete(Ptrtnode Q){ int m; Queue p; if(Q->front==NULL) return 0; else { m=Q->front->data; p=Q->front; Q->front=p->next; if(Q->front==NULL) { Q->rear=NULL; } free(p); return m; }}int main(){ int i,k; Queue Q1; Ptrtnode S; S=(Ptrtnode)malloc(sizeof(struct queuenode)); Create(S); Q1=(Queue)malloc(sizeof(struct queue)); scanf("%d",&k); Q1->data=k; Q1->next=NULL; S->front=Q1; S->rear=Q1; for(i=1;i<maxsize;i++) { scanf("%d",&k); Insert(S,k); } for(i=0;i<maxsize;i++) printf("%d ",Delete(S)); printf("/n"); return 0;}
新聞熱點
疑難解答