单链表

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

typedef struct node
{
    char name[10];
    int score;
    struct node *next;
}Sqlist;

Sqlist *LinkList;

void List ()
{
    puts ("—————————— 创建链表请输入  1");
    puts ("—————————— 显示链表请输入  2");
    puts ("—————————— 插入元素请输入  3");
    puts ("—————————— 删除元素请输入  4");
    puts ("—————————— 查找元素请输入  5");
    puts ("—————————— 逆置链表请输入  6");
    puts ("—————————— 摧毁链表请输入  7");
}

void CreatList()
{
    Sqlist *pre, *p;
    LinkList = (Sqlist *) malloc (sizeof (Sqlist));///为链表开辟内存
    LinkList->next = NULL;
    pre = LinkList;

    int n;
    puts ("请输入要插入的元素个数");
    scanf ("%d", &n);

    puts ("请输入要插入的n的元素");

    for (int i=0; i<n; i++)
    {
        p = (Sqlist *) malloc (sizeof (Sqlist));
        scanf ("%s %d", p->name, &p->score);
        pre->next = p;
        pre = p;
    }
    p->next = NULL;
    puts ("创建链表完成!");
    return;
}

void ShowList ()
{
    Sqlist *p;
    p = LinkList->next;
    int length = 0;

    if (!p)
    {
        puts ("链表为空,请先创建链表!");
        return;
    }

    while (p)
    {
        printf ("%s %d\n", p->name, p->score);
        p = p->next;
        length++;
    }
    printf ("链表的长度为%d\n", length);
    return;
}

void Insert ()
{
    int n, i, j = 1;
    char str[10];
    Sqlist *p, *q;
    puts ("请输入要插入的姓名分数和要插入的位置!");
    scanf ("%s %d %d", str, &n, &i);

    p = LinkList->next;

    while (p && j<i-1)
    {///寻找第i-1个结点;
        p = p->next;
        j++;
    }

    if (!p || j>i)
    {
        puts ("第i个结点不存在");
        return;
    }
    q = (Sqlist *) malloc (sizeof (Sqlist));

    strcpy (q->name, str);
    q->score = n;

    q->next = p->next;
    p->next = q;
    puts ("插入元素成功");
    return;
}

void Delete ()
{
    Sqlist *p, *q;
    p = LinkList->next;

    int j = 1, i;
    puts ("请输入要删除的位置的元素");
    scanf ("%d", &i);

    while (p->next && j<i-1)
    {///寻找第i-1个结点;
        p = p->next;
        j++;
    }

    if (!(p->next) || j>i)
    {
        puts ("第i个结点不存在");
        return;
    }

    q = p->next;
    p->next = q->next;

    puts ("删除成功");
    return;
}

void Search ()
{
    int j = 1;
    char str[10];
    Sqlist *p;

    puts ("请输入要查找的学生的姓名!");

    scanf ("%s", str);
    p = LinkList->next;

    while (p)
    {
        if (strcmp (p->name, str) == 0)
        {
            printf ("要查找的元素的位置为%d\n", j);
            return;
        }

        p = p->next;
        j++;
    }

    if (!p)
    {
        puts ("查找失败");
        return;
    }
}

void Reverse ()
{
    Sqlist *pre, *t;

    if (!LinkList)
    {
        puts ("链表为空,请先创建链表");
        return;
    }

    pre = LinkList->next;
    LinkList->next = NULL;

    while (pre)
    {
        t = pre->next;
        pre->next = LinkList->next;
        LinkList->next = pre;
        pre = t;
    }
    puts ("链表逆置成功!");
    return;
}

void ClearList ()
{
    Sqlist *p, *q;
    p = LinkList->next;

    while (p)
    {
        q = p->next;
        free (p);
        p = q;
    }

    LinkList->next = NULL;
    puts ("摧毁链表成功");
}

int main ()
{
    int n;
    List ();

    while (1)
    {
        scanf ("%d", &n);
        switch(n)
        {
            case 1:{CreatList();
             break;}
            case 2:{ShowList(); break;}
            case 3:{Insert(); break;}
            case 4:{Delete(); break;}
            case 5:{Search(); break;}
            case 6:{Reverse(); break;}
            case 7:{ClearList(); break;}
            case 8:{puts ("程序结束"); break;}
            default: puts ("请输入有效指令");
        }
    }
    return 0;
}

你可能感兴趣的:(单链表)