zoukankan      html  css  js  c++  java
  • step3 . day4 数据结构之线性表 栈和队

    补充一下:循环链表初学可能不好理解,除了多画图以外,把循环链表想象成无限的单向(或者双向)链表,每一个元素都是中间元素,就更好理解了。

    1.栈和队是线性表的两种特殊管理逻辑,两者都是线性表

    2.栈的原则是先入后出FILO(first in last out),类似桶装饼干,最后装入的先被取出用掉,只能在栈顶进行压栈和弹栈操作

    3.队的原则是先入先出FIFO(first in first out),类似水管,流出的水是先进入的水,只能在队尾插入,队首删除操作。

    ①栈的顺序存储结构实现,和顺序链表一样,只是需要遵从栈的规则

    ②栈的链表结构实现


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


    typedef int datatype;
    typedef struct stack{
    datatype data;
    struct stack * next; //栈顶的位置
    }linkstack;

    //1. 创建一个空栈
    linkstack *linkstack_create()
    {
    linkstack *s = NULL;
    s = (linkstack *)malloc(sizeof(linkstack));

    s->next = NULL;

    return s;
    }

    //2. 入栈操作 压栈
    void linkstack_push(linkstack *s,int value)
    {
    linkstack * temp = linkstack_create();
    temp->data = value;

    while(s->next != NULL){ s = s->next;}

    temp->next = s->next;
    s->next = temp;
    }


    //判断栈为空
    int linkstack_is_empty(linkstack *s)
    {
    return s->next == NULL ? 1 : 0;
    }

    //4.出栈 弹栈
    int linkstack_pop(linkstack *s)
    {
    if(linkstack_is_empty(s) == 1)
    {
    printf("stack is empty. ");
    return -1;
    }

    int value;
    linkstack * temp;
    while(s->next->next != NULL){ s = s->next;}
    value = s->next->data;
    temp = s->next;
    s->next = NULL;
    free(temp);
    temp = NULL;

    return value;
    }

    //打印栈里面的所有值
    int linkstack_show(linkstack *s)
    {
    if(linkstack_is_empty(s) == 1)
    {
    printf("stack is empty. ");
    return -1;
    }
    while(s->next !=NULL){
    printf("%d ",s->next->data);
    s = s->next;
    }
    printf(" ");
    return 0;
    }

    int main(int argc, const char *argv[])
    {
    linkstack *s = NULL;
    s = linkstack_create();

    linkstack_push(s,1);
    linkstack_push(s,2);
    linkstack_push(s,3);
    linkstack_push(s,4);
    linkstack_push(s,5);
    linkstack_push(s,6);

    linkstack_show(s);

    printf("%d ",linkstack_pop(s));
    linkstack_show(s);
    return 0;
    }

    ③队的顺序存储实现,需要掌握的队首和队尾值之间的相互取余关系,实现循环队列


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

    #define N 32

    typedef struct queue{
    int date[N];
    int front;
    int rear;
    }sequeue;

    sequeue * sequeue_creat(){
    sequeue * q = NULL;
    q = (sequeue*)malloc(sizeof(sequeue));

    q->front = 0;
    q->rear = 0;
    return q;
    }

    int sequeue_is_full(sequeue *q){
    return (q->rear + 1) % N == q->front ? 1 : 0;
    }

    int sequeue_is_empty(sequeue *q){
    return q->rear == q->front ? 1 : 0;
    }

    int sequeue_input(sequeue *q,int value){
    if(sequeue_is_full(q)){
    printf("sequeue_is_full ");
    return -1;
    }

    q->date[q->rear] = value;
    q->rear = (q->rear + 1) % N;
    return 0;
    }

    int sequeue_output(sequeue * q){
    if(sequeue_is_empty(q)){
    printf("sequeue_is_empty ");
    return -1;
    }
    int value;
    value = q->date[q->front];
    q->front = (q->front+1) % N;
    return value;
    }

    void sequeue_show(sequeue* q){

    int i = 0;
    for(i = q->front;i != q->rear;i = ((i+1) % N)){
    printf("%d ",q->date[i]);
    }
    printf(" ");

    }


    int main(int argc, const char *argv[])
    {

    sequeue *q =NULL;
    q = sequeue_creat();

    sequeue_input(q,1);
    sequeue_input(q,2);
    sequeue_input(q,3);
    sequeue_input(q,4);
    sequeue_input(q,5);
    sequeue_input(q,6);

    sequeue_show(q);

    printf("%d ",sequeue_output(q));
    sequeue_show(q);

    return 0;
    }

    ③队的链表存储形式实现,和单向链表类似,注意保持front和priv指针


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

    //队成员结构体
    typedef struct linkqueue{
    int date;
    struct linkqueue * next;
    }linkqueue;

    //队首尾指针结构体
    typedef struct lnqueue{
    struct linkqueue * front;
    struct linkqueue * priv;
    }lnqueue;

    //节点创建函数
    linkqueue * linkqueue_creat(){
    linkqueue * head = NULL;
    head = (linkqueue*)malloc(sizeof(linkqueue));

    head->next = NULL;
    return head;
    }

    //队头及队指针创建
    lnqueue * lnqueue_creat(){
    linkqueue * head =NULL;
    head = linkqueue_creat();
    lnqueue* ln = NULL;
    ln = (lnqueue *)malloc(sizeof(lnqueue));

    ln->front = head;
    ln->priv = head;
    }

    //进队 尾插
    void lnqueue_input(lnqueue * ln,int value){

    linkqueue * temp =NULL;
    temp = linkqueue_creat();
    temp->date = value;

    temp->next = ln->priv->next;
    ln->priv->next = temp;
    ln->priv = temp;
    }

    //判断是否位空队
    int lnqueue_is_empty(lnqueue *ln){
    return ln->front == ln->priv ? 1 : 0;
    }

    //遍历
    int lnqueue_show(lnqueue * ln){
    if(lnqueue_is_empty(ln)){
    printf("lnqueue_is_empty ");
    return -1;
    }
    linkqueue *temp = ln->front;
    while(temp->next != NULL){
    printf("%d ",temp->next->date);
    temp = temp->next;
    }
    printf(" ");
    return 0;
    }

    //出队 头删
    int lnqueue_output(lnqueue * ln){
    if(lnqueue_is_empty(ln)){
    printf("lnqueue_is_empty ");
    return -1;
    }

    linkqueue * temp =NULL;
    int value;

    temp = ln->front->next;
    value = temp->date;
    ln->front->next = temp->next;
    if(ln->priv == temp){
    ln->priv = ln->front;
    }
    free(temp);
    temp = NULL;

    return value;


    }

    int main(int argc, const char *argv[])
    {
    lnqueue * ln = NULL;
    ln = lnqueue_creat();

    lnqueue_input(ln,1);
    lnqueue_input(ln,2);
    lnqueue_input(ln,3);
    lnqueue_input(ln,3);
    lnqueue_input(ln,3);
    lnqueue_input(ln,6);

    lnqueue_show(ln);

    printf("%d ",lnqueue_output(ln));
    lnqueue_show(ln);

    printf("%d ",lnqueue_output(ln));
    lnqueue_show(ln);

    printf("%d ",lnqueue_output(ln));
    lnqueue_show(ln);

    printf("%d ",lnqueue_output(ln));
    lnqueue_show(ln);

    printf("%d ",lnqueue_output(ln));
    lnqueue_show(ln);

    printf("%d ",lnqueue_output(ln));
    lnqueue_show(ln);

    printf("%d ",lnqueue_output(ln));
    lnqueue_show(ln);
    return 0;
    }

  • 相关阅读:
    ssh出错 sign_and_send_pubkey: signing failed: agent refused operation
    使用dd命令制作U盘启动盘wodim刻录光盘cd dvd
    Python示例项目学习
    Python知乎上推荐的项目
    Python10大热门项目
    Python开源项目Top30
    Python适合练手的项目
    Python80个练手项目列表
    Python爬虫实例项目
    Python实例100个(基于最新Python3.7版本)
  • 原文地址:https://www.cnblogs.com/huiji12321/p/11233942.html
Copyright © 2011-2022 走看看