數據結構:順序隊和鏈隊(C語言)

隊的特點是先進先出。

順序隊

#include<stdio.h>

typedef int XX;
#define MAXSIZE 8

typedef struct{
	XX *base;
	int top;
	int rear;
}queue;

//入隊
void settop(queue &L,XX data){
	if((L.top+1)%MAXSIZE==L.rear){
		printf("隊滿了,無法入隊。");
		return;
	}
	L.base[L.top] = data;
	L.top++;
	L.top=L.top%(MAXSIZE);
}

//出隊
XX gettop(queue &L){
	if(L.rear==L.top){
		printf("隊內沒有元素。");
		return 0;
	}

	XX w;
	w=L.base[L.rear];
	L.rear++;
	L.rear=L.rear%MAXSIZE;
	return w;
}

//取對頭元素
XX havetop(queue L){
	if(L.rear==L.top){
		printf("隊內沒有元素。");
		return 0;
	}
	XX w=L.base[L.rear];
	return w;
}
//顯示隊內元素
void appear(queue L){
	int i;
	i=L.rear;
	printf("隊內元素爲(先進的在前面):");
	while(i!=L.top){
		printf("%d\t",L.base[i]);
		i++;
		i=i%MAXSIZE;
	}
	printf("\n");
}

void main(){
	queue L;
	L.base=new XX[MAXSIZE+1];
	L.top=L.rear=0;
	printf("*********************************************\n");
	int size;
	XX data;
	printf("請輸入您要入隊的元素的個數:\n");
	scanf("%d",&size);
	printf("請輸入入隊元素:\n");
	for(int i=0;i<size;i++){
		scanf("%d",&data);
		settop(L,data);
	}
	printf("創建成功,");
	appear(L);
	printf("-------------------------------\n");
	int ii;
	printf("1入隊\t2出隊\t3取隊頭元素\t4顯示隊內元素\n");

	printf("請輸入您想要的操作數:\n");
	scanf("%d",&ii);
	printf("-------------------------------\n");
	while(ii){
		if(ii==1){
			printf("請輸入您想入隊的元素:\n");
			scanf("%d",&data);
			settop(L,data);
			printf("\n入隊成功,入隊元素爲:%d\n\n",data);
		}
		else if(ii==2) {
			XX hh=gettop(L);
			printf("\n出隊成功,出隊元素爲:%d\n\n",hh);
		}
		else if(ii==3){
			XX hhh=havetop(L);
			printf("\n隊頭元素爲:%d\n\n",hhh);
		}
		else if(ii==4){
			printf("\n");
			appear(L);
			printf("\n");
		}
		else
			break;
		printf("-------------------------------\n");
		printf("請輸入您的下一步操作數字:\n");
		scanf("%d",&ii);
		printf("-------------------------------\n");
	}
}


鏈隊

#include<stdio.h>
#include<stdlib.h>
typedef int XX;

typedef struct Lnode{
	XX data;
	struct Lnode *next;
}Lnode,*linkLnode;

typedef struct{
	linkLnode top;
	linkLnode tail;
}queue;


//入隊
void settop(queue &L,XX data){
	Lnode *e;
	e=new Lnode;
	e->next=L.tail->next;
	L.tail->next=e;
	L.tail=e;
	e->data=data;
}

//出隊
XX gettop(queue &L){
	Lnode *e;
	XX w;
	if(L.tail==L.top){
		printf("隊內沒有元素\n");
		return 0;}
	e=L.top->next;
	L.top->next=e->next;
	w=e->data;
	free(e);
	if(L.top->next==NULL)
		L.tail=L.top;
	return w;
}

//取隊頭
XX havetop(queue L){
	if(L.top==L.tail){
		printf("隊內沒有元素\n");
		return 0;}
	return L.top->next->data;
}


//顯示隊內元素
void appear(queue L){
	if(L.tail==L.top){
		printf("隊爲空。\n");
		return;}
	Lnode *e;
	e=L.top->next;
	printf("隊內元素爲:");
	while(e!=NULL){
		printf("%d\t",e->data);
		e=e->next;
	}
	printf("\n");
}

void main(){
	queue *L;
	L=new queue;
	L->top=L->tail=new Lnode;
	L->tail->next=NULL;
	printf("************************************\n");
	int size;
	XX data;
	printf("請輸入你想入隊的元素個數:\n");
	scanf("%d",&size);
	printf("請輸入您要入隊元素個數:\n");
	for(int i=0;i<size;i++){
		scanf("%d",&data);
		settop(*L,data);}
	printf("新建成功,");
	appear(*L);
	printf("-----------------------------------\n");
	int ii;
	printf("1入隊\t2出隊\t3取隊頭元素\t4顯示隊內元素\n");
	printf("請輸入您想要的操作數:\n");
	scanf("%d",&ii);

	while(ii){
		if(ii==1){
			printf("請輸入您想入隊的元素:\n");
			scanf("%d",&data);
			settop(*L,data);
			printf("\n入隊成功\n");
		}
		else if(ii==2){
		XX ww=gettop(*L);
		printf("取出的元素爲:%d\n",ww);
		}
		else if(ii==3){
			XX www=havetop(*L);
			printf("隊頭元素爲:%d\n",www);
		}
		else if(ii==4)
			appear(*L);
		else
			break;
		printf("-----------------------------------\n");
		printf("請輸入您想要的操作數:\n");
		scanf("%d",&ii);
	}
}


發佈了41 篇原創文章 · 獲贊 19 · 訪問量 8176
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章