数据结构基础(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");
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值