Data Structures - Implement stacks and queues using doubly linked lists

stacks and queues

The content of this section is an extension after the bidirectional chain: the implementation of stacks and queues is also implemented on the basis of the bidirectional chain. Detailed explanation of two-way chain: Two-way chain
Course source: bilibilili Li Huiqin

stack

The characteristics of the stack: first in, last out

stack creation

STACK* stack_create(int initsize)
{
    
    
   return llist_create(initsize);
}

push onto the stack

int stack_push(STACK *ptr ,const void*data)
{
    
    
    return llist_insert(ptr,data,LLIST_FORWARD);
}

pop

//匹配函数,使之永久成立
static int always_match(const void *p1,const void *p2)
{
    
    
    return 0;
}

int stack_pop(STACK *ptr ,void *data)
{
    
    
    return llist_fetch(ptr,(void *)0,always_match,data);
}

Destruction of the stack

void stack_destory(STACK *ptr)
{
    
    
    llist_destory(ptr);
}

stack operation code test

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

#define NAMESIZE 32
int ret;

struct score_st
{
    
    
    int id;
    char name[NAMESIZE];
    int math;
    int chinese;
};

static void  printf_s(void *record)
{
    
    
    struct score_st *r = record;
    printf("%d %s %d %d \n",r->id,r->name,r->math,r->chinese);
}

int main()
{
    
    
    STACK *st;
    struct score_st tmp;
    st =  stack_create(sizeof(struct score_st));
    if (st == NULL)
    {
    
    
       exit(1);
    }
    for (int i = 0; i < 7; i++)
    {
    
       
        tmp.id = i;
        snprintf(tmp.name,NAMESIZE,"std%d",i);
        tmp.math = rand()%100;
        tmp.chinese = rand()%100;
        
        if(stack_push(st,&tmp))
        exit(1);
    }
    
    while (1)
    {
    
    
        ret =  stack_pop(st,&tmp);
        if (ret == -1)
            break;
        printf_s(&tmp);
    }
    stack_destory(st);
    exit(0);
}

Output result:

insert image description here

queue

The characteristics of the queue: first in first out

team creation

QUEUE * queue_create(int size)
{
    
    
    return llist_create(size);
}

join the team

int queue_en(QUEUE *ptr,const void * data)
{
    
    
    return llist_insert(ptr,data,LLIST_BACKWARD);
}

out of the team

static int  alwaws_match(const void *p1 ,const void *p2)
{
    
    
    return 0;
}

int queue_de(QUEUE *ptr,void *data)
{
    
    
    return llist_fetch(ptr,(void*)0,alwaws_match,data);
}

Deletion of the queue

void queue_destory(QUEUE *ptr)
{
    
    
    llist_destory(ptr);
}

code test

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

#define MAXSIZE 32

struct score_st
{
    
    
    int id;
    char name[MAXSIZE];
    int math;
    int chinese;
};

static void printf_s(void *record)
{
    
    
    struct score_st *r = record;
    printf("%d %s %d %d \n",r->id,r->name,r->math,r->chinese);
}

int main()
{
    
    
    QUEUE *qu;
    int ret;
    struct score_st tmp;
    qu = queue_create(sizeof(struct score_st));
        if (qu == NULL)
            exit(1);       
    for (int  i = 0; i < 7; i++)
    {
    
    
        tmp.id = i;
        snprintf(tmp.name,MAXSIZE,"stu%d",i);
        tmp.math = rand()%100;
        tmp.chinese = rand()%100;
        
        if(queue_en(qu,&tmp)!=0)
            break;
    }
      
    while (1)
    {
    
    
        ret =  queue_de(qu,&tmp);
        if (ret == -1)
            break;
        printf_s(&tmp);
    }
    queue_destory(qu);
    exit(0);
}

Output result:

Contrary to the output of the stack~~
insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324040487&siteId=291194637