数据结构之链表

来源:互联网 发布:mac如何投屏到电视 编辑:程序博客网 时间:2024/05/01 05:11
涉及到链表的建立、链表的输入、链表的输出、以及链表的归并、链表的插入
#include<iostream>#define OK 1#define ERROR 0using namespace std;typedef struct LNode{int data;struct LNode *next;}LNode,*List;void InitList(List &L){//新建一个链表 L=new LNode;L->next=NULL;//return OK;}void InputList(List &L,int n){//输入链表中的个数 cout<<"请输入"<<n<<"个数字:"<<endl;LNode *p,*r;r=L;for(int i=0;i<n;i++){p=new LNode;cin>>p->data;p->next=NULL;//r->next=p->data;r->next=p;r=p;}// return OK;}bool LocateList(List La,int m){//判断链表中是否有m LNode *p;p=La->next;while(p!=NULL){if(p->data==m)   return true;p=p->next;}return false;}void InsertList(List &La,int num){//将数字num插入到链表中 LNode *p;p=new LNode;p->data=num;p->next=La->next;La->next=p;}void UnionList(List &La,List Lb){//对链表La和链表Lb进行归并 int e;LNode *p;p=Lb->next;while(p!=NULL){e=p->data;if(!LocateList(La,e)){InsertList(La,e);p=p->next;} }// return OK; }void OutputList(List La){//输入归并后链表中的个数 LNode *p;p=new LNode;p=La->next;while(p!=NULL){cout<<p->data<<" ";p=p->next;}cout<<endl;}int main(){List La,Lb;int n,m;InitList(La);InitList(Lb);cout<<"输入La的个数:"<<endl;cin>>n;InputList(La,n);cout<<"输入Lb的个数:"<<endl;cin>>m;InputList(Lb,m);UnionList(La,Lb);OutputList(La);return 0;}

0 0
原创粉丝点击