数据结构栈和队列Linux Ubuntu下的C语言实现

       大家好,我是练习编程时长两年半的昆工第一ikun,前几天我们说完了线性顺序表,单向循环链表和双向循环链表,今天我们来说一下比较简单的栈和队列。话不多说,上代码。

    

1.栈

逻辑结构:线性结构

存储结构:顺序存储、链式存储

特点:先进后出(FILO)

(1)顺序栈:

顺序栈相关结构体:
    #define N 64
    typedef int data_t;
    typedef struct seqstack
    {
        data_buf[N];
        int top; //栈顶指针,指向栈中最后一个元素的下标
    }SeqStack;
      
#include <stdio.h>
#include <stdlib.h>
#include "seqstack.h"

Seqstack *seqstack_create()
{
	Seqstack *s = (Seqstack *)malloc(sizeof(Seqstack));
	if(NULL == s){
		printf("malloc faild!\n");
	}
	s->top = -1;
	return s;

}

int seqstack_is_full(Seqstack *s)
{

	if(s->top == N-1){
	return 1;
	}
	else
	return 0;
}
int seqstack_is_empty(Seqstack *s)
{

	if(s->top == -1){
	return 1;
	}
	else
	return 0;
}

void seqstack_push(Seqstack *s, data_t data)
{
 	if(seqstack_is_full(s) == 1){
 	return;
 	}
 	s->top++;
 	s->buf[s->top] = data;
}

void seqstack_push(Seqstack *s, data_t *data)
{
      if(seqstack_is_empty(s) ==1){
      return;
            }
	*data = s->buf[s->top];
	s->top--;
	return;
}

(2)栈的链式存储

链式栈节点相关结构体

typedef struct linkstack
{
    data_t  data;
    struct linkstack *next;
}Linkstack;
#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"

Linkstack *linkstack_create()    //创建链表
{
	Linkstack *s = (Linkstack *)malloc(sizeof(Linkstack));
	if(NULL == s){
	printf("malloc faild!\n");
	return NULL;
	}
	s->data = -1;
	s->next = NULL;
	return s;
}
int linkstack_is_empty(Linkstack *s)  //判空
{
	if(s->next == NULL){
	return 1;
	}
	else{
	return 0;
	}
}
void linkstack_push(Linkstack *s, data_t data)  //进栈
{
	Linkstack *new = (Linkstack *)malloc(sizeof(Linkstack)); //创建新的节点
    if(NULL == new){
        printf("创建失败!\n");
        return;
    }
    new->data = data;     //给新节点赋值
    new->next = NULL;

    new->next = s->next;   //将新节点与头节点链接
    s->next = new;         
    return;	
}
void linkstack_pop(Linkstack *s)
{
	if(linkstack_is_empty(s) == 1){
	printf("linkstack is empty!\n");
	return;
	}
	
	Linkstack *p = s->next;
	while(p->next != NULL){
	
	s->next = p->next;
	printf("%d ", p->data);
	free(p);
	p = s->next;
	}
	printf("%d\n", p->data);
	return;
}

2.队列

逻辑结构:线性结构

存储结构:顺序存储、链式存储

特点:先进先出(FIFO)

队列的出队和入队是在队列的两端进行

顺序队列相关结构图

#define N 64
typedef int data_t;
struct sequeue
{
    data_t buf[N];
    int front;  //队头指针,指向出队元素的下标
    int rear;   //队尾指针,指向最后一个元素的下标
}
​
空队列:
    front == rear;
    
满队列:
    (rear+1)%N == front;
队列中入队最大数为N-1,为了区分空队列和满队列
       
    
#include <stdio.h>
#include <stdlib.h>
#include "sequeue.h"

Sequeue *create_sequeue()
{
	Sequeue *s = (Sequeue *)malloc(sizeof(Sequeue));
	if(NULL == s){
	printf("malloc failed!\n");
	return NULL;
	}
	memset(s->buf, 0, sizeof(s->buf));
	
	s->front = 0;
	s->rear = 0;
	return s;
}

int sequeue_is_full(Sequeue *s)
{
	if((s->rear+1)%N = s->front){
	return 1;
	}
	else{
	return 0;
	}
}
int sequeue_is_empty(Sequeue *s)
{
	if(s->rear == s->front){
	return 1;
	}
	else{
	return 0;
	}
}
void enqueue(Sequeue *s, data_t data)
{
	if(sequeue_is_full(s) != 1){
	s->buf[s->rear] = data;
	s->rear = (s->rear+1)%N;
	}
	else
	printf("队列已满!\n");
	return;
}


void dequeue(Sequeue *s, data_t *data)
{
	int n = s->front % N;
	if(sequeue_is_empty(s) != 1){
	*data = s->buf[s->front];
	s->front = (s->front+1)%N;
		
		}

}

链式队列

链式队列模型相关结构体

typedef int data_t;
typedef struct node
{
    data_t data
    struct node *next;
}Node
​
typedef struct linkqueue
{
    struct node *front; //链式队列的队头节点
    struct node *rear;   //链式队列的队尾节点
}Linkqueue;    
​
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkqueue.h"

Linkqueue *create_linkqueue()
{
	Node *head = (Node *)malloc(sizeof(Node));
	if(NULL == head){
	printf("malloc failed!\n");
	return NULL;
	}
	head->data = -1;
	head->next = NULL;
	
	Linkqueue *Q =(Linkqueue *)malloc(sizeof(Linkqueue));
	if(NULL == Q){
	printf("malloc failed!\n");
	return NULL;
	}
	
	Q->front = head;
	Q->rear = head;
	return Q;
}

int linkqueue_is_empty(Linkqueue *Q)
{
	if(Q->rear == Q->front){
	return 1;
	}
	else{
	return 0;
	}
	
}

void enlinkqueue(Linkqueue *Q, data_t data)
{
	Node *new = (Node *)malloc(sizeof(Node));
	if(NULL == new){
	printf("malloc failed!\n");
	return;
	}
	new->data = data;
	new->next = NULL;
	
	Q->rear->next = new;
	Q->rear = Q->rear->next; 
}

void delinkqueue(Linkqueue *Q, data_t *data)
{
	Node *p = Q->front->next;
	if(Q->front->next != NULL){
	Q->front->next = p->next;
	*data = p->data;
	free(p);
	}
	else{
	Q->rear = Q->front;
	}
	return;
}

        好了,今天的栈和队列就分享到这啦,因为涉及到四个内容,坤坤就只给出了函数主体实现代码,大家自行补充头文件和主函数吧,我是练习编程时长两年半的昆工第一ikun,咋们明天见。

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值