单链表的操作(C语言描述)

单链表的操作(*C语言描述*

线性表的链式存储结构是用一组任意的存储单元保存线性表的数据元素。

为了操作方便,我们在创建链表时,常采用有头结点的链表。

头结点:

  • ### 头结点是为了操作的统一与方便而设立的,放在第一个元素结点之前,其数据域一般无意义(当然有些情况下也可存放链表的长度、用做监视哨等等)。
  • ### 有了头结点后,对在第一个元素结点前插入结点和删除第一个结点,其操作与对其它结点的操作统一了。
  • ### 首元结点也就是第一个元素的结点,它是头结点后边的第一个结点。
  • ### 头结点不是链表所必需的。

以学生信息管理为例

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

typedef struct Student_Information{
    int num;
    char name[5];
    double marks;
}Stu_In;

typedef struct Student {
    Stu_In s;
    int size;
    struct Student *next;
}Stu;

Stu_In input()//输入结点信息
{
    Stu_In s;
    printf("input information\n");
    scanf("%d %s %lf", &s.num, s.name, &s.marks);
    return s;
}

Stu *Create_End() //有头结点尾插法创建链表
{
    int b;
    Stu *head, *temp, *p;
    head = (Stu*)malloc(sizeof(Stu));
    head->size = 0;
    p = head;           //让p指向头节点
    while(1)
    {
        b = 1;
        printf("1 or 0\n");
        scanf("%d", &b);
        if(b == 0)
            break;
        temp = (Stu*)malloc(sizeof(Stu));
        temp->s = input();
        p->next = temp;        //把新节点添加到表尾
        p = temp;           // (p = p->next;)把指针指向新节点
        head->size++;
    }
    p->next = NULL;            //尾节点的指针域置空
    return head;
}

Stu *Create_Front() //头插法创建链表
{
    int b;
    Stu *head, *p;
    head = (Stu*)malloc(sizeof(Stu));
    head->size = 0;
    while(1)
    {
        b = 1;
        printf("1 or 0\n");
        scanf("%d", &b);
        if(b == 0)
            break;
        p = (Stu*)malloc(sizeof(Stu));
        p->s = input();
        p->next = head->next;
        head->next = p;
        head->size++;
    }
    return head;
}

Stu* Insert(Stu* L)//将新节点插入指定结点后
{
    int Lc;
    printf("please input number\n");
    scanf("%d", &Lc);
    Stu *p, *temp;
    p = L;
    temp = (Stu*)malloc(sizeof(Stu));
    printf("please input insert data\n");
    temp->s = input();
    while(Lc > 0){
        p = p->next;
        Lc--;
    }
    temp->next = p->next;
    p->next = temp;
    L->size++;
    return L;
}

Stu* Delete(Stu* L)//删除指定结点
{
    int Lc;
    Stu *p, *p1;
    printf("please input number\n");
    scanf("%d", &Lc);
    p = L;
    while(Lc > 0){
        p1 = p;
        p = p->next;
        Lc--;
    }
    p1->next = p->next;
    L->size--;
    free(p);
    return L;
}

Stu* Sort(Stu *L)//按成绩倒序
{
    Stu *p, *p1, *temp;
    temp = (Stu*)malloc(sizeof(Stu));
    p = L;
    p = p->next;
    int i, j;
    for(i = 0; i < L->size; i++){
        p = L->next;
    for(j = 0; j < L->size - i - 1; j++){
    if(p->s.marks < p->next->s.marks){
        temp->s = p->s;
        p->s = p->next->s;
        p->next->s = temp->s;
    }
    p = p->next;
    }
    }
    free(temp);
    return L;
}

Stu* Change_Data(Stu* L)//修改数据
{
    int Lc;
    Stu *p = L;
    printf("please input number\n");
    scanf("%d", &Lc);
    while(Lc > 0){
        p = p->next;
        Lc--;
    }
    printf("please input new data\n");
    p->s = input();
    return L;
}

void* Num_Seek(Stu* L)//按学号寻找
{
    int Lc;
    printf("please input number\n");
    scanf("%d", &Lc);
    while(L->s.num != Lc){
        L = L->next;
    }
    printf("%d, %s, %lf\n", L->s.num, L->s.name, L->s.marks);
}

void Display(Stu *L)//遍历并显示链表信息
{
    printf("%d\n", L->size);
    L = L->next;
    while(L != NULL){
        printf("%d, %s, %lf\n", L->s.num, L->s.name, L->s.marks);
        L = L->next;
    }
}

void Select(Stu *H)
{
    int i = 1;
    while(i != 0){
        printf("  please input 0 - 8\n");
        scanf("%d", &i);
        switch(i){
        case 1:
            H = Create_End();
            break;
        case 2:
            H = Create_Front();
            break;
        case 3:
            H = Insert(H);
            break;
        case 4:
            H = Delete(H);
            break;
        case 5:
            H = Sort(H);
            break;
        case 6:
            H = Change_Data(H);
            break;
        case 7:
            Num_Seek(H);
            break;
        case 8:
            Display(H);
            break;
        case 0:
            break;
        default:
            printf("please input correct number\n");
            }
    }
}

void Menu()
{
}
int main()
{
    Stu *H;
    H = (Stu*)malloc(sizeof(Stu));
    Select(H);
    return 0;
}
/**创建链表时数据
1
1 zwn 1.1
1
2 cpq 2.2
1
3 sxq 3.3
1
4 llx 4.4
1
5 zwl 5.5
1
6 chn 6.6
0
 *
 */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值