当前位置:编程学习 > C/C++ >>

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

[cpp]
#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; 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,