数据结构C++链表:头插法、尾插法、获取长度、两个链表组合、删改查

#include<bits/stdc++.h>
using namespace  std;
#define Elemtype int
typedef struct node{
    Elemtype data;
    struct node* next;
}LNode,*LinkList;

void InitList(LNode* &L){
    L=new LNode;
    L->next=NULL;
}

void Hinput(LNode* &L,int n){
    LNode *p;
    for(int i=1;i<=n;i++){
        cout<<"please input the "<<i<<"-th number"<<endl;
        p=new LNode;
        cin>>p->data;
        p->next=L->next;
        L->next=p;
    }
}

void show(LNode* L){
    LNode *p;
    p=L->next;
    while(p!=NULL){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
}

int getLength(LNode *L){
    int Len=0;
    LNode *p;
    p=L->next;
    while(p!=NULL){
        Len++;
        p=p->next;
    }
    return Len;
}

void Tinput(LNode *L,int n){
    LNode *p;
    LNode *r;
    r=L;
    for(int i=1;i<=n;i++){
        cout<<"please input the "<<i<<"-th number"<<endl;
        p=new LNode;
        cin>>p->data;
        r->next=p;
        r=p;
    }
    r->next=NULL;
}

void insertList(LNode* &L,int num){
    LNode *p;
    p=new LNode;
    p->data=num;
    p->next=NULL;
    LNode *r;r=L;
    while(r->next!=NULL){
        r=r->next;
    }
    r->next=p;
}

void findData(LNode *L,int key){
    LNode *p;
    int loc=1;
    p=L->next;
    while(p !=NULL){
     if(p->data==key){cout<<loc<<"-th is the location"<<endl;return;}
      p=p->next;
      loc++;
    }
    cout<<"no such data"<<endl;
    return;
}

void insertt(LinkList L,Elemtype inserit,int loc){
    LNode *p;
    p=new LNode;
    p->data=inserit;
    LNode *r;
    r=L;
    int location=0;
    while(r->next!=NULL){
        if(location==loc-1){
            p->next=r->next;
            r->next=p;
            return;
        }
        location++;
        r=r->next;
    }
}

void deleteit(LinkList L,int deleloc){
    int location=0;
    LNode *p;
    LNode *r;
    p=L;
    while(p->next!=NULL){
        if(location==deleloc-1){
            r=p->next;
            p->next=r->next;
            free(r);
        }
        location++;
        p=p->next;
    }
}

void integrate(LinkList L1,LinkList L2){
    LNode *p=L1;
    while(p->next!=NULL){p=p->next;}
    p->next=L2->next;
    show(L1);
return;
}

int main(){
    //LinkList head;
    LNode *head;
    while (true){
        cout<<"\n0.exit\n1.init\n2.headAppend\n3.showList\n4.getTheLength\n5.tailAppend\n6.endInsert\n7.find\n8.insert\n9.delete\n10.integrate\n";
        int choice;cin>>choice;
        switch(choice){
            case 0:
                return 0;
                break;
            case 1:
                cout<<"init\n";
                InitList(head);
                break;
            case 2:
                cout<<"please input the number of your inputs\n";
                int num;cin>>num;
                Hinput(head,num);
                break;
            case 3:
                cout<<"show the list\n";
                show(head);
                break;
            case 4:
                int len;
                cout<<"length\n";
                len=getLength(head);
                cout<<"Length is:"<<len<<endl;
                break;
            case 5:
                cout<<"please input the number of your inputs\n";
                cin>>num;
                Tinput(head,num);
                break;
            case 6:
                Elemtype da;
                cout<<"insert the data\n";
                cin>>da;
                insertList(head,da);
                break;
            case 7:
                Elemtype findit;
                cout<<"input the data you wanna find"<<endl;
                cin>>findit;
                findData(head,findit);
                break;
            case 8:
                cout<<"input the data and the location"<<endl;
                Elemtype inserit;int loc;
                cin>>inserit>>loc;
                insertt(head,inserit,loc);
                break;
            case 9:
                cout<<"input the data location you wanna delete"<<endl;
                int deleloc;
                cin>>deleloc;
                deleteit(head,deleloc);
                break;
            case 10:
                cout<<"integrate two linear list"<<endl;
                LNode *head2;
                InitList(head2);
                cout<<"please input the number of your inputs\n";
                cin>>num;
                Hinput(head2,num);
                show(head2);
                cout<<"ready to integrate?(press any key)"<<endl;
                int cho;cin>>cho;
                integrate(head,head2);
                break;
            default:
                cout<<"no that kind of choice"<<endl;
                break;
    }}
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值