情帮忙解释一个问题
我总是对链表这方面不怎么理解,希望大侠帮我解释一下关于这方面的知识,谢谢
程序代码:#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0 //宏定义NULL为0
#define SIZ sizeof(struct stu) //宏定义 SIZ为sizeof(struct stu)
struct stu //结构体
{
int num;
stu *next;
};
void main()
{
stu *H=NULL; //定义头指针
stu *r,*s,*p,*q;
int x,y;
cin>>x;
r=NULL; //建立单链表A
while(x!=NULL)
{
s=(stu*) malloc(SIZ);
s->num=x;
if(H==NULL)
H=s;
else
r->next=s;
r=s;
cin>>x;
}
if(r!=NULL)
r->next=NULL;
r=H;
while(r!=NULL) //输出单链表A
{
cout<<r->num<<" ";
r=r->next;
}
cout<<endl;
cin>>y; //输入要插入的数据
r=H;
p=H; //指向头结点
s=(stu*) malloc(SIZ); //申请空间
s->num=y;
if(H==NULL) //头结点为空,使头结点指向S
H=s;
else //头结点不为空,插入S
while(p->num<s->num)
{
if(p->next->num>s->num)
{
q=p->next;
s->next=q;
p->next=s;
}
p=p->next;
}
r=H;
while(r!=NULL)
{
cout<<r->num<<" ";
r=r->next;
}
cout<<endl;
}
插入
程序代码:#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#define NULL 0 //宏定义NULL为0
#define SIZ sizeof(struct stu) //宏定义 SIZ为sizeof(struct stu)
struct stu //结构体
{
int num;
stu *next;
};
void main()
{
stu *Ha=NULL; stu *Hb=NULL; stu *Hc=NULL; //定义头指针
stu *r,*s;
struct stu *p,*q; //定义指针p,q
int x;
cout<<"in put A:"<<endl; //建立单链表A
cin>>x;
r=Ha;
for(;x;cin>>x)
{
s=(stu*) malloc(SIZ);
s->num=x;
if(Ha==NULL)
Ha=s;
else
r->next=s;
r=s;
}
if(r!=NULL)
r->next=NULL;
r=Ha; //输出单链表A
while(r!=NULL)
{
cout<<r->num<<" ";
r=r->next;
}
cout<<endl;
cout<<"in put B:"<<endl; //建立单链表B
cin>>x;
r=Hb;
for(;x;cin>>x)
{
s=(stu*)malloc(SIZ);
s->num=x;
if(Hb==NULL)
Hb=s;
else
r->next=s;
r=s;
}
if(r!=NULL)
r->next=NULL;
r=Hb;
while(r!=NULL) //输出单项链表B
{
cout<<r->num<<" ";
r=r->next;
}
cout<<endl;
p=Ha;q=Hb;
r=Hc; //建立单链表C
while(p&&q) //p不为空,且q不为空
{
if(p->num<q->num)
{
s=p;
p=p->next;
}
else
{
s=q;
q=q->next;
}
if(Hc==NULL)
{
Hc=s;
r=Hc;
}
else
{
r->next=s;
r=s;
}
}
if(p!=NULL)
r->next=p;
else
if(q!=NULL)
r->next=q;
else
r=r->next;
r=Hc; //输出单链表C
while(r!=NULL)
{
cout<<r->num<<" ";
r=r->next;
}
cout<<endl;
}
合并
程序代码:#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#define NULL 0
#define SIZ sizeof(struct stu)
struct stu
{
int num;
stu *next;
};
stu *r,*s;
void main()
{
stu * same(stu *ha,stu *hb);
stu *Ha = NULL; stu *Hb = NULL; stu *Hc = NULL; //定义头指针
int x;
cout << "in put A:" << endl; //建立单链表A
cin >> x;
r = Ha;
for(; x ;cin >> x)
{
s = (stu*) malloc(SIZ);
s->num = x;
if(Ha == NULL)
Ha = s;
else
r->next = s;
r = s;
}
if(r != NULL)
r->next = NULL;
r = Ha;
while(r != NULL)
{
cout << r->num <<" ";
r = r->next;
}
cout << endl;
cout << "in put B:" << endl; //建立单链表B
cin >> x;
r = Hb;
for(; x ; cin>>x)
{
s = (stu*)malloc(SIZ);
s->num = x;
if(Hb == NULL)
Hb = s;
else
r->next = s;
r = s;
}
if(r != NULL)
r->next = NULL;
r = Hb;
while(r != NULL)
{
cout << r->num <<" ";
r = r->next;
}
cout << endl;
Hc = same( Ha, Hb);
r = Hc;
while(r != NULL)
{
cout << r->num <<" ";
r = r->next ;
}
cout << endl;
free(s);
}
stu *same(stu *ha,stu *hb)
{
stu *hc = NULL;
r = hc;
stu *q = ha;
stu *p = hb;
s = (stu*)malloc(SIZ);
while(p != NULL && q != NULL)
{
if(p->num == q->num)
{
s->num = p->num ;
if(hc == NULL)
{
hc = s;
}
else
{
r->next = s;
}
r = s;
s = (stu*)malloc(SIZ);
p = p->next ;
q = q->next ;
}
else
{
if(p->num < q->num )
{
p = p->next ;
}
else
{
q = q->next ;
}
}
}
if(r != NULL)
r->next = NULL;
return hc;
}求交集
