数据结构算法练习

Posted 黑胡子大叔的小屋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构算法练习相关的知识,希望对你有一定的参考价值。

数据结构与算法

链表

顺序表

#include <stdio.h>
#define MAX_LEN 20

struct ARRAY_LIST
    int index;
    int data[MAX_LEN];
;

void init(struct ARRAY_LIST *arr)

    if(NULL == arr)
        return;
    
    arr->index = 0;



void add(struct ARRAY_LIST *arr,int value)

    if(arr->index == MAX_LEN)
        printf("顺序表已满,无法插入!\\n");
        return;
    
    arr->data[arr->index] = value;
    arr->index ++;



void out(struct ARRAY_LIST *arr)

    int i;
    i = 0;
    while (i < arr->index)
    
        printf("%d\\n",arr->data[i]);
        i++;
    




int main()

    struct ARRAY_LIST arr;

    init(&arr);

    add(&arr,1);
    add(&arr,2);
    add(&arr,3);
    add(&arr,4);

    out(&arr);
    

链表

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

struct LINKNODE

    int value;
    struct LINKNODE *next;
;

int headadd(struct LINKNODE *head,int value)

    struct LINKNODE *node;
    struct LINKNODE *p;

    p = head->next;
    node = malloc(sizeof(struct LINKNODE));
    node->value = value;
    head->next = node;
    node->next = p;



int out(struct LINKNODE *linklist)

    if(linklist == NULL)
        printf("链表为空");
        return 0;
    

    struct LINKNODE *node;

    node = linklist->next;
    while (node!=NULL)
    
        printf("%d\\n",node->value);
        node = node->next;
    



struct LINKNODE *init(struct LINKNODE *head)

    struct LINKNODE *p;

    p = malloc(sizeof(struct LINKNODE));
    if(p == NULL)
        printf("Fail to malloc memory!\\n");
        return NULL;
    

    head = p;
    p->next = NULL;
    
    return head;


int delete(struct LINKNODE **head)

    struct LINKNODE *p;

    while((*head) != NULL)

        p = *head;
        *head = (*head)->next;
        free(p);

    



struct LINKNODE *add(struct LINKNODE *head,int value)

    struct LINKNODE *node;
    struct LINKNODE *p;

    node = malloc(sizeof(struct LINKNODE));
    node->value = value;
    node->next = NULL;

    p = head;
    while(p->next != NULL)
        p = p->next;
    
    p->next = node;

    return head;



int add2(struct LINKNODE **head,int value)

    struct LINKNODE *node;
    struct LINKNODE *p;

    node = malloc(sizeof(struct LINKNODE));
    node->value = value;

    p = (*head)->next;
    (*head)->next = node;
    node->next = p;



int main()

    struct LINKNODE *head;  //头结点

    head = init(head);
    printf("开始添加\\n");
    // head = add(head,1);
    // head = add(head,2);
    // head = add(head,3);
    add2(&head,1);
    add2(&head,2);
    add2(&head,3);
    printf("添加完成\\n");
    out(head);
    delete(&head);
    printf("释放完成\\n");
    out(head);


链表实例

删除链表中奇数结点

/**
 * @file        t1.c
 * @brief       释放链表中奇数结点
 */
#include <stdio.h>
#include <stdlib.h>

struct LINKNODE
    struct LINKNODE *next;
    int data;
;

void init(struct LINKNODE **head)

    *head = malloc(sizeof(struct LINKNODE));
    (*head)->next = NULL;
    (*head)->data = 0;


void add(struct LINKNODE *head,int data)

    struct LINKNODE *p;
    struct LINKNODE *node;
    
    p = head->next;
    node = malloc(sizeof(struct LINKNODE));
    node->data = data;
    head->next = node;
    node->next = p;


void delood(struct LINKNODE *head)

    struct LINKNODE *p;
    struct LINKNODE *pre;

    p = head->next;
    pre = head;

    while(p != NULL)
        if(p->data % 2 != 0)
            //释放
            printf("释放 %d\\n",p->data);
            pre->next = p->next;
            free(p);
            p = NULL;
            p = pre;
        
        pre = p;
        p = p->next;
    


void show(struct LINKNODE *head)

    struct LINKNODE *p;

    if(head->next == NULL)
        printf("链表为空\\n");
        return;
    

    p = head->next;
    while(p != NULL)
        printf("data :%d\\n",p->data);
        p = p->next;
    


int main()

    struct LINKNODE *head;

    init(&head);
    printf("初始化完成\\n");

    add(head,1);
    add(head,2);
    add(head,3);
    add(head,4);
    add(head,5);
    add(head,6);
    add(head,7);
    add(head,9);
    
    delood(head);

    show(head);


栈部分

顺序栈

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxSize 10
typedef struct arraystack

    int Data[MaxSize];   // 存储元素的数组
    int topIdx;       //栈顶指针
Node;
//入栈
void push(struct arraystack *node,int value)
    node->Data[node->topIdx] = value;
    node->topIdx++;

//弹栈
void pop(Node node)
    node.topIdx--;

//栈展示
void show(Node node)
    int i;
    for(i = 0; i < node.topIdx; i++)
        printf("value: %d \\n",node.Data[i]);
    

//判断栈空      返回0为空  非0为不空
int stackempty(Node node)
    printf("node.topIdx:%d\\n",node.topIdx);
    if(node.topIdx != 0)
        return 1;
    
    return 0;

//判断栈满      返回0为满 非零为不满
int stackfull(Node node)
    if(node.topIdx != MaxSize)
        return 1;
    
    return 0;

int main()
    Node node;
    node.topIdx = 0;
    int value,j;
    int empty,full;
    int num;
    empty = stackempty(node);
    if(empty == 0)
        printf("栈空!\\n");
    else
        printf("栈非空:%d\\n",empty);
    
    printf("开始键入个数:\\n");
    scanf("%d",&num);
    for(j = 0; j < num;j++)
        full = stackfull(node);
        if(full == 0)
            printf("栈满!topindex为:%d\\n",node.topIdx);
        else
            printf("请输入:");
            scanf("%d",&value);
            push(&node,value);
        
    
    empty = stackempty(node);
    if(empty != 0)
        printf("栈不空!\\n");
    

    show(node);

队列

顺序队列,一般写法,存在假溢出的问题

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxLen 20
typedef struct Query
    int data[MaxLen];
    int front;
    int rear;
*Q;
//初始化队列
void Init(Q query)
    query->front=0;
    query->rear=0;

//入队
int InQuery(Q query,int value)
    int ret = 0;

    if(query->rear == MaxLen)
        printf("队列满");
        ret = 1;
    
    printf("入队:%d\\n",value);
    query->data[query->rear++] = value;
error:
    return ret;

//出队
int OutQuery(Q query)
    int ret = 0;
    printf("出队数据:%d \\n",query->data[query->front]);
    if(quer->rear == 0)
        printf("队列空!");
        ret = 1;
        goto error;
    
    query->front ++;
error:
    return ret;

//输出
void Show(Q query)
    int i;
    for(i = query->front;i < query->rear; i++)
        printf("输出[%d]: %d \\n",i,query->data[i]);
    

int main()
    struct Query query;
    Init(&query);
    InQuery(&query,1);    
    InQuery(&query,2);    
    InQuery(&query,3);    
    InQuery(&query,4);    
    InQuery(&query,5);
    printf("出队\\n");    
    OutQuery(&query);
    Show(&query);

顺序循环队列

/**
 * @file        arrayquery.c
 * @brief       顺序队列
 *              队列,先进先出
 * @author      UncleBb
 * @version     0.0.0.1
 * @date        2021/08/19
 */
#include <stdio.h>
#define MAX_LEN 5

#ifndef TRUE
#define TRUE                        1U                          //真
#endif

#ifndef FALSE
#define FALSE                       0U                          //假
#endif

struct ARRAYQUERY
    int data[MAX_LEN];
    int front;
    int rear;
;

//初始化
void init(struct ARRAYQUERY *query)

    query->front = 0;
    query->rear = 0;




//判断队满
int isFull(struct ARRAYQUERY *query)

    if((query->rear + 1) % MAX_LEN == query->front)

        printf("队满\\n");
        return TRUE;

    

    return FALSE;



//判断队空
int isEmpty(struct ARRAYQUERY *query)
    
    if(query->rear == query->front)

        printf("队列空\\n");
        return TRUE;

    

    return FALSE;


//入队
void in(struct ARRAYQUERY *query,int value)

    if(isFull(query))
        return;
    
    printf("入队数据:%d\\n",value);
    query->data[query->rear] 以上是关于数据结构算法练习的主要内容,如果未能解决你的问题,请参考以下文章

字符串练习题: 拓扑结构相同子树

Python算法练习--把搜索树转成双向链表

数据结构之二叉树基础OJ练习另一颗树的子树

二叉树基础练习

数据结构与算法之深入解析“二叉搜索子树的最大键值和”的求解思路与算法示例

数据结构与算法二叉树——另一棵树的子树