1、随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序);
2、把单向链表中元素逆置(不允许申请新的结点空间);
3、在单向链表中删除所有的偶数元素结点;
4、编写在非递减有序链表中插入一个元素使链表元素仍有序的函数。
5、利用算法4建立两个非递减有序单向链表,然后合并成一个非递增链表。
6、利用算法4建立两个非递减有序单向链表,然后合并成一个非递减链表。
7、一元多项式的存储并实现两个多项式相加。
1、随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序);
.#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct l
{
int data;
struct l *next;
}linelist;
linelist *creat()
{
int x;
linelist *head;
linelist *p;
head=new linelist;head->next=NULL;
p=head;
while(1)
{
scanf("%d",&x);
if(x==0) break;//输入为零终止输入(表示链表建立完成)
linelist *s;
s=new linelist;
s->data=x;s->next=p->next;
p->next=s;
p=s;
}
p->next=NULL;
return head;
}
void output(linelist *L)
{
linelist *p;
p=L->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
int main()
{
linelist *L;
L=creat();
output(L);
return 0;
}
2单链表元素逆置
不开辟新的空间只改变指针指向
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef struct LinkList
{
int data;
struct LinkList *next;
}LinkList;
LinkList* CreateList()//尾插法建立链表
{
int temp;
LinkList *head;
LinkList *rear;
rear=new LinkList;
head=new LinkList;
head=rear;
rear->next=NULL;
while(1)
{
scanf("%d",&temp);//输入数据
if(temp==0)
{
break;
}
LinkList *knot;
knot=new LinkList;
knot->data=temp;
rear->next=knot;
rear=knot;
}
return head;
}
void inverse(LinkList *head)//逆置链表中元素
{
LinkList *temp,*temp2;
temp=head->next;//temp指向首元节点
head->next=NULL;
while(temp!=NULL)//遍历链表,直到表尾
{
temp2=temp->next;
temp->next=head->next;//将摘取的节点插入到头结点后面
head->next=temp;
temp=temp2;
}
}
void DisplayList(LinkList *head)//输出链表
{
head=head->next;
while(head->next!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("%d\n",head->data);
}
int main()
{
LinkList *head;
head=CreateList();
inverse(head);
DisplayList(head);
return 0;
}
3.删除所有偶元素节点
先存一下每个链表的前驱,然后判断每个数据是否为偶数,是就删掉
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef struct LinkList
{
int data;
struct LinkList *next;
}LinkList;
LinkList* CreateList()//建立单链表
{
int temp;
LinkList *head;
LinkList *rear;
rear=new LinkList;
head=new LinkList;
head=rear;
rear->next=NULL;
while(1)
{
scanf("%d",&temp);
if(temp==0)
{
break;
}
LinkList *knot;
knot=new LinkList;
knot->data=temp;
rear->next=knot;
rear=knot;
}
return head;
}
void inverse(LinkList *head)
{
LinkList *temp,*temp2;
temp=head->next;
head->next=NULL;
while(temp!=NULL)
{
temp2=temp->next;
temp->next=head->next;
head->next=temp;
temp=temp2;
}
}
void DisplayList(LinkList *head)
{
head=head->next;
while(head->next!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("%d\n",head->data);
}
void Delete(LinkList *head)//
{
LinkList *find,*pre;
find=head->next;
pre=head;
while(find!=NULL)//核心
{
if((find->data)%2==0)//若是偶元素的节点就删除
{
pre->next=find->next;
find=pre->next;
}
else//若非偶元素节点则指向下一个
{
pre=find;
find=find->next;
}
}
}
int main()
{
LinkList *head;
head=CreateList();
Delete(head);
DisplayList(head);
return 0;
}
4.编写在非递减有序链表中插入一个元素使链表元素仍有序的函数
//建立非递减有序单向链表
#include <iostream>
using namespace std;
#define Maxsize 100
typedef struct LinkList
{
int data;
struct LinkList* next;
}LinkList;
LinkList* InitList()
{
LinkList* L;
L = new LinkList;
L->next = NULL;
LinkList* temp1, * temp2;
int x;
while (cin >> x && x)//输入x 当x=0时停止输入
{
LinkList* knot;
knot = new LinkList;
knot->data = x;
temp1 = L;
temp2 = L->next;
while (temp2)//每输入一个数就遍历一次链表
{
if (temp2->data > x)//找到值比x大的节点,将其插入
{
break;
}
temp1 = temp2;
temp2 = temp2->next;
}
knot->next = temp1->next;
temp1->next = knot;
}
return L;
}
void DisplayList(LinkList *head)//输出链表
{
head = head->next;
while (head->next != NULL)
{
cout << head->data<<" ";
head = head->next;
}
cout << head->data << endl;
}
int main()
{
LinkList* L;
L = InitList();
DisplayList(L);
return 0;
}
5利用算法4建立两个非递减有序单向链表,然后合并成一个非递增链表。
//两非递减链表变成一非递增链表
#include <iostream>
using namespace std;
#define Maxsize 100
typedef struct LinkList
{
int data;
struct LinkList* next;
}LinkList;
LinkList* InitList()//建立一个非递减链表
{
LinkList* L;
L = new LinkList;
L->next = NULL;
LinkList* temp1, * temp2;
int x;
while (cin >> x && x)//有序链表的创建
{
LinkList* knot;
knot = new LinkList;
knot->data = x;
temp1 = L;
temp2 = L->next;
while (temp2)
{
if (temp2->data > x)
{
break;
}
temp1 = temp2;
temp2 = temp2->next;
}
knot->next = temp1->next;
temp1->next = knot;
}
return L;
}
void DisplayList(LinkList *head)
{
head = head->next;
while (head->next != NULL)
{
cout << head->data<<" ";
head = head->next;
}
cout << head->data << endl;
}
LinkList* Addition(LinkList* L1, LinkList* L2)//两递增链表变成一递减链表
{
LinkList *temp,*L3,*p1,*p2,*p3;
p1=L1->next;//p1为L1工作指针,初始化为首元节点
p2=L2->next;//p2为L2工作指针,初始化为首元节点
L3=p3=L1;
L3->next=NULL;
while(p1||p2)//只要有一个表非空,就指向待摘取节点元素
{
if(!p1)
{
temp=p2;
p2=p2->next;
}
else if(!p2)
{
temp=p1;
p1=p1->next;
}
else if(p1->data<=p2->data)//取p1中较小者,temp指向p1,p1后移
{
temp=p1;
p1=p1->next;
}
Else//取p1中较小者,temp指向p1,p1后移
{
temp=p2;
p2=p2->next;
}
temp->next=L3->next;
L3->next=temp;
}
return L3;
}
int main()
{
LinkList* L1, * L2, * L3;
L1 = InitList();
L2 = InitList();
L3 = Addition(L1, L2);
DisplayList(L3);
return 0;
}
7.实现两多项式相加
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct LinkList
{
int data;
int exp;
struct LinkList *next;
}LinkList;
LinkList* CreateList(int n)//创建链表
{
LinkList *rear;
LinkList *head;
rear=new LinkList;
head=rear;
for(int i=1;i<=n;i++)
{
int temp1,temp2;
scanf("%d,%d",&temp1,&temp2);
LinkList *knot;
knot=new LinkList;
knot->data=temp1;
knot->exp=temp2;
rear->next=knot;
rear=knot;
}
rear->next=NULL;
return head;
}
void DisplayList(LinkList *head)
{
head=head->next;
while(head->next!=NULL)
{
printf("%d*x^%d ",head->data,head->exp);
head=head->next;
}
printf("%d*x^%d\n",head->data,head->exp);
}
LinkList* Addition(LinkList *head1,LinkList *head2)//两多项式相加算法
{
LinkList *p1,*p2,*p3;
p3=head1;
p1=head1->next;//p1为head1工作指针
p2=head2->next;//p2为head2工作指针
while(p1&&p2)//p1和p2均非空
{
if(p1->exp>p2->exp)//p1指数大于p2指数
{
p3->next=p2;
p3=p2;
p2=p2->next;
}
else if(p1->exp<p2->exp)//p1指数小于p2指数
{
p3->next=p1;
p3=p1;
p1=p1->next;
}
else if(p1->exp==p2->exp)//若指数相等
{
int sum;
sum=p1->data+p2->data;//将两系数相加
if(sum==0)//若和为0跳过这两个节点
{
p1=p1->next;
p2=p2->next;
}
else//若不为零修改p1当前节点系数值为两项系数的和
{
p1->data=sum;
p3->next=p1;
p3=p1;
p1=p1->next;
p2=p2->next;
}
}
}
if(p1==NULL)//插入非空多项式剩余段
{
p3->next=p2;
}
if(p2==NULL)插入非空多项式剩余段
{
p3->next=p1;
}
return head1;
}
int main()
{
LinkList *temp,*head1,*head2;
int m,n;
cin>>m;
head1=CreateList(m);
cin>>n;
head2=CreateList(n);
temp=Addition(head1,head2);
DisplayList(temp);
return 0;
}