个人博客页面链接:http://www.shihao.online/(django搭建的个人博客,还在完善中)

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

#define true 1
#define false 0
int s = 0;      //定义全局变量s接收查找数据的位置

//结点类型定义
typedef struct linknode
{
    char data;
    struct linknode *next;
}Node;

//构造链表(带头结点的尾插法建表)
Node* create_list()
{
    int len, i, t;    //len为链表长度, t用来接收输入值
    Node *p, *tail;   //定义p用来创建结点,tail指向链表的尾节点
    Node *head = (Node *)malloc(sizeof(Node));   //生成头结点
    tail = head;         //尾结点的初值指向头结点
    printf("请输入你要创建的链表的长度:");
    scanf("%d",&len);
    getchar();
    for (i = 0; i < len; i++)
    {
        printf("请输入第%d个结点数据域的值:",i+1);
        scanf("%c",&t);
        getchar();
        p = (Node *)malloc(sizeof(Node));
        p->data = t;
        tail->next = p;
        tail = p;
    }
    tail->next = NULL;
    printf("链表建立成功!\n");
    return head;
}

//输出链表
void output_list(Node *head)
{
    Node *p = head->next;
    while (p != NULL)
    {
        printf("%c ",p->data);
        p = p->next;
    }
    printf("\n");
}

//求链表长度
int length_list(Node *head)
{
    Node *p = head;
    int len = 0;
    while (p->next != NULL)
    {
        len++;
        p=p->next;
    }
    return len;
}

//判断链表是否为空
int empty_list(Node *head)
{
    if (head->next == NULL)
        return true;
    else
        return false;
}

//删除链表中某个元素
int delete_list(Node *head, int pos, char *val)  // pos(从1开始)代表你要删除链表中第几个元素
{                                                // 指针val用来存放删除的数值
    int i;
    Node *p = NULL;
    Node *t = NULL;
    if (empty_list(head) || pos > length_list(head))
        return false;
    p = head;
    for (i = 0; i < pos - 1; i++)   //找到第pos-1个位置的结点
        p = p->next;
    t = p->next;              //用t记住p,结束后释放掉,防止内存泄漏
    *val = p->next->data;           //保存删除的数值
    p->next = p->next->next;        //指向下一结点
    free(t);
    return true;
}

//插入某个元素
int insert_list(Node *head, int pos, char data)
{
    int i;
    Node *p = NULL;
    Node *t = NULL;
    if (pos > length_list(head) + 1 || empty_list(head))
        return false;
    p = head;
    for (i = 0; i < pos - 1; i++)
        p = p->next;
    t = (Node *)malloc(sizeof(Node));
    t->data = data;
    t->next = p->next;
    p->next = t;
    return true;
}

//在链表中按值查找
int search_list(Node *head, char data) //在链表中查找和data相同的数
{
    int i = 1;
    Node *p = NULL;
    if (empty_list(head))
    {
        printf("链表为空\n");
        return false;
    }
    p = head->next;
    while (p != NULL && p->data != data)
    {
        i++;
        p = p->next;
    }
    if (p != NULL)
    {
        printf("在第%d个位置找到%c\n", i, data);
        s = i;
        return true;
    }
    printf("未找到值为%c的点\n",data);
    return false;
}

int main()
{
    int ch;
    Node *head;
    printf(" 线性表子系统\n");
    printf("**************************************************\n");
    printf("* 1-----建表 *\n");
    printf("* 2-----插入 *\n");
    printf("* 3-----删除 *\n");
    printf("* 4-----显示 *\n");
    printf("* 5-----查找 *\n");
    printf("* 6-----求表长 *\n");
    printf("* 0-----返回 *\n");
    printf("**************************************************\n");
    while (1)
    {
        printf("\n***************************************************\n");
        printf("请输入你要进行的操作对应的数字:");

        scanf("%d",&ch);
        getchar();

        switch(ch)
        {
            case 1:                          //建表
            {
                head = create_list();
                break;
            }
            case 2:                          //插入
            {
                int pos;
                char data;
                printf("请输入你要插入的数据的位置:");
                scanf("%d",&pos);
                getchar();
                printf("请输入你要插入数据的值:");
                scanf("%c",&data);
                getchar();
                if(insert_list(head, pos, data) == 0)
                    printf("输入的位置错误\n");
                else
                {
                    printf("插入后的链表的长度为:");
                    printf("%d\n",length_list(head));
                    printf("链表遍历为:");
                    output_list(head);
                }
                break;
            }
            case 3:                         //删除
            {
                char data;
                printf("请输入你要删除的数据的值: ");
                scanf("%c",&data);
                getchar();
                if (search_list(head, data) == 0)      //查找要删除的数据的位置,判断是否查找成功
                    break;
                delete_list(head, s, &data);   //s为全局变量,存放的是查找的数据的位置。
                printf("删除后的链表的长度为: ");
                printf("%d\n",length_list(head));
                if (empty_list(head))       //判断链表删除后是否为空
                {
                    printf("链表为空\n");
                    break;
                }
                printf("链表遍历为: ");
                output_list(head);
                break;
            }
            case 4:                //显示
            {
                if (empty_list(head))
                {
                    printf("链表为空\n");
                    break;
                }
                printf("链表遍历为: ");
                output_list(head);
                break;
            }
            case 5:                //查找
            {
                char data;
                printf("请输入你要查找的数据的值:");
                scanf("%c",&data);
                getchar();
                search_list(head, data);
                break;
            }
            case 6:                 //求链表长度
            {
                printf("链表的长度为:%d\n",length_list(head));
                break;
            }
            case 0:                 //输入0结束
            {
                return 0;
            }
        }
    }
}