算法导论第十章 基本数据结构实现(栈,队列,链表),课后题答案

栈实现一种后进先出的策略,入栈和出栈,都是通过栈顶指针来操作。

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

#define SIZE 10
typedef struct{
    int array[SIZE];
    int top;
}T_stack;

bool Empty(T_stack * Stack){
    if(Stack->top == -1)
        return true;
    else
        return false;
}

void Push(T_stack * Stack ,int x){
    if(Stack->top == SIZE-1){
        printf("the stack is full\n");
        return;
    }
    else{
        Stack->top = Stack->top +1;
        Stack->array[Stack->top] = x;
    }
}

int Pop(T_stack * Stack ){
    if(Empty(Stack)){
        printf("the stack is empty\n");
        return;
    }
    else{
        Stack->top = Stack->top-1;
        return Stack->array[Stack->top +1];
    }
}

int main(void){
    int i,num;
    T_stack * Stack;
    Stack = malloc(sizeof(T_stack));
    Stack->top = -1;
    srand((unsigned)time(NULL));  
        for(i = 0;i< SIZE;i++){
        num = rand() % SIZE; //生成随机数
        Push(Stack,num);
    }
    for(i  = 0;i< SIZE;i++)
        printf("%d ",Stack->array[i]);
    printf("the last data is :%d\n",Pop(Stack));
    return 0;
}


队列

队列是一种先进先出的策略,就像排队一样,新来的从队尾加入,出队的从队头出去。

#include<stdio.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 10
typedef struct {
    int array[SIZE];
    int head;
    int tail;
}Heap;

void Enqueue(Heap * he,int x){
    if(he->head == ((he->tail+1)% SIZE)){
        printf("the heap is full");
        return ;
    }
    else{
        he->array[he->tail] = x;
        he->tail = (he->tail+1) % SIZE;
    }
}

int Dequeue(Heap * he){
    int x;
    if(he->head == he->tail){
        printf("the heap is empty");
        return;
    }
    else{
        x = he->array[he->head];
        if(he->head == SIZE-1)
            he->head = 0;
        else
            he->head--;
    }
    return x;
}

int main(void){
    int i,num;
    Heap * he;
    he = malloc(sizeof(Heap));
    he->head = he->tail = 0;
    srand((unsigned)time(NULL));
    for(i = 0;i <SIZE;i++){
        num = rand() %10;
        Enqueue(he,num);
    }
    printf("the heap is:\n");
    for(i = 0;i< SIZE;i++){
        printf("%d ",he->array[i]);
    }
    printf("*****************\n");
    printf("the first data is:%d\n",Dequeue(he));
    return 0;
}

课后10.1-2的代码实现

#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 10
typedef struct {
    int array[SIZE];
    int top1;
    int top2;
}T_stack;

bool Empty(T_stack * Stack,int zone);
void Push(T_stack * Stack,int zone,int x);
int Pop(T_stack * Stack,int zone);

bool Empty(T_stack * Stack,int zone){
    if(zone == 1){
        if(Stack->top1 == -1)
            return true;
        else
            return false;
    }
    if(zone == 2){
        if(Stack->top2 == SIZE)
            return true;
        else 
            return false;
    }
}

void Push(T_stack * Stack,int zone,int x){
    if(Stack->top1 == Stack->top2){
        printf("the stack is overflow\n");
        return;
    }
    if(zone == 1){
        Stack->top1 = Stack->top1+1;
        Stack->array[Stack->top1] = x;
    }
    else if(zone == 2){
        Stack->top2 = Stack->top2-1;
        Stack->array[Stack->top2] = x;
    }
}

int  Pop(T_stack * Stack,int zone){
    if(zone == 1){
        if(Empty(Stack,zone)){
            printf("the stack is empty\n");
            return ;
        }
        else{
            Stack->top1 = Stack->top1-1;
            return Stack->array[Stack->top1+1];
        }
    }
    else if(zone == 2){
        if(Empty(Stack,zone)){
            printf("the stack is empty\n");
            return;
        }
        else{
            Stack->top2 = Stack->top2+1;
            return Stack->array[Stack->top2-1];
        }
    }
}
int main(void){
    int i,num;
    T_stack * T_s;
    T_s = malloc(sizeof(T_stack));
    T_s->top1 = -1;
    T_s->top2 = SIZE;
    srand((unsigned)time(NULL));
    for(i = 0;i< SIZE/2;i++){
        num = rand() % 10;
        Push(T_s,1,num);
    }
    for(i  = 0;i< SIZE/2;i++){
        num = rand() % 10;
        Push(T_s,2,num);
    }
    printf("the left stack first data: %d\n",Pop(T_s,1));
    printf("the right stack first data: %d\n",Pop(T_s,2));
    return 0;
}

课后10.1-5的代码实现

#include<stdio.h>
#include <stdlib.h>
#define SIZE 10
typedef struct{
    int array[SIZE];
    int head;
    int tail;
}Deque;

void enqueue_head(Deque * deq,int x){
    if(deq->head == deq->tail+1){
        printf("the deque is overflow");
        return ;
    }
    else{

        deq->array[deq->head] = x;
        if(deq->head == 0)
            deq->head = SIZE-1;
        else{
            deq->head = deq->head-1;
        }
    }
    printf("the head is: %d \n",deq->head);
}

void enqueue_tail(Deque * deq ,int x){
    if(deq->head == deq->tail +1){
        printf("the deque is overflow");
        return ;
    }
    else{
        deq->array[deq->tail] = x;
        deq->tail = (deq->tail+1 )% SIZE;
    }
}

int Dequeue_head(Deque * deq){
    int x;
    if(deq->head == deq->tail){
        printf("the deque is empty");
        return ;
    }
    else{
        if(deq->head == SIZE-1){
            x = deq->array[0];
            deq->head = 0;
        }
        else{
            x = deq->array[deq->head+1];
            deq->head++;
        }
    }
    return x;
}

int Dequeue_tail(Deque * deq){
    int x;
    if(deq->head == deq->tail){
        printf("the deque is empty");
        return;
    }
    else{
        if(deq->tail == 0){
            x = deq->array[SIZE-1];
            deq->tail = SIZE-1;
        }
        else{
            x = deq->array[deq->tail-1];
            deq->tail--;
        }
    return x;
    }
}

int main(void){
    int i;
    Deque * deq;
    deq = malloc(sizeof(Deque));
    deq->head =0;
    deq->tail = 1;
    enqueue_head(deq,1);
    enqueue_head(deq,2);
    enqueue_tail(deq,3);
    enqueue_tail(deq,4);
    printf("%d ",deq->array[0]);
    printf("%d ",deq->array[1]);
    printf("%d ",deq->array[2]);
    printf("%d ",deq->array[9]);
    printf("the head first is :%d\n",Dequeue_head(deq));
    printf("the tail first is : %d \n",Dequeue_tail(deq));
    return 0;
}

链表

在构造结构体时表示方式略有不同,单向链表中的表示更加清晰。
1,带有表头的双向链表

//具有表头的双向链表
#include<stdio.h>
#include <stdlib.h>
typedef struct T_list {
    int key;
    struct T_list * prev;
    struct T_list * next;
}T_list;

T_list * List_Search(T_list * L,int k){
    T_list * P;
    P =  L->next;
    while(P != NULL){
        if(P->key == k)
            return P;
        else
            P = P->next;
    }
    return P;
}

void List_insert(T_list * L,int x){
    T_list * node;
    node = (T_list *)malloc(sizeof(T_list));
    node->key = x;
    if(L->next == NULL){
        L->next = node;
        node->next = NULL;
        node->prev = L;
    }
    else{
        node->next = L->next;
        L->next->prev = node;
        node->prev = L;
        L->next = node;
    }
}

void List_delete(T_list * L,int x){
    T_list * P;
    P = List_Search(L,x);
    if(P != NULL){
        P->prev->next = P->next;
        P->next->prev = P->prev;
        free(P);
    }
    else
        printf("the data is noexist\n");
}

void List_print(T_list * L ){
    T_list * P;
    P = L->next;
    while(P != NULL){
        printf("%d ",P->key);
        P = P->next;
    }
    printf("\n");
}

int main(void){
    int i,num;
    T_list * head;
    T_list * temp;
    head = malloc(sizeof(T_list));
    head->next = NULL;
    for(i = 0;i< 5;i++){
        scanf("%d",&num);
        List_insert(head,num);
    }
    List_print(head);
    List_delete(head,3);
    List_print(head);
    return 0;
}

单向链表

#include <stdio.h>
#include <stdlib.h>
struct T_list;
typedef struct T_list * List;
typedef List Position;
struct T_list{
    int key;
    Position next;
};

Position List_Search(List L,int k){
    List P;
    P = L->next;
    while(P != NULL && P->key != k)
        P = P->next;
    return P;
}

void List_insert(List L,int x){
    Position P;
    P = malloc(sizeof(struct T_list));
    P->key = x;
    P->next = L->next;
    L->next = P;
}

//找到的x元素之前的元素
void List_delete(List L,int x){
    Position P;
    P = L;
    while(P->next != NULL && P->next->key != x)
            P = P->next;
    if(P->next != NULL){
        Position de;
        de = P->next;
        P->next = de->next;
        free(de);
    }
    else
        printf("the list is empty\n");
}
void List_print(List L){
    Position P;
    P = L->next;
    while(P != NULL){
        printf("%d ",P->key);
        P = P->next;
    }
    printf("\n");
}
int main(void){
    int i,num;
    List head;
    head = malloc(sizeof(struct T_list));
    head->next = NULL;
    for(i = 0;i< 5;i++){
        scanf("%d",&num);
        List_insert(head,num);
    }
    List_print(head);
    List_delete(head,3);
    List_print(head);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值