数据结构(C语言)--栈和队列

本文详细介绍了使用C语言实现的四种基本数据结构:顺序栈、链栈、链队列和循环队列。每种结构都提供了核心操作的代码示例,如创建、入栈/入队、出栈/出队等,帮助读者深入理解这些数据结构的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于C语言实现的栈与队列

1.顺序栈

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define True 1
#define False 0


#define STACK_INT_SIZE 10  /*存储空间初始分配量*/
#define STACKINCREMENT 5  /*存储空间分配增量*/
typedef  int ElemType; /*定义元素的类型*/
typedef  int Status; /*定义元素的类型*/
typedef struct{
    ElemType *base;
    ElemType *top;
    int stacksize;     /*当前已分配的存储空间*/
}SqStack;

Status InitStack(SqStack &S);   /*构造空栈*/
Status push(SqStack &S,ElemType e); /*入栈*/
Status Pop(SqStack &S,ElemType &e);  /*出栈*/
Status GetTop (SqStack S, ElemType &e); /*取顺序栈栈顶元素*/
Status StackEmpty(SqStack  S); /*判断顺序栈是否为空*/
int StackLength( SqStack S );/*求顺序栈的长度*/
Status CreateStack(SqStack &S);     /*创建栈*/
void PrintStack(SqStack &S);   /*出栈并输出栈中元素*/
Status DestroyStack( SqStack &S );   /*销毁栈*/

Status InitStack(SqStack &S){
    S.base=(ElemType *)malloc(STACK_INT_SIZE *sizeof(ElemType));
    if(!S.base) return ERROR;
    S.top=S.base;
    S.stacksize=STACK_INT_SIZE;
    return OK;
}/*InitStack*/

Status Push(SqStack &S,ElemType e)  /*入栈*/
{
    while(S.top-S.base>=S.stacksize)
	{
		S.base=(ElemType *)realloc(S.base,
			(S.stacksize + STACKINCREMENT)*sizeof(ElemType));
		if(!S.base)return False;
		S.top=S.base+S.stacksize;
		S.stacksize+=STACKINCREMENT;
	}
	*S.top++ =e;
	return OK;
}/*Push*/

Status Pop(SqStack &S,ElemType &e)   /*出栈*/
{
	if(S.top ==S.base)return ERROR;
	e = *--S.top;
	return OK;
	
}/*Pop*/

Status GetTop (SqStack S, ElemType &e) /*取顺序栈栈顶元素*/
{
	if(S.top ==S.base)return ERROR;
	e=*(S.top - 1);
	return OK;
}/*GetTop*/

Status StackEmpty(SqStack  S) /*判断顺序栈是否为空*/
{
	if(S.top==S.base)return True;
	else return ERROR;
}/*StackEmpty*/

int StackLength( SqStack S )/*求顺序栈的长度*/
{
	return S.top-S.base;
}/*StackLength*/

Status CreateStack(SqStack &S)    /*创建栈*/
{
    int e;
    if(InitStack(S))
        printf("Init Success!\n");
    else{
        printf("Init Fail!\n");
        return ERROR;
    }
    printf("input data:(Terminated by inputing a character)\n");
    while(scanf("%d",&e))
        Push(S,e);
    return OK;
}/*CreateStack*/

void PrintStack(SqStack &S){
    ElemType e;
    while(Pop(S,e))
        printf("%3d",e);
    printf("\n");
}/*Pop_and_Print*/

Status DestroyStack(SqStack &S)   /*销毁栈*/
{
	if( S.base )
	{
		free(S.base);
		S.stacksize = 0;
		S.base = S.top = NULL;
	}
	return OK;
}

int main(){
    SqStack ss;
	ElemType e;
    printf("\ncreate Stack\n");
    CreateStack(ss);
	GetTop(ss,e);
    printf("\nData on the top of the Stack is %d\n",e);
    printf("\n");
	if(StackEmpty(ss)) 
		printf("\nthe Stack is empty.\n");
	else
		printf("\nthe Stack is not empty.\n");
    printf("\n");
    printf("\nThere are %d datas in the Stack \n",StackLength(ss));
    printf("\n");
    printf("\nPop&Print\n");
    PrintStack(ss);	
	if(StackEmpty(ss)) 
		printf("\nthe Stack is empty.\n");
	else
		printf("\nthe Stack is not empty.\n");
    printf("\n");	
	DestroyStack(ss);
    return 0;
}  

2.链栈

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define True 1
#define False 0

typedef  int SElemType; /*定义元素的类型*/
typedef  int Status; 

typedef  struct StackNode{
	SElemType  data;
	struct StackNode *next;
} StackNode, *LinkStack;

Status InitStack(LinkStack &S);   /*构造空栈*/
Status push(LinkStack &S,SElemType e); /*入栈*/
Status Pop(LinkStack &S,SElemType &e);  /*出栈*/
Status GetTop (LinkStack S, SElemType &e); /*取链栈栈顶元素*/
Status StackEmpty(LinkStack  S); /*判断链栈是否为空*/
int StackLength(LinkStack S );/*求链栈的长度*/
Status CreateStack(LinkStack &S);     /*创建栈 栈中元素依次入栈*/
void PrintStack(LinkStack &S);   /*出栈并输出栈中元素*/
Status DestroyStack(LinkStack &S );   /*销毁栈*/

Status InitStack(LinkStack &S)   /*构造空栈*/
{
	S=NULL;
	return True;
}
Status push(LinkStack &S,SElemType e) /*入栈*/
{
	StackNode *p;
	p=(StackNode *)malloc(sizeof(StackNode));
	if(!p)return False;
	p->data=e;
	p->next=S;
	S=p;
	return OK;
}
Status Pop(LinkStack &S,SElemType &e)  /*出栈*/
{
	StackNode *p;
	if(S==NULL)return ERROR;
	e = S->data;
	p=S;
	S=S->next;
	free(p);
	return OK;
}
Status GetTop (LinkStack S, SElemType &e) /*取链栈栈顶元素*/
{
	if(S==NULL)return ERROR;
	else return e=S->data;
}
Status StackEmpty(LinkStack  S) /*判断链栈是否为空*/
{
	if(S==NULL)return True;
	else return ERROR;
}
int StackLength(LinkStack S )/*求链栈的长度*/
{
	int j=0;
	while(S!=NULL)
	{
		j++;
		S=S->next;
	}
	return j;
}
Status CreateStack(LinkStack &S)     /*创建栈 栈中元素依次入栈*/
{
	int e;
	if(InitStack(S))
		printf("Init Success!\n");
    else
	{
        printf("Init Fail!\n");
        return ERROR;
	}
	printf("input data:\n");
    while(scanf("%d",&e))
        push(S,e);
    return OK;
	
}
void PrintStack(LinkStack &S)   /*出栈并输出栈中元素*/
{
	SElemType e;
	while(Pop(S,e))
	{
		
		printf("%d",e);
	}
	
}
Status DestroyStack(LinkStack &S )   /*销毁栈*/
{
	while(S)
	{
		free(S);	
	}
	return OK;
}

int main()
{
	LinkStack ss;
	SElemType e;
	printf("\ncreate Stack\n");
	CreateStack(ss);
	GetTop(ss,e);
	printf("\nData on the top of the Stack is %d\n",e);
    printf("\n");
	if(StackEmpty(ss)) 
		printf("\nthe Stack is empty.\n");
	else
		printf("\nthe Stack is not empty.\n");
    printf("\n");
    printf("\nThere are %d datas in the Stack \n",StackLength(ss));
    printf("\n");
	printf("\nPop&Print\n");
    PrintStack(ss);	
	if(StackEmpty(ss)) 
		printf("\nthe Stack is empty.\n");
	else
		printf("\nthe Stack is not empty.\n");
    printf("\n");	
	DestroyStack(ss);
    return 0;
}  

3.链队列

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define True 1
#define False 0

typedef  int QElemType; /*定义元素的类型*/
typedef  int Status; 

typedef  struct QNode{//结点类型
	QElemType data ;
	struct QNode *next ;
}QNode, *QueuePtr;
typedef   struct{ // 链队列类型
	QueuePtr front;  //队头指针
	QueuePtr rear;   //队尾指针
}LinkQueue;

Status InitQueue (LinkQueue &Q);   /*初始化链队列*/
Status EnQueue(LinkQueue &Q, QElemType e); /*入队*/
Status DeQueue(LinkQueue &Q, QElemType &e);  /*出队*/
Status GetHead(LinkQueue Q, QElemType &e); /*取链队列的队头元素*/
Status QueueEmpty(LinkQueue Q); /*判断链队列是否为空*/
int QueueLength (LinkQueue Q);/*求队长*/
Status CreateQueue(LinkQueue &Q);     /*创建链队列  链队列中元素依次入队*/
void PrintQueue(LinkQueue &Q);   /*输出链队列中元素*/
Status DestroyQueue(LinkQueue &Q);   /*销毁链队列*/

Status InitQueue(LinkQueue &Q)   /*初始化链队列*/
{
	Q.front=Q.rear=(QNode *)malloc(sizeof(QNode));
	if(!Q.front)return False;
	Q.front->next=NULL;
	return OK;
}
Status EnQueue(LinkQueue &Q, QElemType e) /*入队*/
{
	QNode *p;
	p=(QNode *)malloc(sizeof(QNode));
	if(!p)return False;
	p->data=e;
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	return OK;
}
Status DeQueue(LinkQueue &Q, QElemType &e)  /*出队*/
{
	QNode *p;
	if(Q.front==Q.rear)return ERROR;
	p=Q.front->next;
	e=p->data;
	Q.front->next=p->next;
	if(Q.rear==p)Q.rear=Q.front;    //Q.rear=Q.front不可换边,程序提前结束
	free(p);
	return OK;
}
Status GetHead(LinkQueue Q, QElemType &e) /*取链队列的队头元素*/
{
	QNode *p;
	if(Q.front==Q.rear)return ERROR;
    p=Q.front->next;
	e=p->data;
	Q.front->next=p->next;
    if(Q.rear==p)Q.front=Q.rear;
	free(p);
	return OK;
}
Status QueueEmpty(LinkQueue Q) /*判断链队列是否为空*/
{
	if(Q.front==Q.rear)return True;
	else return ERROR;
}
int QueueLength (LinkQueue Q)/*求队长*/
{
    QNode *p;
	QElemType j=0;
    while(Q.front)
	{
		p=Q.front->next;
		j++;
		Q.front=Q.front->next;
	}
	return j;
}
Status CreateQueue(LinkQueue &Q)     /*创建链队列  链队列中元素依次入队*/
{
	QElemType e;
	if(InitQueue(Q))
		printf("Init Success!\n");
    else
	{
        printf("Init Fail!\n");
        return ERROR;
	}
	printf("input data:\n");
    while(scanf("%d",&e))
        EnQueue(Q,e);
    return OK;
}
void PrintQueue(LinkQueue &Q)   /*输出链队列中元素*/
{
	QElemType e;
	while(DeQueue(Q,e))
	printf(" %d  ",e);
}
Status DestroyQueue(LinkQueue &Q)   /*销毁链队列*/
{
	while(Q.front)
	{
		Q.rear=Q.front->next;
		free(Q.front);
		Q.front=Q.rear;
	}
	return OK;
}

int main()
{
	LinkQueue ss;
	QElemType e;
	printf("\ncreate Queue\n");
	CreateQueue(ss);
	GetHead(ss,e);
	printf("\nData on the ahead of the Queue is %d\n",e);
    printf("\n");
	if(QueueEmpty(ss)) 
		printf("\nthe Queue is empty.\n");
	else
		printf("\nthe Queue is not empty.\n");
    printf("\n");
    printf("\nThere are %d datas in the Queue \n",QueueLength(ss));
    printf("\n");	
    printf("\n DeQueue & Print\n");
    PrintQueue(ss);	
	if(QueueLength(ss)) 
		printf("\nthe Queue is empty.\n");
	else
		printf("\nthe Queue is not empty.\n");
    printf("\n");
	DestroyQueue(ss);
    return 0;
}   	

4.循环队列

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define True 1
#define False 0
#define MAXQSIZE 10 // 最大队列长度

typedef  int QElemType; /*定义元素的类型*/
typedef  int Status; 

typedef struct {
	QElemType *base;// 动态分配存储空间基址
	int front;//队头指针 
	int rear;//队尾指针
} SqQueue;

Status InitQueue (SqQueue &Q);   /*初始化循环队列*/
Status EnQueue(SqQueue &Q, QElemType e); /*入队*/
Status DeQueue(SqQueue &Q, QElemType &e);  /*出队*/
Status GetHead(SqQueue Q, QElemType &e); /*取循环队列的队头元素*/
Status QueueEmpty(SqQueue Q); /*判断循环队列是否为空*/
int QueueLength (SqQueue Q);/*求队长*/
Status CreateQueue(SqQueue &Q);     /*创建循环队列  循环队列中元素依次入队*/
void PrintQueue(SqQueue &Q);   /*输出循环队列中元素*/
Status DestroyQueue(SqQueue &Q);   /*销毁栈循环队列*/

Status InitQueue (SqQueue &Q)   /*初始化循环队列*/
{
	Q.base=(QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
	if(!Q.base)return False;
	Q.front=Q.rear=0;
	return OK;
}
Status EnQueue(SqQueue &Q, QElemType e) /*入队*/
{
	if((Q.rear+1)%MAXQSIZE==Q.front)return ERROR;
	Q.base[Q.rear] = e;
	Q.rear=(Q.rear+1)%MAXQSIZE;
	return OK;
}
Status DeQueue(SqQueue &Q, QElemType &e)  /*出队*/
{
	if(Q.front==Q.rear)return ERROR;
	e=Q.base[Q.front];
	Q.front=(Q.front+1)%MAXQSIZE;
	return OK;
}
Status GetHead(SqQueue Q, QElemType &e) /*取循环队列的队头元素*/
{
    if(Q.front==Q.rear)return ERROR;
    e = Q.base[Q.front];
	Q.front=(Q.front+1)%MAXQSIZE;
	return OK;
}
Status QueueEmpty(SqQueue Q) /*判断循环队列是否为空*/
{
	if(Q.front==Q.rear)return True;
	else return ERROR;
}
int QueueLength (SqQueue Q)/*求队长*/
{
	return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
Status CreateQueue(SqQueue &Q)     /*创建循环队列  循环队列中元素依次入队*/
{
	QElemType e;
    if(InitQueue(Q))
        printf("Init Success!\n");
    else{
        printf("Init Fail!\n");
        return ERROR;
    }
    printf("input data:\n");
    while(scanf("%d",&e))
        EnQueue(Q,e);
    return OK;
}
void PrintQueue(SqQueue &Q)   /*输出循环队列中元素*/
{
	QElemType e;
    while(DeQueue(Q,e))
        printf("%3d",e);
    printf("\n");
}
Status DestroyQueue(SqQueue &Q)   /*销毁栈循环队列*/
{
	while ( Q.front!= Q.rear)
{
  free(&Q.base[Q.front]);
  Q.front=(Q.front+1)%MAXQSIZE;
}	
   return OK;
}
int main()
{
    SqQueue ss;
	QElemType e;
	printf("\ncreate Queue\n");
	CreateQueue(ss);
	GetHead(ss,e);
	printf("\nData on the ahead of the Queue is %d\n",e);
    printf("\n");
	if(QueueEmpty(ss)) 
		printf("\nthe Queue is empty.\n");
	else
		printf("\nthe Queue is not empty.\n");
    printf("\n");
    printf("\nThere are %d datas in the Queue \n",QueueLength(ss));
    printf("\n");	
    printf("\n DeQueue & Print\n");
    PrintQueue(ss);
	DestroyQueue(ss);
	printf("\nthe Queue is destroy......\n");
	printf("\n");
    return 0;
}   	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值