1.设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点。
以下构建了无头结点的单链表:
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct Node
{
int data;
Node *next;
}*Linklist,Node;
Linklist InitList(Linklist &p)//单链表初始化
{
p=NULL;
return p;
}
void HeadCreate(Linklist &p,int n)//从表尾到表头建立单链表
{
p=InitList(p);//初始化
Node *s;
p=NULL;
for(int i=0;i<n;i++)
{
s=new Node;
cin>>s->data;
s->next=p;
p=s;
}
}
void headcreate(Linklist &p,int n)//从表头到表尾建立单链表
{
p=InitList(p);
Node *s,*r;
for(int i=0;i<n;i++)
{
s=new Node;
cin>>s->data;
s->next=NULL;
if(p==NULL)
p=r=s;
else
{
r->next=s;
r=s;
}
}
}
//以上两种创建方式中p记录了表头所在位置
void PrintList(Linklist p)
{
Node *r=p;
while(r!=NULL)
{
cout<<r->data<<" ";
r=r->next;
}
cout<<endl;
}
void del_(Linklist &L,int x)
{
Node *p;
if(L==NULL)
return;
if(L->data==x)
{
p=L;
L=L->next;
delete p;
del_(L,x);
}
else del_(L->next,x);
}
int main( )
{
Linklist L,P;
int n=8;
L=InitList(L);
cout<<"请输入8个数据:"<<endl;
HeadCreate(L,n);
PrintList(L);
del_(L,5);
PrintList(L);
cout<<"请输入8个数据:"<<endl;
headcreate(P,n);
PrintList(P);
del_(P,5);
PrintList(P);
return 0;
}
运行结果如下:
也不知道对不对,但运行结果正确。。。
2.构造有序(升序)的单链表,并实现单链表的逆置(可以采用结构化的程序设计方法实现,即不必定义类)
输入链表中的数据。(用0表示输入的结束,0不能添加到链表中),按顺序输出有序链表中的数据
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
node *first,*p,*s,*r;
int x;
//建表
first=new node;
first->next=NULL;
while(cin>>x&&x)
{
s=new node;
s->data=x;
r=first;
p=first->next;
while(p&&x>p->data)
{
r=p;
p=p->next;
}
s->next=p;
r->next=s;
}
//输出
p=first->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
//逆置
p=first->next;
s=NULL;
while(p)
{
r=p->next;
p->next=s;
s=p;
p=r;
}
//输出
p=s;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
return 0;
}
运行结果如下:
3.
#include<iostream>
using namespace std;
int a[1000],b[1000];
struct node
{
int data;
node *next;
};
class linklist
{
public:
linklist();
linklist(int n,int b[]);//头插法
linklist(int a[],int n);//尾插法
void printlist();
int del(int x);
int delwz(int n);
bool dzyx();//判断该链表是否递增有序
~linklist();
private:
node *first;
};
linklist::linklist()
{
first=new node;
first->next=NULL;
}
linklist::linklist(int n,int b[])//头插法
{
first=new node;
first->next=NULL;
node *s;
for(int i=0; i<n; i++)
{
s=new node;
s->data=b[i];
s->next=first->next;
first->next=s;
}
}
linklist::linklist(int a[],int n)//尾插法
{
first=new node;
node *s=NULL,*p=first;
for(int i=0; i<n; i++)
{
s=new node;
s->data=a[i];
p->next=s;
p=s;
}
p->next=NULL;
}
void linklist::printlist()
{
node *p;
p=first->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int linklist::del(int x)
{
node *p=first->next,*s=NULL,*q=first;
if(p==NULL)
return 0;
while(p!=NULL)
{
if(p->data==x)
{
s=q->next;
q->next=s->next;
delete q;
}
q=p;
p=p->next;
}
return 1;
}
int linklist::delwz(int n)
{
if(n<1)
return 0;
node *p=first,*s=NULL;
int c=0,x;
while(p!=NULL&&c<n-1)
{
p=p->next;
c++;
}
if(p==NULL||p->next==NULL)
return 0;
else
{
s=p->next;
x=s->data;
p->next=s->next;
delete p;
return x;
}
return 1;
}
bool linklist::dzyx()
{
node *p=first->next,*s=NULL;
int c=1;
while(p)
{
s=p->next;
if(s!=NULL&&(s->data)<(p->data))
{
c=0;
break;
}
p=p->next;
}
if(c)
return 1;
else return 0;
}
linklist::~linklist()
{
node *p=first;
while(first)
{
first=first->next;
delete p;
p=first;
}
}
int main()
{
int n=0,x,m=0;
while(cin>>x&&x!=0)
{
a[n++]=x;
}
while(cin>>x&&x!=0)
{
b[m++]=x;
}
linklist l1(a,n),l2(m,b);
l1.printlist();
l2.printlist();
cin>>x;
if(l1.del(x))
l1.printlist();
else cout<<"Error"<<endl;
cin>>x;
if(l2.delwz(x))
l2.printlist();
else cout<<"Error"<<endl;
if(l1.dzyx())
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
不知道对不对。。。
正确答案^_^
#include <iostream>
using namespace std;
struct node
{
int data;
node *next,*piror;
};
class doublelink
{
public:
doublelink();
void Insert(int x);
int Delete(int x);
void printlist();
~doublelink();
private:
node *first;
};
doublelink::doublelink()
{
first=new node;
first->next=NULL;
first->piror=NULL;
}
void doublelink::Insert(int x)
{
node *p=first->next,*s,*q=first;
s=new node;
s->data=x;
s->next=NULL;
while(p)
{
if(p->data>=x)
{
s->next=p;
s->piror=q;
q->next=s;
p->piror=s;
break;
}
q=p;
p=p->next;
}
if(p==NULL)
{
s->piror=q;
s->next=q->next;
q->next=s;
}
}
int doublelink::Delete(int x)
{
node *p=first->next,*s=NULL,*q=first;
if(p==NULL)
return 0;
while(p!=NULL)
{
if(p->data==x)
{
p=p->next;
s=q->next;
q->next=p;
if(p!=NULL)
p->piror=q;
delete s;
}
else
{
q=p;
p=p->next;
}
}
}
void doublelink::printlist()
{
node *p=first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
doublelink::~doublelink()
{
node *p=first;
while(first!=NULL)
{
first=first->next;
delete p;
p=first;
}
}
int main()
{
doublelink l;
int x;
while(cin>>x&&x!=0)
{
l.Insert(x);
}
l.printlist();
while(cin>>x&&x!=0)
{
l.Insert(x);
}
l.printlist();
while(cin>>x&&x!=0)
{
l.Delete(x);
}
l.printlist();
return 0;
}
#include <iostream>
using namespace std;
int a[1000];
struct node
{
int data;
node *next;
};
class linklist
{
public:
linklist();
void Insert(int x);
void printlist();
void add(linklist l);
void summ();
private:
node *first;
int sum;
};
linklist::linklist()
{
first=new node;
first->next=NULL;
sum=0;
}
void linklist::Insert(int x)
{
node *p=first->next,*q=first,*s;
s=new node;
s->data=x;
s->next=NULL;
while(p)
{
if(p->data==x)
{
break;
}
else if(p->data>x)
{
s->next=p;
q->next=s;
break;
}
q=p;
p=p->next;
}
if(p==NULL)
{
s->next=q->next;
q->next=s;
}
}
void linklist::summ()
{
node *p=first->next;
while(p)
{
sum++;
p=p->next;
}
cout<<sum<<endl;
}
void linklist::printlist()
{
summ();
node *p=first->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void linklist::add(linklist l)
{
node *p=first->next;
while(p)
{
l.Insert(p->data);
p=p->next;
}
}
int main()
{
linklist l1,l2;
int n,m,x;
cin>>n>>m;
for(int i=0; i<n; i++)
{
cin>>x;
l1.Insert(x);
}
//l1.printlist();
for(int i=0; i<m; i++)
{
cin>>x;
l2.Insert(x);
}
//l2.printlist();
l1.add(l2);
l2.printlist();
return 0;
}
#include <iostream>
using namespace std;
int a[1000];
struct node
{
int data;
node *next;
};
class linklist
{
public:
linklist();
void Insert(int x);
void printlist();
void add(linklist *l);
void summ();
private:
node *first;
int sum;
};
linklist::linklist()
{
first=new node;
first->next=NULL;
sum=0;
}
void linklist::Insert(int x)
{
node *p=first->next,*q=first,*s;
s=new node;
s->data=x;
s->next=NULL;
while(p)
{
if(p->data==x)
{
break;
}
q=p;
p=p->next;
}
if(p==NULL)
{
s->next=q->next;
q->next=s;
}
}
void linklist::summ()
{
node *p=first->next;
while(p)
{
sum++;
p=p->next;
}
cout<<sum<<endl;
}
void linklist::printlist()
{
summ();
node *p=first->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void linklist::add(linklist *l)
{
node *p=l->first->next;
while(p)
{
Insert(p->data);
p=p->next;
}
}
int main()
{
linklist l1,l2,*L=&l2;
int n,m,x;
cin>>n>>m;
for(int i=0; i<n; i++)
{
cin>>x;
l1.Insert(x);
}
//l1.printlist();
for(int i=0; i<m; i++)
{
cin>>x;
l2.Insert(x);
}
//l2.printlist();
l1.add(L);
l1.printlist();
return 0;
}
#include <iostream>
using namespace std;
const int maxsize=10000;
class seqlist
{
public:
seqlist(int a[],int n);
int len();
void insert(int i,int x);
int del(int i);
int locate(int x);
void printlist();
private:
int data[maxsize];
int length;
};
seqlist::seqlist(int a[],int n)
{
if(n>maxsize)
throw"";
for(int i=0;i<n;i++)
data[i]=a[i];
length=n;
}
int seqlist::len()
{
return length;
}
void seqlist::insert(int i,int x)
{
if(length>=maxsize)
return;
if(i<1||i>length+1)
return;
for(int j=length;j>=i;j--)
{
data[j]=data[j-1];
}
data[i-1]=x;
length++;
}
int seqlist::del(int i)
{
int x;
if(length==0)
return 0;
if(i<1||i>length)
return 0;
x=data[i-1];
for(int j=i;j<length;j++)
{
data[j-1]=data[j];
}
length--;
return x;
}
int seqlist::locate(int x)
{
for(int i=0;i<length;i++)
{
if(data[i]==x)
return i+1;
}
}
void seqlist::printlist()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
int main()
{
int a[10000],n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
seqlist l(a,n);
l.printlist();
l.del(1);
l.printlist();
cout<<l.len()<<endl;
l.insert(2,100);
l.printlist();
cout<<l.locate(100)<<endl;
return 0;
}