2024/4/7作业

文章介绍了用C语言实现的链式栈(包括创建、入栈、出栈和显示操作)以及顺序队列(创建、入队、出队、显示和队列长度计算)的示例代码,展示了基本的数据结构操作实现。
摘要由CSDN通过智能技术生成

linkstack.c

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

Linkstack *create_linkstack(){
    Linkstack *head=(Linkstack *)malloc(sizeof(Linkstack));
    if(NULL == head){
        printf("申请空间失败,创建链式栈失败\n");
        return NULL;
    }
    head->data.len=0;
                                                                      
    head->next = NULL;

    return head;
}

void push_linkstack(Linkstack *head,int num){
    Linkstack *temp=(Linkstack *)malloc(sizeof(Linkstack));
    if(NULL == temp){
        printf("申请空间失败,入栈失败\n");
        return;
    }
    temp->data.text=num;
    temp->next=NULL;

    temp->next = head->next;
    head->next = temp;
    head->data.len++;
    return;
}

datatype pop_linkstack(Linkstack* head){
    if(NULL == head->next){
        printf("空链式栈\n");
        return 0;
    }
    Linkstack *p=head->next;
    head->next=p->next;
    datatype num = p->data.text;
    free(p);
    p=NULL;
    head->data.len--;
    return num;
}

void show_linkstack(Linkstack *head){
    Linkstack *p=head;
    while(p->next!=NULL){
        p=p->next;
        printf("%d\t",p->data.text);
    }
    putchar(10);
    return;
}

linkstack.h

#ifndef __LINKSTACK__
#define __LINKSTACK__

typedef int datatype;
typedef struct node{
    union{
        datatype text;
        datatype len;
    }data;
    struct node *next;
}Linkstack;

Linkstack *create_linkstack();
void push_linkstack(Linkstack *head,int num);
datatype pop_linkstack(Linkstack *head);           
void show_linkstack(Linkstack *head);

#endif

main.c

#include <stdio.h>
#include "linkstack.h"

int main(int argc, const char *argv[])
{
    Linkstack *head=create_linkstack();
    printf("请输入一个数字\n");
    int num;
    scanf("%d",&num);
    while(num!=1){
        if(num%2==1)
            push_linkstack(head,1);
        else
            push_linkstack(head,0);                     
        num/=2;
    }
    push_linkstack(head,1);
    for(int i=0;i<8-head->data.len;i++)
        printf("0\t");
    while(head->next != NULL)
        printf("%d\t",pop_linkstack(head));
    putchar(10);
    return 0;
}
                                                        

sequeue.c

#include <stdio.h>
#include <stdlib.h>
#include "./sequeue.h"

Sequeue *create_sequeue(){
    Sequeue *sq=(Sequeue *)malloc(sizeof(Sequeue));
    if(NULL == sq){
        printf("申请空间失败,顺序队列创建失败\\n");
        return NULL;
    }
    
    sq->front=0;
    sq->rear=0;
    
    return sq;
}

void push_sequeue(Sequeue *sq,int num){
    if((sq->rear+1)%7 == sq->front){
        printf("队列已满,入队失败\n");
        return;
    }
    sq->data[sq->rear]=num;
    sq->rear=(sq->rear+1)%N;
    return;
}

datatype pop_sequeue(Sequeue *sq){
    if(sq->rear == sq->front){
        printf("队列为空,出队失败\n");
        return (datatype)-1;
    }
    datatype num = sq->data[sq->front];
    sq->front=(sq->front+1)%7;
    return num;
}

void show_sequeue(Sequeue *sq){
    for(int i = sq->front;i!=sq->rear;i=(i+1)%N){
        printf("%d ",sq->data[i]);
    }
    putchar(10);
    return;
}

void len_sequeue(Sequeue *sq){
    int count=0;
    for(int i = sq->front;i!=sq->rear;i=(i+1)%N){
        count++;
    }
    printf("长度为%d\n",count);
    return;                                                                     
}

sequeue.h

#ifndef __SEQUEUE__
#define __SEQUEUE__

#define N 7
typedef int datatype;
typedef struct {
    datatype data[N];
    int front;
    int rear;
}Sequeue;

Sequeue *create_sequeue();
void push_sequeue(Sequeue *sq,int num);
datatype pop_sequeue(Sequeue *sq);
void show_sequeue(Sequeue *sq);
void len_sequeue(Sequeue *sq);

#endif                                                          

main.c

#include <stdio.h>
#include "./sequeue.h"

int main(int argc, const char *argv[])
{
    Sequeue *sq = create_sequeue();
    push_sequeue(sq,1);
    push_sequeue(sq,2);
    push_sequeue(sq,3);
    push_sequeue(sq,4);
    push_sequeue(sq,5);
    push_sequeue(sq,6);
    pop_sequeue(sq);
    show_sequeue(sq);
    len_sequeue(sq);
    return 0;
}                                        
                                         

运行结果

linkqueue.h

#ifndef __LINKQUEUE__
#define __LINKQUEUE__

typedef int datatype;

typedef struct node{
    union{
        datatype text;
        int len;
    }data;
    struct node *next;
}Linkqueue;
                                                                          
typedef struct{
    Linkqueue *rear;
    Linkqueue *head;
}Linkqueue_hr;

Linkqueue_hr *create_linkqueue();
void push_linksequeue(Linkqueue_hr *lq,datatype num);
void pop_linksequeue(Linkqueue_hr *lq);
void show_linksequeue(Linkqueue_hr *lq);

#endif

linkqueue.c

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

Linkqueue_hr *create_linkqueue(){
    Linkqueue_hr *lq=(Linkqueue_hr *)malloc(sizeof(Linkqueue_hr));
    if(NULL == lq){
        printf("申请空间失败,链式队列创建失败\n");
        return NULL;
    }

    lq->head = (Linkqueue *)malloc(sizeof(Linkqueue));
    if(NULL == lq->head){
        printf("申请空间失败,头结点创建失败\n");
        free(lq);
        return NULL;
    }
    lq->head->data.len = 0;
    lq->head->next=NULL;

    lq->rear = lq->head;
    return lq;
}

void push_linksequeue(Linkqueue_hr *lq,datatype num){
    Linkqueue *temp = (Linkqueue *)malloc(sizeof(Linkqueue));
    if(NULL == temp){
        printf("创建节点失败,入队失败\n");
        return;
    }
    temp->data.text = num;
    temp->next = NULL;

    temp->next = lq->rear->next;
    lq->rear->next = temp;

    lq->rear = lq->rear->next;

    lq->head->data.len++;
}

void pop_linksequeue(Linkqueue_hr *lq){
    if(lq->head == lq->rear){
        printf("空队列\n");
        return;
    }
    Linkqueue *temp = lq->head->next;
    if(temp == lq->rear)
        lq->rear = lq->head;
    lq->head->next = temp -> next;
    free(temp);
    temp = NULL;

    lq->head->data.len--;
    return;
}

void show_linksequeue(Linkqueue_hr *lq){
    Linkqueue * p = lq->head;
    while(p->next != NULL){
        p = p ->next;                                                                                 
        printf("%d\t",p->data.text);
    }
    putchar(10);
    return;
}
                                                                                                      

main.c

#include <stdio.h>
#include "linkqueue.h"

int main(int argc, const char *argv[])
{
    Linkqueue_hr *lq = create_linkqueue();
    push_linksequeue(lq,100);
    push_linksequeue(lq,200);
    push_linksequeue(lq,300);
    push_linksequeue(lq,400);                    
    pop_linksequeue(lq);
    show_linksequeue(lq);
    return 0;
}

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值