數據結構基礎(1) 棧與隊列基礎操作(傻瓜版)

Tips

棧和隊列就是操作受限的順序表和鏈表,不必死記具體操作,只要對棧和隊列有一個大致認識,記住相關的圖示就夠了。圖示數據結構比較直觀,看着圖,操作自然會寫。以下是一些最基本的操作,切忌死記硬背。(具體知識隨便一本數據結構書上都有)這裏循環隊列就沒介紹,個人感覺現在計算機空間都夠用,沒必要搞循環隊列。(放假前借了一本數據結構的基礎書,好多圖就直接照書,懶得畫了)

  1. 順序棧

圖示

在這裏插入圖片描述

操作

#include<stdio.h>
#include<stdlib.h>

#define MAXSIZE 200

typedef struct{
	int stack[MAXSIZE];
	int top;
}Stack,*Stack_P;

void initstack(Stack_P);
void clearstack(Stack_P);
int gettop(Stack_P p);
int isfull(Stack_P p);
int isempty(Stack_P p);
int pop(Stack_P p);
void push(Stack_P p,int);
void showstack(Stack_P);

main()
{
	int i=0;
	Stack P;
	initstack(&P);
	for(i=0;i<10;i++)
	{
		push(&P,i);
	}
	showstack(&P);
	printf("pop out %d\n",pop(&P));
	showstack(&P);
	printf("pop out %d\n",pop(&P));
	showstack(&P);
	printf("empty?%d full?%d\n",isempty(&P),isfull(&P));
 } 
 
 void initstack(Stack_P p)
 {
 	p->top=-1;
 }
 void clearstack(Stack_P p)
 {
 	p->top=-1;
 }
 int isfull(Stack_P p)
 {
 	if (p->top==MAXSIZE-1)
 	{
 		printf("Stack is full\n");
 		return 1;
	 }
	 else return 0;
 }
 int isempty(Stack_P p)
 {
 	if (p->top==-1)
 	{
 		printf("Stack is empty\n");
 		return 1;
	 }
	 else return 0;
 }
 int gettop(Stack_P p)
 {
 	if (isempty(p))
 	{
 		printf("cant get top \n");
 		exit(1);
	 }
	 return p->stack[p->top];
 }
 int pop(Stack_P p)
 {
 	if (isempty(p))
	{
 		printf("cant pop out \n");
 		exit(1);
	 }
	 return p->stack[p->top--];
	  }
void push(Stack_P p,int x)
{
	if (isfull(p))
	{
 		printf("cant push \n");
 		exit(1);
	 }
	 p->stack[++(p->top)]= x;
}
void showstack(Stack_P p)
{
	int i;
	for(i=0;i<=p->top;i++)
	{
		printf("%d ",p->stack[i]);
	}
	printf("\n");
}

2.鏈棧

圖示

在這裏插入圖片描述

操作

#include<stdio.h>
#include<stdlib.h>

typedef struct node{
	int data;
	struct node* next;
}Node,*Node_P;

typedef struct{
	Node_P top;
}Linkstack;

void initstack(Linkstack *);
void clearstack(Linkstack *);
int isempty(Linkstack*);
void push(Linkstack *,int);
int pop(Linkstack *);
int gettop(Linkstack*);
void showstack(Linkstack *);

void main()
{
	Linkstack P;
	initstack(&P);
	int i=0;
	for(i=0;i<10;i++)
	{
		push(&P,i);
	}
	showstack(&P); 
	printf("%d\n",gettop(&P));
	printf("%d\n",pop(&P));
	showstack(&P); 
	clearstack(&P);
	showstack(&P);
	
}
void initstack(Linkstack *p)
{
	p->top=NULL; 
}
void clearstack(Linkstack *p)
{
	Node_P q=p->top,temp;
	while(q)
	{
		temp=q;
		q=q->next;
		free(temp);
	}
	p->top=NULL;
}
int isempty(Linkstack* p)
{
	if (p->top==NULL) return 1;
	else return 0;
}
void push(Linkstack *p,int x)
{
	Node_P head=p->top;
	Node_P temp=(Node_P)malloc(sizeof(Node));
	if (temp==NULL)
	{
		printf("fail to allocate memory");
		exit(1);
	}
	temp->data=x;
	temp->next=p->top;
	p->top=temp;
}
int pop(Linkstack *p)
{
	if (isempty(p))
	{
		printf("cant pop");
		exit(1);
	}
	Node_P q=p->top;
	int temp=p->top->data;
	p->top=p->top->next;
	free(q);
	return temp;
}
void showstack(Linkstack * p)
{
	Node_P q=p->top;
	if (q==NULL)
	{
		printf("empty \n");
		exit(1);
	}
	while(q!=NULL)
	{
		printf("%d ",q->data);
		q=q->next;
	}
}
int gettop(Linkstack* p)
{
	if (isempty(p))
	{
		printf("empty\n");
		exit(1);
	}
	return p->top->data;
}

3.順序隊列

圖示

在這裏插入圖片描述

操作

#include<stdio.h>
#include<stdlib.h>
#define N 50
typedef struct{
	int a[N];
	int front,rear;
}Queue; 
void initqueue(Queue*p);
void clear(Queue*p);
int gettop(Queue *p);
int isempty(Queue *p);
int isfull(Queue *p);
void inqueue(Queue *p,int x);
int outqueue(Queue *p);
void showqueue(Queue *p); 

int main()
{	Queue P;
	initqueue(&P);
	printf("%d\n",P.front);
	int i;
	for(i=0;i<6;i++)
	{
		inqueue(&P,i);
		printf("%d\n",P.front);
	}
	printf("%d\n",P.front);
	showqueue(&P);
	printf("%d\n",P.front);
	printf("front:%d top:%d out:%d\n",P.front,gettop(&P),outqueue(&P));
	showqueue(&P);
	clear(&P);
	showqueue(&P);
	return 0;
}
void initqueue(Queue*p)
{
	p->front=-1;
	p->rear=-1;
}
void clear(Queue*p)
{
	p->front=-1;
	p->rear=-1;
}
int gettop(Queue *p)
{
	if(p->front==p->rear)
	{
		printf("empty queue\n");
		exit(1);
	}
	return p->a[p->front+1];
}
int isempty(Queue *p)
{
	if (p->front==p->rear) return 1;
	else return 0;
}
int isfull(Queue *p)
{
	if (p->rear==N-1) return 1;
	else return 0;
}
void inqueue(Queue *p,int x)
{
	if(isfull(p))
	{
		printf("full queue\n");
		exit(1);
	}
	p->rear++;
	p->a[p->rear]=x;
}
int outqueue(Queue *p)
{
	if(p->front==p->rear)
	{
		printf("empty queue\n");
		exit(1);
	}
	p->front++;
	return p->a[p->front];
}
void showqueue(Queue *p)
{
	if(isempty(p))
	{
		printf("empty queue\n");
		exit(1);
	}
	int i;
	for(i=p->front+1;i<=p->rear;i++)
	{
		printf("%d ",p->a[i]);
	}
	printf("\n");
}



4.鏈式隊列

圖示

在這裏插入圖片描述

操作

#include<stdio.h>
#include<stdlib.h>

typedef struct node{
	int data;
	struct node *next;
}Node;

typedef struct{
	Node *front;
	Node *rear;
}Queue; 

void initqueue(Queue*);
void clearqueue(Queue *);
int isempty(Queue *);
int gettop(Queue *);
int outqueue(Queue *);
void inqueue(Queue *,int);
void showqueue(Queue *);

int main()
{	Queue P;
	initqueue(&P);
	int i;
	for(i=0;i<6;i++)
	{
		inqueue(&P,i);
	}
	showqueue(&P);
	printf("out:%d \n",outqueue(&P));
	printf("top:%d\n",gettop(&P));
	showqueue(&P);
	clearqueue(&P);
	showqueue(&P);
	return 0;
}
void initqueue(Queue* p)
{
	p->front=NULL;
	p->rear=NULL;
}
void clearqueue(Queue* p)
{
	Node *q;
	while(p->front!=p->rear)
	{
		q=p->front;
		p->front=q->next;
		free(q);
	}
	free(p->front);
	p->front=NULL;
	p->rear=NULL; 
}
int isempty(Queue *q)
{
	if (q->front==NULL&&q->rear==NULL) return 1;
	else return 0;
}
int gettop(Queue *p)
{
	if (isempty(p))
	{
		printf("empty queue\n");
		exit(1);
	}
	return p->front->data;
}
int outqueue(Queue *p)
{	int temp;
	if (isempty(p))
	{
		printf("empty queue\n");
		exit(1);
	}
	if(p->front==p->rear)
	{   temp=p->front->data;
		p->front=NULL;
		p->rear=NULL;
		return temp;
		
	}
	else
	{
		temp=p->front->data;
		p->front=p->front->next;
		return temp;
	}
}
void inqueue(Queue *p,int x)
{
	Node *q=(Node*)malloc(sizeof(Node));
	q->data=x;
	if(p->front==NULL&&p->rear==NULL)
	{
		p->front=q;
		p->rear=q;
		q->next=NULL;
	}
	else
	{
		p->rear->next=q;
		q->next=NULL;
		p->rear=q;
	}
}
void showqueue(Queue *p)
{
	Node* q=p->front;
	if(isempty(p))
	{
		printf("empty queue\n");
		exit(1);
	}
	while(q!=NULL)
	{
		printf("%d",q->data);
		q=q->next;
	}
	printf("\n");
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章