线性表
題目內容:
采用单向链表实现一元多项式的存储并实现两个多项式相加并输出结果。
输入输出说明:
输入:
2x^2 3x^3 5x^5 7x^7#
-2x^2 2x^3 4x^4 5x^7 9x^9#
输出:
5x^3 4x^4 5x^5 12x^7 9x^9
#include<iostream>
#include <stdio.h>
struct node
{
int co;
int exp;
struct node * next;
};
node* Creat()
{
node* head;
node* s, *p;
int c, e;
char a;
head=new node;
head->next=NULL;
do
{
scanf("%d*x^%d%c",&c,&e,&a);
s=new node;
s->co = c;
s->exp = e;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next = s;
s->next = NULL;
}while(a!='#');
return head;
}
void Display(node * head)
{
if(head==NULL)
{
return ;
}
node* p;
p=head->next;
while(p!=NULL)
{
printf("%d*x^%d ",p->co,p->exp);
p=p->next;
}
printf("\n");
}
node* Add(node * A, node * B)
{
node * C=new node;
C->next=NULL;
node * p1=A->next;
node * p2=B->next;
node * p3;
node * pC=C;
while(p1!=NULL && p2!=NULL)
{
if(p1->exp < p2->exp)
{
p3=new node;
p3->co=p1->co;
p3->exp=p1->exp;
p3->next=NULL;
pC->next=p3;
pC=p3;
p1=p1->next;
}
else if (p1->exp > p2->exp)
{
p3=new node;
p3->co=p2->co;
p3->exp=p2->exp;
p3->next=NULL;
pC->next=p3;
pC=p3;
p2=p2->next;
}
else //p1->exp == p2->exp
{
if((p1->co + p2->co) != 0)
{
p3=new node;
p3->co = p1->co + p2->co;
p3->exp= p1->exp;
p3->next=NULL;
pC->next=p3;
pC=p3;
}
p1=p1->next;
p2=p2->next;
}
}
if(p1==NULL)
{
while(p2!=NULL)
{
p3=new node;
p3->co=p2->co;
p3->exp=p2->exp;
p3->next=NULL;
pC->next=p3;
pC=p3;
p2=p2->next;
}
}
else //p2==NULL
{
while(p1!=NULL)
{
p3=new node;
p3->co=p1->co;
p3->exp=p1->exp;
p3->next=NULL;
pC->next=p3;
pC=p3;
p1=p1->next;
}
}
return C;
}
int main()
{
node * p1;
p1=Creat();
node * p2;
p2=Creat();
node * p3;
//Display(p1);
//Display(p2);
p3=Add(p1,p2);
Display(p3);
return 0;
}
題目內容:
随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序)。
遍历单向链表。
输入输出说明:
输入:
1 2 3 5 7 8 0
输出:
1 2 3 5 7 8
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node*next;
}Lnode;
void create(Lnode *phead)
{
int data;
Lnode *p=phead,*q;
scanf("%d",&data);
while(data!=0)
{
q=(Lnode *)malloc(sizeof(Lnode));
q->next=NULL;
q->data=data;
p->next=q;
p=q;
scanf("%d",&data);
}
}
void output(Lnode* phead)
{
phead=phead->next;
while(phead!=NULL)
{
printf("%d ",phead->data);
phead=phead->next;
}
}
int main()
{
Lnode *phead=(Lnode *)malloc(sizeof(Lnode));
phead->next=NULL;
create(phead);
output(phead);
return 0;
}
題目內容:
试编写算法,把单向链表中元素逆置(不允许申请新的结点空间)。
输入输出说明:
输入:
1 2 4 6 8 9 0
输出:
9 8 6 4 2 1
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node*next;
}Lnode;
void create(Lnode *phead)
{
int data;
Lnode *p=phead,*q;
scanf("%d",&data);
while(data!=0)
{
q=(Lnode *)malloc(sizeof(Lnode));
q->next=NULL;
q->data=data;
p->next=q;
p=q;
scanf("%d",&data);
}
}
void output(Lnode* phead)
{
phead=phead->next;
while(phead!=NULL)
{
printf("%d ",phead->data);
phead=phead->next;
}
}
void reverse(Lnode *phead)
{
Lnode *p=phead,*q;
p=phead->next;
phead->next=NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=NULL;
q->next=phead->next;
phead->next=q;
}
}
int main()
{
Lnode *phead=(Lnode *)malloc(sizeof(Lnode));
phead->next=NULL;
create(phead);
reverse(phead);
output(phead);
return 0;
}
題目內容:
编写在非递减有序链表中插入一个元素使链表元素仍有序的函数,并利用该函数建立一个非递减有序单向链表。
输入输出说明:
输入:
1 6 4 3 5 8 7 9 0
输出:
1 3 4 5 6 7 8 9
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node*next;
}Lnode;
void create(Lnode *phead)
{
int data;
Lnode *p=phead,*q;
scanf("%d",&data);
while(data!=0)
{
q=(Lnode *)malloc(sizeof(Lnode));
q->next=NULL;
q->data=data;
p->next=q;
p=q;
scanf("%d",&data);
}
}
void output(Lnode* phead)
{
phead=phead->next;
while(phead!=NULL)
{
printf("%d ",phead->data);
phead=phead->next;
}
}
void reverse(Lnode *phead)
{
Lnode *p=phead,*q;
p=phead->next;
phead->next=NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=NULL;
q->next=phead->next;
phead->next=q;
}
}
void deletee(Lnode *phead)
{
Lnode *p,*q;
p=phead;
q=phead->next;
while(q!=NULL)
{
if(q->data%2==0)
{
p->next=q->next;
free(q);
q=p->next;
}
else
{
p=q;
q=q->next;
}
}
}
elemtype length(Lnode *phead)
{
Lnode *p;
p=phead->next;
int a=0;
while(p!=NULL)
{
p=p->next;
a++;
}
return a;
}
Lnode *sortlist (Lnode *phead)
{
Lnode *p;
if(phead->next==NULL)
{
return phead;
}
int n,i,j;
n=length(phead);
for(i=1;i<n;i++)
{
p=phead->next;
for(j=0;j<n-1;j++)
{
if(p->data > p->next->data)
{
elemtype t=p->data;
p->data=p->next->data;
p->next->data=t;
}
p=p->next;
}
}
return phead;
}
int main()
{
Lnode *phead=(Lnode *)malloc(sizeof(Lnode));
phead->next=NULL;
create(phead);
Lnode *p=sortlist(phead);
output(p);
return 0;
}
題目內容:
试编写算法,在单向链表中删除所有的偶数元素结点。
输入输出说明:
输入:
1 2 3 4 6 7 9 0
输出:
1 3 7 9
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node*next;
}Lnode;
void create(Lnode *phead)
{
int data;
Lnode *p=phead,*q;
scanf("%d",&data);
while(data!=0)
{
q=(Lnode *)malloc(sizeof(Lnode));
q->next=NULL;
q->data=data;
p->next=q;
p=q;
scanf("%d",&data);
}
}
void output(Lnode* phead)
{
phead=phead->next;
while(phead!=NULL)
{
printf("%d ",phead->data);
phead=phead->next;
}
}
void reverse(Lnode *phead)
{
Lnode *p=phead,*q;
p=phead->next;
phead->next=NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=NULL;
q->next=phead->next;
phead->next=q;
}
}
void deletee(Lnode *phead)
{
Lnode *p,*q;
p=phead;
q=phead->next;
while(q!=NULL)
{
if(q->data%2==0)
{
p->next=q->next;
free(q);
q=p->next;
}
else
{
p=q;
q=q->next;
}
}
}
int main()
{
Lnode *phead=(Lnode *)malloc(sizeof(Lnode));
phead->next=NULL;
create(phead);
deletee(phead);
output(phead);
return 0;
}
題目內容:
建立两个非递减有序单向链表,然后合并成一个非递增链表。
输入输出说明:
输入:
1 9 5 7 3 0
2 8 4 6 0
输出:
9 8 7 6 5 4 3 2 1
#include <iostream>
#include<stdio.h>
#include <stdlib.h>
using namespace std;
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node*next;
}lnode,*linklist;
void createhead(linklist &l)
{
l=new lnode;
l->next=NULL;
int x;
linklist p,r;
p=new lnode;
r=l;
scanf("%d",&x);
while(x!=0)
{
p=new lnode;
p->data=x;
p->next=NULL;
r->next=p;
r=p;
scanf("%d",&x);
}
}
void output(linklist l)
{
linklist p=l->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void mergelist(linklist &la,linklist &lb,linklist &lc)
{
linklist pa=la->next;
linklist pb=lb->next;
linklist pre;
lc=la;
la->next=NULL;
while(pa&&pb)
{
if(pa->data <= pb->data)
{
pre=pa->next;
pa->next=la->next;
la->next=pa;
pa=pre;
}
else
{
pre=pb->next;
pb->next=la->next;
la->next=pb;
pb=pre;
}
}
while(pa)
{
pre=pa->next;
pa->next=la->next;
la->next=pa;
pa=pre;
}
while(pb)
{
pre=pb->next;
pb->next=la->next;
la->next=pb;
pb=pre;
}
free(lb);}
/*void mergelist(linklist &la,linklist &lb,linklist &lc)
{
linklist pa=la->next;
linklist pb=lb->next;
linklist pc;
pc=lc=la;
lc->next=NULL;
while(pa||pb)
{
if(!pa)
{
pc=pb;
pb=pb->next;
}
if(!pb)
{
pc=pa;
pa=pa->next;
}
else if(pa->data <= pb->data)
{
pc=pa;
pa=pa->next;
}
else
{
pc=pb;
pb=pb->next;
}
pc->next=lc->next;
lc->next=pc;
}
delete lb;
}*/
int length(linklist l)
{
int n=0;
linklist p=l->next;
while(p)
{
p=p->next;
n++;
}
return n;
}
lnode *sortlist (lnode *phead)
{
lnode *p;
if(phead->next==NULL)
{
return phead;
}
int n,i,j;
n=length(phead);
for(i=1;i<n;i++)
{
p=phead->next;
for(j=1;j<n;j++)
{
if(p->data < p->next->data)
{
elemtype t=p->data;
p->data=p->next->data;
p->next->data=t;
}
p=p->next;
}
}
return phead;
}
int main()
{
linklist la,lb,lc;
createhead(la);
createhead(lb);
mergelist(la,lb,lc);
linklist p= sortlist(lc);
output(p);
return 0;
}
題目內容:
编写算法建立的链表,实现将其分解成两个链表,其中一个全部为奇数,另一个全部为偶数(尽量利用已知的存储空间)。
输入输出说明:
输入:
1 2 3 4 5 6 7 8 9 0
输出:
1 3 5 7 9
2 4 6 8
//https://blog.csdn.net/wuxuanyi27/article/details/51174330
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node*next;
}Lnode;
void create(Lnode *phead)
{
int data;
Lnode *p=phead,*q;
scanf("%d",&data);
while(data!=0)
{
q=(Lnode *)malloc(sizeof(Lnode));
q->next=NULL;
q->data=data;
p->next=q;
p=q;
scanf("%d",&data);
}
}
void output(Lnode* phead)
{
phead=phead->next;
while(phead!=NULL)
{
printf("%d ",phead->data);
phead=phead->next;
}
}
void reversee(Lnode *phead)
{
Lnode *p=phead,*q;
p=phead->next;
phead->next=NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=NULL;
q->next=phead->next;
phead->next=q;
}
}
void deletee(Lnode *phead)
{
Lnode *p,*q;
p=phead;
q=phead->next;
while(q!=NULL)
{
if(q->data%2==0)
{
p->next=q->next;
free(q);
q=p->next;
}
else
{
p=q;
q=q->next;
}
}
}
elemtype length(Lnode *phead)
{
Lnode *p;
p=phead->next;
int a=0;
while(p!=NULL)
{
p=p->next;
a++;
}
return a;
}
Lnode *sortlist (Lnode *phead)
{
Lnode *p;
if(phead->next==NULL)
{
return phead;
}
int n,i,j;
n=length(phead);
for(i=1;i<n;i++)
{
p=phead->next;
for(j=0;j<n-1;j++)
{
if(p->data > p->next->data)
{
elemtype t=p->data;
p->data=p->next->data;
p->next->data=t;
}
p=p->next;
}
}
return phead;
}
void insertt(Lnode *p,Lnode *t)
{
Lnode *p1=p->next,*p2=p;
if(p->next==NULL)
{
p->next=t;
return;
}
while(p1->data<t->data&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
if(p1->next!=NULL)
{
t->next=p2->next;
p2->next=t;
}
else
{
if(p1->data > t->data)
{
t->next=p2->next;
p2->next=t;
}
else
{
t->next=p1->next;
p1->next=t;
}
}
}
}
void add(Lnode *p1,Lnode *p2)
{
Lnode *p,*q;
p=p2->next;
while(p!=NULL)
{
q=p;
p=p->next;
insertt(p1,q);
}
}
void create1(Lnode *phead)
{
int data;
Lnode *p=phead,*q;
scanf("%d",&data);
while(data!=0)
{
q=(Lnode *)malloc(sizeof(Lnode));
q->next=NULL;
q->data=data;
insertt(p,q);
scanf("%d",&data);
}
}
void divide(Lnode * head)//分为左右两部分,左边的元素为奇数,右边所有元素为偶数
{
Lnode *L=head,*p,*q,*r,*s;
r=L;
p=L->next;
int i,j=0;
while(p!=NULL)
{
s=p;//s为最后一个节点;
p=p->next;
j++;
}
r=L;
p=L->next;
q=p->next;
for( i=0; i<j-1; i++)
{
if((p->data)%2==0)
{
r->next=q;
p->next=NULL;
s->next=p;
s=p;
p=q;
q=p->next;
}
else
{
r=p;
p=q;
q=p->next;
}
}
}
void depart(Lnode * head,Lnode* l)
{
Lnode* q=head,*p;
q=q->next;
while(q->data%2==1)
{
elemtype x;
x=q->data;
p=(Lnode *)malloc(sizeof(Lnode));
p->data=x;
p->next=l->next;
l->next=p;
head->next=q->next;
free(q);
q=head->next;
}
}
int main()
{
Lnode