链表(建立+插入+删除+输出)

#include <stdio.h>

#include <stdlib.h>

struct node

{

    int date;

    node *next;

}s[100];

int main()

{

    int n,i,t;

    while(~scanf("%d",&n))

    {

        node *root,*p;

        root = (node *)malloc(sizeof(node));

        root -> next = NULL;

        p = root;



        //建立

        for(i = 0 ; i < n ; i ++)

        {

            node *k = (node *)malloc(sizeof(node));

            scanf("%d",&t);

            k -> date = t;

            k -> next = NULL;

            p -> next = k;

            p = p -> next;

        }



        //删除

        int delete_num;

        printf("输入删除的数据:");

        scanf("%d",&delete_num);

        p = root;

        while(p -> next != NULL)

        {

            if(p -> next -> date == delete_num)

            {

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

                break;

            }

            p = p -> next;

        }





        //插入

        //在有序数列中插入数据

        int insert_num;

        printf("插入的数据:");

        scanf("%d",&insert_num);

        p = root;

        while(p -> next != NULL)

        {

            if(p -> next -> date >= insert_num)

            {

                node *k = (node *)malloc(sizeof(node));

                k -> date = insert_num;

                k -> next = p -> next;

                p -> next = k;

                break;

            }

            p = p -> next;

        }



        //输出

        p = root;

        while(p -> next != NULL)

        {

            printf("%d ",p -> next ->date);

            p = p -> next;

        }

    }

    return 0;

}

练习:http://acm.hdu.edu.cn/showproblem.php?pid=2019

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