【c语言数据结构】单链表的基本操作

该文详细介绍了如何使用C语言进行C程序的调试及单链表的基本操作,包括定义链表结构体、创建链表、在指定位置插入和删除节点以及正序和逆序显示链表内容。代码示例展示了完整的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、目的与要求
1.复习C程序调试、运行的基本操作方法。
2.熟练掌握编辑、编译、连接和运行C 程序的方法。
3.掌握单链表的定义、创建、插入和删除操作程序实现。
二、实验内容
1.定义单链表结构体,获取数据元素。
2.创建链表以输入-1 结束。
3.在第i个节点处插入数据元素X。
4.删除第i个节点。
5.显示链表中的数据元素。
6逆序显示链表中的数据元素

链表节点结构体的定义:

typedef struct Node
{
    int data;
    struct Node *next;
}Node;

创建节点:

Node *create_Node(int n)
{
    Node *node = (Node *)malloc(sizeof(Node));
    if(!node)
    {
        printf("内存不足!\n");
        return NULL;
    }
    node->data = n;
    node->next = NULL;
    return node;
} 

创建单链表:

Node *create_link()
{
    int n=0;
    Node *head = create_Node(0);
    if(!head)
        exit(-1);
    Node *tail;
    tail = head;
    scanf("%d", &n);
    while(n != -1)
    {
        Node *pnew = create_Node(n);
        if(!pnew)
            break;
        tail->next = pnew; //旧节点的指针指向新节点
        tail= pnew;       //新节点变为旧节点
        scanf("%d",&n);
    }
    printf("Input End\n");
    return head;
}

删除单链表:

//删除单链表,释放内存空间
void freeLink(Node *head)
{
    //1.判断链表是否为空,如果为空,则返回
    if(head==NULL)
        return;
    //2.如果非空,则逐个节点释放
    Node *p, *q;
    p = head;
    while(p->next!=NULL)
    {
        q = p->next;
        p->next = q->next;
        free(q);
    }
    free(head);
}

遍历单链表-----正序输出:

//遍历单链表--正序输出
void displayLink(Node *head)
{
    //1.判断链表是否为空,如果为空,则直接返回
    if(!head)
        return;
    //2.打印每一个节点信息
    Node *p = head->next;
    printf("链表正序输出为:\n");
    while(p!=NULL)
    {
        printf("%d\n", p->data);
        p = p->next;
    }
}

遍历单链表-----逆序输出:

//遍历单链表--逆序输出
void Inverse_displayLink(Node *head)
{
    // 1.判断链表是否为空,如果为空,则直接返回
    if (!head)
        return;
    // 2.打印每一个节点信息
    Node *p = head->next;
    int count = 0,i;
    int a[50] = {0};
    printf("链表倒序输出为:\n");
    while (p != NULL)
    {
        a[count] = p->data;
        p = p->next;
        count++;    
    }
    for (i = count-1; i>=0;i--)
    {
        printf("%d\n", a[i]);
    }
}

在指定位置插入节点:

//在指定位置插入节点
void insertNode(Node *head,Node *pnew,int i)
{
    //1.判断链表是否为空,如果为空,则拒绝插入
    if(!head)
        return;
    //2.判断插入的节点是否为空,如果为空,则拒绝插入
    if(!pnew)
        return;
    //3.遍历链表,找到插入位置
    Node *p = head;
    int n = 0;
    for (n = 0; n < i && p->next != NULL;n++)
    {
        p = p->next;
    }
    //4.插入新节点
    pnew->next = p->next;
    p->next = pnew;
}

在指定位置删除节点:

//在指定位置删除节点
void deleteNode(Node *head,int i)
{
    //1.判断链表是否为空,如果为空,则拒绝删除,直接返回
    if(!head)
        return;
    if(i<=0)
        return;
    //2.遍历链表,找到需要删除的位置
    Node *p = head,*q;
    int n = 0;
    for (n = 0; n < i && p->next != NULL;n++)
    {
        p = p->next;
    }
    if(!p)
        return;
    //3.删除找到的节点
    q = p->next;
    p->next = q->next;
    free(q);
}

全部代码:

/*
实现的内容有:
1.定义单链表结构体
2.创建链表以输入-1结束
3.在第i个节点处插入数据元素x
4.删除第i个节点
5.显示链表中的数据元素
6.逆序显示链表中的数据元素
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
    int data;
    struct Node *next;
}Node;

Node *create_Node(int n)
{
    Node *node = (Node *)malloc(sizeof(Node));
    if(!node)
    {
        printf("内存不足!\n");
        return NULL;
    }
    node->data = n;
    node->next = NULL;
    return node;
} 

Node *create_link()
{
    int n=0;
    Node *head = create_Node(0);
    if(!head)
        exit(-1);
    Node *tail;
    tail = head;
    scanf("%d", &n);
    while(n != -1)
    {
        Node *pnew = create_Node(n);
        if(!pnew)
            break;
        tail->next = pnew; //旧节点的指针指向新节点
        tail= pnew;       //新节点变为旧节点
        scanf("%d",&n);
    }
    printf("Input End\n");
    return head;
}
//删除单链表,释放内存空间
void freeLink(Node *head)
{
    //1.判断链表是否为空,如果为空,则返回
    if(head==NULL)
        return;
    //2.如果非空,则逐个节点释放
    Node *p, *q;
    p = head;
    while(p->next!=NULL)
    {
        q = p->next;
        p->next = q->next;
        free(q);
    }
    free(head);
}
//遍历单链表--正序输出
void displayLink(Node *head)
{
    //1.判断链表是否为空,如果为空,则直接返回
    if(!head)
        return;
    //2.打印每一个节点信息
    Node *p = head->next;
    printf("链表正序输出为:\n");
    while(p!=NULL)
    {
        printf("%d\n", p->data);
        p = p->next;
    }
}
//遍历单链表--逆序输出
void Inverse_displayLink(Node *head)
{
    // 1.判断链表是否为空,如果为空,则直接返回
    if (!head)
        return;
    // 2.打印每一个节点信息
    Node *p = head->next;
    int count = 0,i;
    int a[50] = {0};
    printf("链表倒序输出为:\n");
    while (p != NULL)
    {
        a[count] = p->data;
        p = p->next;
        count++;    
    }
    for (i = count-1; i>=0;i--)
    {
        printf("%d\n", a[i]);
    }
}
//在指定位置插入节点
void insertNode(Node *head,Node *pnew,int i)
{
    //1.判断链表是否为空,如果为空,则拒绝插入
    if(!head)
        return;
    //2.判断插入的节点是否为空,如果为空,则拒绝插入
    if(!pnew)
        return;
    //3.遍历链表,找到插入位置
    Node *p = head;
    int n = 0;
    for (n = 0; n < i && p->next != NULL;n++)
    {
        p = p->next;
    }
    //4.插入新节点
    pnew->next = p->next;
    p->next = pnew;
}
//在指定位置删除节点
void deleteNode(Node *head,int i)
{
    //1.判断链表是否为空,如果为空,则拒绝删除,直接返回
    if(!head)
        return;
    if(i<=0)
        return;
    //2.遍历链表,找到需要删除的位置
    Node *p = head,*q;
    int n = 0;
    for (n = 0; n < i && p->next != NULL;n++)
    {
        p = p->next;
    }
    if(!p)
        return;
    //3.删除找到的节点
    q = p->next;
    p->next = q->next;
    free(q);
}
int main()
{
    Node *head = create_link();
    displayLink(head);
    Inverse_displayLink(head);
    printf("插入新节点后的单链表\n");
    int n = 0;
    scanf("%d", &n);
    Node *pnew = create_Node(n);
    insertNode(head, pnew, 2);
    displayLink(head);
    printf("删除指定位置的节点之后的单链表\n");
    deleteNode(head, 2);
    displayLink(head);
    freeLink(head);
    system("pause");
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兜兜里有好多糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值