单链表的创建、插入、删除

看网上资料,然后写的关于单链表的创建、插入、删除等方面的代码练练手,代码存在一些bug,仅供参考。

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

//1.定义链表数据结构
struct node 
{
    int num;
    struct node *next;
};

//函数声明
struct node *creat(struct node *head);
void print(struct node *head);
struct node *del(struct node *head,int num);
struct node *insert(struct node *head,int num,int count);
int n = 0;  //节点的个数

int main()
{
    int num = 0,count = 0;
    struct node *head;
    head = NULL;        //2.创建一个空表

    //创建
    printf("************************创建**********************\n");
    head = creat(head); //创建单链表
    print(head);        //打印单链表

    //删除
    printf("************************删除**********************\n");
    printf("输入要删除的数据:\n");
    scanf("%d",&num); 
    printf("num is %d\n",num);
    head = del(head,num);
    print(head); 

    //插入
    while(1)
    {
        printf("*************************插入*********************\n");
        printf("请输入要插入的位置:1---%d:::::",n+1);
        scanf("%d",&count);
        printf("请输入要插入的数据:");
        scanf("%d",&num);
        head = insert(head,num,count);
        print(head); 
    }
    return 0;
}


//链表的创建
struct node *creat(struct node *head)
{
    struct node *p1,*p2;

    //3.利用malloc函数向系统申请节点
    p1 = p2 = (struct node *)malloc(sizeof(struct node)); //申请节点
    printf("输入大于0的值,小于等于0则结束,值的存放地址为:p1_addr = %d\n",p1);
    scanf("%d",&p1->num);
    p1->next = NULL;
    while(p1->num > 0)
    {
        if(head == NULL)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;

        p1 = (struct node *)malloc(sizeof(struct node));
        p1->next = NULL;//置空
        n++;
        printf("输入大于0的值,小于等于0则结束,值的存放地址为:p%d_addr = %d\n",n,p1);
        scanf("%d",&p1->num);
    }

    free(p1); 
    p1 = NULL;
    p2->next = NULL;
    printf("输入结束\n");
    return head;
}

//链表的遍历打印
void print(struct node *head)
{

    struct node *tmp;

    tmp = head;

    printf("链表打印开始!!!\n");
    printf("节点个数为:%d\n",n);
    while(tmp != NULL)
    {
        printf("输入的值为:num = %d,地址为:addr = %d\n",tmp->num,tmp);
        tmp = tmp->next;
    }
    printf("链表打印结束!!!\n");
}

//链表的删除
struct node *del(struct node *head,int num)
{
    struct node *p1,*p2;

    if(head == NULL)
    {
        printf("链表为空链表!!!\n");
        return head;
    }

    p1 = head;

    //开始查找
    while((num != p1->num) && (p1->next != NULL))
    {
        p2 = p1;         //把p1节点赋给p2
        p1 = p1->next;   //p1后移一个节点
    }

    if(p1->num == num)  //找到此数字
    {
        if(p1 == head)  //若p1是首结点
            head = p1->next;
        else
        {
            p2->next = p1->next;  //把要删除的p1所指向的下一个
            free(p1);
        }
        n--;  //节点数减1
    }
    else               //没有找到这个数字
    {
        printf("没有发现%d\n",num);
    }

    return head;
}

//链表的插入
struct node *insert(struct node *head,int num,int count)
{
    struct node *p0,*p1,*p2;

    p1 = (struct node *)malloc(sizeof(struct node));
    p1->num = num;
    p1->next = NULL;

    p0 = head;
    p2 = p0->next;

    //查找要插入的位置
    if(count == n + 1)  //插入尾部
    {
        while(p0->next != NULL)  //找到链表最后面
        {
            p0 = p0->next;
        }
        p0->next = p1;
    }
    else if(count == 1) //插入头部
    {
        p1->next = head;
        head = p1;
    }
    else
    {
        for(;count > 2;count--)
        {
            p0 = p0->next;
            p2 = p0->next;
        }

        p1->next = p0->next;
        p0->next = p1;
    }
    n++;

    return head;
}

运行结果这里就不贴图了,仅供参考,如果那里有错误,敬请指教。

你可能感兴趣的:(单链表,创建-插入-删除)