不带头节点链表的前插法,后插法,顺序添加法

这篇博客介绍了如何在不带头节点的链表中实现前插法、后插法和顺序添加节点的操作。通过C语言实现,包括添加、删除、查看和释放链表的功能。用户可以选择不同的操作并输入相应的数据,程序会动态地更新链表。

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

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

typedef struct student
{
    long id;
    char name[32];
    char sex[8];
    int age;
    float math;
    struct student *next;
}stu,*pstu;

stu *head;//全局指针变量默认初始化为NULL

stu *frontadd(stu *head);
stu *rearadd(stu *head);
stu *add(stu *head);
stu *delete(stu *head);
void print_list(stu *head);
void free_list(stu *head);

int main()
{
    int sel=0;
    char ch;
    while(1)
    {
        printf("\t1:前插法添加节点\n\t2:后插法添加节点\n\t3:添加顺序节点\n\t4:删除节点\n\t5:查看链表\n\t6:退出\n");
        printf("请输入选项:\n");
        scanf("%d",&sel);
        printf("%d\n",sel);
        //sleep(2);
        ch=getchar();//取出回车,避免循环
        //printf("%c\n",ch);
        switch(sel)
        {
            case 1:
                head=frontadd(head);
                break;
            case 2:
                head=rearadd(head);
                break;
            case 3:
                head=add(head);
                break;
            case 4:
                head=delete(head);
                break;
            case 5:
                print_list(head);
                break;
            case 6:
                //退出前释放内存
                free_list(head);
                exit(0);
            default:
                printf("reinput 1-6\n");        
        }
    }

    return 0;
}

stu *frontadd(stu *head)
{
    stu *s=NULL;
    //stu *p=NULL;
    char judge;
    while(1)
    {
        s=(stu *)malloc(sizeof(stu));
        s->next=NULL;
        printf("请输入学号:\n");
        scanf("%ld",&s->id);
        printf("请输入姓名:\n");
        scanf("%s",s->name);
        printf("请输入性别:\n");
        scanf("%s",s->sex);
        printf("请输入年龄:\n");
        scanf("%d",&s->age);
        printf("请输入数学成绩:\n");
        scanf("%f",&s->math);

        s->next=head;
        head=s;    

        while(getchar()!='\n');
        printf("是否继续添加?(y/n)");
        judge=getchar();
        if(judge=='y')
        {
            continue;
        }
        else
        {
            break;
        }
    }
    return head;
}

stu *rearadd(stu *head)
{
    stu *s=NULL;
    stu *p=NULL;
    stu *q=NULL;
    char judge;
    while(1)
    {
        s=(stu *)malloc(sizeof(stu));
        s->next=NULL;
        printf("请输入学号:\n");
        scanf("%ld",&s->id);
        printf("请输入姓名:\n");
        scanf("%s",s->name);
        printf("请输入性别:\n");
        scanf("%s",s->sex);
        printf("请输入年龄:\n");
        scanf("%d",&s->age);
        printf("请输入数学成绩:\n");
        scanf("%f",&s->math);

        //添加节点
        if(NULL==head)
        {
            head=s;
        }
        else
        {
            p=head;
            while(p!=NULL)
            {
                q=p;
                p=p->next;
            }
            q->next=s;
        }

        while(getchar()!='\n');
        printf("是否继续添加?(y/n)");
        judge=getchar();
        if(judge=='y')
        {
            continue;
        }
        else
        {
            break;
        }
    }
    return head;
}

stu *add(stu *head)
{
    stu *s=NULL;
    stu *p=NULL;
    stu *q=NULL;
    char judge;
    while(1)
    {
        s=(stu *)malloc(sizeof(stu));
        s->next=NULL;
        printf("请输入学号:\n");
        scanf("%ld",&s->id);
        printf("请输入姓名:\n");
        scanf("%s",s->name);
        printf("请输入性别:\n");
        scanf("%s",s->sex);
        printf("请输入年龄:\n");
        scanf("%d",&s->age);
        printf("请输入数学成绩:\n");
        scanf("%f",&s->math);

        //添加节点
        if(NULL==head)
        {
            head=s;
        }
        else
        {
            if(s->id<head->id)
            {
                s->next=head;
                head=s;
            }
            else
            {
                p=head;//每次查找都需要从链表头开始
                while((p!=NULL)&&(s->id>p->id))
                {
                    q=p;
                    p=p->next;
                }

                q->next=s;
                s->next=p;            
            }
        }

        while(getchar()!='\n');
        printf("是否继续添加?(y/n)");
        judge=getchar();
        if(judge=='y')
        {
            continue;
        }
        else
        {
            break;
        }
    }
    return head;
}

stu *delete(stu *head)
{
    if(NULL==head)
    {
        printf("list empty\n");
        return NULL;
    }
    char judge;
    stu *p=NULL;
    stu *q=NULL;
    long num=0;
    while(1)
    {
        printf("请输入删除的节点数据:\n");
        scanf("%ld",&num);
        p=head;
        if(NULL==head)
        {
            printf("list empty\n");
            return NULL;
        }
        if(head->id==num)
        {
            p=head;
            head=head->next;
            free(p);
            p=NULL;
        }
        else
        {
            while((p!=NULL)&&(p->id!=num))
            {
                q=p;
                p=p->next;
            }
        
            if(NULL==p)
            {
                printf("%ld not in list\n",num);
                break;
            }
            else
            {
                q->next=p->next;
                free(p);
                p=NULL;
            }
        }
    
        while(getchar()!='\n');
        printf("是否继续删除(y/n):");
        judge=getchar();
        if(judge=='y')
        {
            continue;
        }
        else
        {
            break;
        }
    }

    return head;
}

void print_list(stu *head)
{
    if(NULL==head)
    {
        printf("list empty\n");
        return;
    }
    stu *p=NULL;
    p=head;

    printf("学号 姓名 性别 年龄 数学\n");
    while(p!=NULL)
    {
        printf("%ld %s %s %d %.2f\n",p->id,p->name,p->sex,p->age,p->math);
        p=p->next;
    }

    return;
}

void free_list(stu *head)
{
    if(NULL==head)
    {
        printf("list empty\n");
        return;
    }
    stu *p=NULL;
    stu *q=NULL;
    p=head;
    while(p!=NULL)
    {
        q=p;//p指针移动之前先保存起来
        p=p->next;
        free(q);
        q=NULL;
    }
    head=NULL;
}
### 带头节点的双向链表插法 对于带头节点的双向链表,在使用插法创建节点时,需要特别注意更新前后指针的关系。以下是具体实现过程: #### 创建双向链表结构体 首先定义双向链表节点结构体[^3]。 ```c struct Node { int data; struct Node* prev; struct Node* next; }; ``` #### 初始化空链表 初始化一个指向`NULL`的指针表示当前为空链表。 ```c struct Node* head = NULL; ``` #### 插入函数实现 当采用插法插入新的节点时,需考虑两种情形:一是链表为空;二是链表已有至少一个节点存在。无论哪种情况,都需要确保新加入的节点成为新的部,并正确设置其前驱(`prev`)和后继(`next`)指针[^1]。 ```c void insertAtHead(struct Node** head_ref, int new_data) { // 分配内存空间并填充数据 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = new_data; // 如果链表为空,则直接将此节点设为首尾两端 if (*head_ref == NULL) { newNode->prev = NULL; newNode->next = NULL; *head_ref = newNode; } else { // 更新现有第一个节点之前的链接至新节点 (*head_ref)->prev = newNode; // 新节点应连接到旧的第一个节点之后 newNode->next = *head_ref; newNode->prev = NULL; // 移动指针指向最新插入的新节点 *head_ref = newNode; } } ``` 通过上述代码片段实现了在带哑元(哨兵)节点的情况下利用插法构建双向链表的功能。每次调用 `insertAtHead()` 函数都会使传入的数据项作为最新的部元素被加进来,同时保持整个列表内部各节点间正确的顺序关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值