栈和队列的基本操作 (C语言)

本文详细介绍了如何使用C语言实现顺序栈、链栈、循环队列和链队列的数据结构,并提供了初始化、判空、入栈、出栈、队列长度计算、入队和出队等基本操作的代码示例。通过这些示例,读者可以深入理解这些数据结构的实现原理和操作方法。
摘要由CSDN通过智能技术生成


编译器:VS Code
语言:C语言

栈的基本操作主要有:栈的初始化、判空、判满、取栈顶元素、在栈顶进行插入和删除。

顺序栈

顺序栈的基本操作

#include<stdio.h>
#include<stdlib.h>
#define  maxsize 10  //maximum number of elements of stack
#define  increment 10 //追加容量
typedef  struct 
{
    int *top;  //stack top pointer
    int *base;  //stack bottom pointer
    int size;   //stack capacity
}sqstack;
/*
*(s.base)            the elements of stack bottom
*(s.top-1)           the elements of stack top
s.top-s.base         the number of elements
s.top==s.base       stack empty
s.top-s.base==s.size  stack full
*/
//initialization
int init(sqstack &s)
{
    s.base=(int *)malloc(maxsize*sizeof(int));  //Allocate memory space
    if(!s.base)
    {
        printf("memory allocation failed!!");
        return false;
    }
    s.top=s.base;  //stack emmpty
    s.size=maxsize;  //stack capacity
    return true;
}
//judge whether it is an empty stack
int judge(sqstack s)
{   
    if(s.base==s.top)
    {
        printf("the stack is empty\n");
        return false;
    }
    else
    {
        printf("the stack is not empty\n");
        return false;
    }
}
//enter stack
int push(sqstack &s,int e)
{
    int *p;
    if(s.top-s.base==s.size)
    {
        p=(int *)realloc(s.base,(s.size+increment)*sizeof(int)); 
        printf("memory is modified successfully!");
        s.base=p;
        s.top=s.base+s.size;  //modify stack top pointer
        s.size+=increment;  //modify the capacity of stack
    }
	*(s.top++) = e;
    return true;
}   
//pop stack
int pop(sqstack &s)
{
    int e;
    //judge(s);
    e=*(--s.top);
    return  e;
}
//print
int print(sqstack s)
{
    judge(s);
    printf("the elements of stack:");
    while(s.top!=s.base)  
    {    
        printf("%3d",*(--s.top));
    }   
    printf("\n");
    return true;
}
int main()
{
    sqstack s;
    int e;
    init(s);
    for(int i=1;i<6;i++)
    {
        push(s,i);
    }
    print(s);
    e=pop(s);
    printf("%d",e);
}

链栈

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

typedef struct _node
{
    int data;  //data failed
    struct _node *next; //pointer failed
}node,*link_node;
//initialization
int init(link_node &l)
{
    l=(link_node)malloc(sizeof(node));  //Allocate memory space
    l->next=NULL;
    return true;
}
//push 
int push(link_node &l,int e)
{
    link_node p;
    p=(link_node)malloc(sizeof(node));  //Allocate a new memory space for p
    p->data=e;
    p->next=l->next;  //将p结点作为首结点
    l->next=p;
    return true;
}
//pop
int pop(link_node &l)
{
    int r;
    link_node p;
    if(l->next==NULL)
    {
        printf("the chain stack is empty\n");
        return false;
    }
    printf("the chain stack is not empty\n");
    p=l->next;
    r=p->data;
    l->next=p->next;
    free(p); 
    return r; 
}
//judge wewhter it is empty chain stack
int judge(link_node l)
{
    if(l->next==NULL)
    {
        printf("the chain stack is empty\n");
        return false;
    }
    else
    {
        printf("the chain stack is not empty\n");
        return true;        
    }
}
//print
int print(link_node l)
{
    printf("the elements of chain stack is:\n");
    link_node p=l->next;
    while(l->next!=NULL)
    {
        printf("%d\n",p->data);
        p=p->next;   //pointer moves backward
    }
    return true;
}
int main()
{
    int e,i;
    link_node l;
    init(l);
    while(scanf("%d",&i)!=EOF)
    {
        printf("%d enters the chain stack.\n",i);
        push(l,i);  //enter the stack
    }
    e=pop(l);
    printf("top elements is %d\n",e);
    //print(l);
}

队列

队列基本操作主要有:队列的初始化、判空、判满、取队列顶元素、在队列行插入和删除。

循环队列(circular queue)

#include<stdio.h>
#include<stdlib.h>
#define maxsize 10
typedef struct
{
    int data[maxsize];  //静态数组存放队列元素
    int front,rear;   //队头指针、队尾指针
}queue;
/*
队空:q.front=q.rear
队满:(q.rear+1)%maxsize==q.front
队列长度:(q.front+maxsize-q.rear)%maxsize
队头元素:q.base[q.front]
队尾元素:q.base[q.rear]
入队元素的下标:q.rear
*/
//initialization
int init(queue &q)
{
    q.front=q.rear=0;  //initial pointer all point to position 0
}
//judge whether it is an empty circular queue
int judge(queue q)
{
    if(q.rear==q.front)
    {
        printf("the queue is empty");
        return false;
    }
    else
    {
        printf("the queue is not empty");
        return true;
    }
}
//the length of queue
int length(queue q,int &len)
{
    //if(q.front<=q.rear)   //当front在rear后面
    //{
    //    length=q.rear-q.front;
    //}
    //else
    //{
    //    length=maxsize-(q.front-q.rear);  //front在rear前面
    //}
    //总结
    len=(maxsize+q.rear-q.front)%maxsize;
    return true;
}
//push
int push(queue &q,int e)   //rear进元素
{
    int len;
    length(q,len);
    if(len==maxsize-1){  //the circular is full
        printf("the queue is full");
        return false;
    }
    q.data[q.rear]=e;
    //if(q.rear<maxsize-1){  
    //    q.rear++;
    //}
    //else{   //rear==maxsize-1
    //    q.rear==0;
    //}
    //总结
    q.rear=(q.rear+1)%maxsize;
    printf("%d enters the queue\n",e);
}
//remove
int remove(queue &q)
{
    int e;
    if(q.rear==q.front){
        return false;
    }
    e=q.data[q.front];  //元素并没有删除,只是保存在e中
    if(q.front<maxsize-1){
        q.front++;
    }
    else{   //front==maxsize-1
        q.front=0;  //回到0的位置
    }
    return true;
}
//main fuction
int main()
{
    queue q;
    init(q);
    for(int i=0;i<=maxsize-1;i++){
        push(q,i);
    }
    printf("\nthe pointer rear is %d\n",q.rear);
    printf("the pointer front is %d",q.front);
}

链队列

在这里插入图片描述

  1. 元素结点的结构和链表一致。
  2. 包含指向头结点和队尾元素的指针。
  3. 先进先出
#include<stdio.h>
#include<stdlib.h>

typedef struct _node{
    int data;  //data fialed
    struct _node *next;   
}node,*link_node;
//define pointer
typedef struct{
    link_node front,rear; //队头指针、队尾指针
}*link_queue;
//initialization
int init(link_queue &q)
{   
    q->front=q->rear=(link_node)malloc(sizeof(node));// Allocate memory space
    q->front->next=NULL;
    return true;
}
//judge whther it is an empty chain queue
int judge(link_queue q)
{   
    if(q->rear==q->front){
        printf("the chain queue is empty\n");
        return true;
    }
    else{
        printf("the chain queue is not empty\n");
        return  true;
    }
}
//push
int push(link_queue &q,int e)
{
    link_node p=(link_node)malloc(sizeof(node));  //Allocate memory space for p 
    if(q->rear->next){
        printf("the chain queue is full");
        return false;
    }
    p->data=e;
    p->next=NULL;
    q->rear->next=p;
    q->rear=p;
    printf("%d enters the chain queue\n",q->rear->data);
    return true;
}
//去队头元素
int pop(link_queue &q)
{
    int e;
    link_node temp;
    if(q->rear=q->front){
        printf("the chain queue is empty\n");
        return false;
    }
    q->front=q->front->next;
    temp=q->front;
    e=temp->data;
    q->front=q->front->next;  //pointer front moves backward
    free(q); //free the space of q
    printf("top elements is %d\n",e);
    return true;
}
//print
int print(link_queue q)
{
    q->front=q->front->next;
    while(q->front!=q->rear){
        printf("%d\n",q->front->data);
        q->front=q->front->next;  //front moves backward
    }
    printf("%d",q->front->data);  //当front和rear相同时,输出最后一个结点的元素
    printf("\n");
    return true;
}
int main()
{
    link_queue q;
    init(q);
    for(int i=0;i<10;i++)
    {
        push(q,i);
    }
    //print(q);
    //judge(q);
    //pop(q);
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值