数据结构 ---- 链表操作 插入、查询、删除、判空、获取大小等操作

/*
1.实现一个表ADT的数据结构,可以进行插入、查询、删除、判空、获取大小等操作
2.底层使用链表存储
*/

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

//函数原型声明
struct Node* createHeaderNode();

void insertFirst(struct Node* header,int x);
void insertLast(struct Node* header,int x);
struct Node* find(struct Node* header,int x);
struct Node* findKth(struct Node* header,int x);
void delete(struct Node* header,int x);
bool isEmpty(struct Node* header);
int size(struct Node* header);
void printList(struct Node* header);

struct Node
{
    int element;        //结点中值
    struct Node* next;  //指向下一个结点的指针
};

int main(void)
{
    struct Node* node;
    struct Node* header;

    //创建表头(哑结点)
    header = createHeaderNode();

    //判断表是否为空表
    printf("List isEmpty?:%d\n",isEmpty(header));
    printf("================================\n");

    //从表头部插入
    insertFirst(header,1);
    insertFirst(header,2);

    //从表尾部插入
    insertLast(header, 3);
    insertLast(header, 4);

    printList(header);
    printf("================================\n");

    printf("List isEmpty?:%d\n",isEmpty(header));
    printf("================================\n");

    //查找结点值为3的结点
    node = find(header,3);
    if(node == NULL)
    {
        printf("not find...\n");
    }
    else
    {
        printf("find node element: %d\n",node->element);
    }
    printf("================================\n");

    //查找表中第2个节点
    node = findKth(header,2);
    if(node == NULL)
    {
        printf("not find...\n");
    }
    else
    {
        printf("find node element: %d\n",node->element);
    }
    printf("================================\n");

    //删除结点值为3的结点
    delete(header,3);
    printList(header);
    printf("================================\n");

    //打印表中结点个数,即表的大小
    printf("List size: %d\n",size(header));
    printf("================================\n");
    
}

//创建表头(哑结点)
struct Node* createHeaderNode()
{
    struct Node* p;
    p = malloc(sizeof(struct Node));
    if(p == NULL)
    {
        printf("Space is run out!\n");
        exit(1);
    }

    p -> next = NULL;
    return p;
}

//判断表是否为空
bool isEmpty(struct Node* header)
{
    return header -> next == NULL;
}

//从表的头部插入
void insertFirst(struct Node* header, int x)
{
    struct Node* tmp;
    tmp = malloc(sizeof(struct Node));

    if(tmp == NULL)
    {
        printf("Space is run out!\n");
        return;
    }

    tmp -> element = x;    //给结点赋值
    tmp -> next = header -> next;
    header->next = tmp;
    return;
}

//在表的尾部插入
void insertLast(struct Node* header, int x)
{
    struct Node* p;
    struct Node* tmp;

    tmp = malloc(sizeof(struct Node));

    if(tmp == NULL)
    {
        printf("Space is run out!\n");
        return;
    }

    tmp->element = x;   //给结点赋值
    tmp->next = NULL;
     
    p = header;
    while(p->next != NULL)
    {
        p = p->next;
    }
    p->next = tmp;
    return;
}

//查找x,如果没找到返回NULL
struct Node* find(struct Node* header, int x)
{
    struct Node* p;
    p = header->next;
    while(p != NULL && p->element != x)
    {
        p = p->next;
    }

    return p;
}

//查找位置为position的结点,如果没找到返回NULL
struct Node* findKth(struct Node* header, int position)
{
    int count = 1;
    struct Node* p;

    if(position <= 0)
    {
        printf("position can't -1 or etc\n");
        return NULL;
    }

    p = header->next;
    while(p != NULL)
    {
        if(count == position)
        {
            return p;
        }
        p = p->next;
        count++;
    }
    return NULL;
}

//打印表中的结点数据
void printList(struct Node* header)
{
    struct Node* p;
    p = header->next;
    while(p != NULL)
    {
        printf("node element = %d\n",p->element);
        p = p->next;
    }
    return;
}

//表中结点个数
int size(struct Node* header)
{
    int count = 0;
    struct Node* p;
    
    p = header->next;
    while(p != NULL)
    {
        count++;
        p = p->next;
    }
    return count;
}

//删除第一个值匹配的结点
void delete(struct Node* header, int x)
{
    struct Node* privious;  //被删除结点前一个结点的指针
    struct  Node* p;

    privious = header;
    p = header->next;

    while(p != NULL)
    {
        if(p->element == x)
        {
            privious->next = p->next;
            free(p);

            break;
        }
        else
        {
            privious = p;
            p = p->next;
        }
    }
    return;
    
}

结果:

List isEmpty?:1
================================
node element = 2
node element = 1
node element = 3
node element = 4
================================
List isEmpty?:0
================================
find node element: 3
================================
find node element: 1
================================
node element = 2
node element = 1
node element = 4
================================
List size: 3
================================

结构体创建:

struct Node
{
    int element;        //结点中值
    struct Node* next;  //指向下一个结点的指针
};

创建表头:

//创建表头(哑结点)
struct Node* createHeaderNode()
{
    struct Node* p;
    p = malloc(sizeof(struct Node));
    if(p == NULL)
    {
        printf("Space is run out!\n");
        exit(1);
    }

    p -> next = NULL;
    return p;
}

判断表是否为空:

bool isEmpty(struct Node* header)
{
    return header -> next == NULL;
}

从表的头部插入:

void insertFirst(struct Node* header, int x)
{
    struct Node* tmp;
    tmp = malloc(sizeof(struct Node));

    if(tmp == NULL)
    {
        printf("Space is run out!\n");
        return;
    }

    tmp -> element = x;    //给结点赋值
    tmp -> next = header -> next;
    header->next = tmp;
    return;
}

在表的尾部插入:

void insertLast(struct Node* header, int x)
{
    struct Node* p;
    struct Node* tmp;

    tmp = malloc(sizeof(struct Node));

    if(tmp == NULL)
    {
        printf("Space is run out!\n");
        return;
    }

    tmp->element = x;   //给结点赋值
    tmp->next = NULL;
     
    p = header;
    while(p->next != NULL)
    {
        p = p->next;
    }
    p->next = tmp;
    return;
}

查找x,如果没找到返回NULL:

struct Node* find(struct Node* header, int x)
{
    struct Node* p;
    p = header->next;
    while(p != NULL && p->element != x)
    {
        p = p->next;
    }

    return p;
}

查找位置为position的结点,如果没找到返回NULL:

struct Node* findKth(struct Node* header, int position)
{
    int count = 1;
    struct Node* p;

    if(position <= 0)
    {
        printf("position can't -1 or etc\n");
        return NULL;
    }

    p = header->next;
    while(p != NULL)
    {
        if(count == position)
        {
            return p;
        }
        p = p->next;
        count++;
    }
    return NULL;
}

打印表中的结点数据:

void printList(struct Node* header)
{
    struct Node* p;
    p = header->next;
    while(p != NULL)
    {
        printf("node element = %d\n",p->element);
        p = p->next;
    }
    return;
}

表中结点个数:

int size(struct Node* header)
{
    int count = 0;
    struct Node* p;
    
    p = header->next;
    while(p != NULL)
    {
        count++;
        p = p->next;
    }
    return count;
}

删除第一个值匹配的结点:

void delete(struct Node* header, int x)
{
    struct Node* privious;  //被删除结点前一个结点的指针
    struct  Node* p;

    privious = header;
    p = header->next;

    while(p != NULL)
    {
        if(p->element == x)
        {
            privious->next = p->next;
            free(p);

            break;
        }
        else
        {
            privious = p;
            p = p->next;
        }
    }
    return;
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值