0
点赞
收藏
分享

微信扫一扫

【Java 问题】集合——List

河南妞 2024-10-15 阅读 27
#include <bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct node{
    ElemType Data;
    struct node* next;
};
typedef struct node* Set_LinkList;
Set_LinkList init(){
    Set_LinkList L = (Set_LinkList)malloc(sizeof(struct node));
    L->Data = -1;
    L->next = NULL;
    return L;
}
void insert(Set_LinkList L, ElemType e){
    Set_LinkList p = L->next;
    if(p == NULL){
        Set_LinkList newnode = (Set_LinkList)malloc(sizeof(struct node));
        L->next = newnode;
        newnode->Data = e;
        newnode->next = NULL;
        return;
    }
    while(p->next != NULL){
        if(p->Data == e){
            return;
        }
        p = p->next;
    }
    Set_LinkList newnode = (Set_LinkList)malloc(sizeof(struct node));
    p->next = newnode;
    newnode->Data = e;
    newnode->next = NULL;
}
void display(Set_LinkList L){
    Set_LinkList p = L->next;
    if(p == NULL){
        return;
    }
    while(p->next != NULL){
        cout<<p->Data<<' ';
        p = p->next;
    }
}
int main()
{
    int x = 0;
    Set_LinkList L = init();
    while(x != -1){
        cin>>x;
        insert(L , x);
    }
    display(L);
    return 0;
}

 

举报

相关推荐

0 条评论