《程序设计基础(B)II》实验2–链表
https://acm.sdut.edu.cn/onlinejudge3/contests/3193/overview
A - 数据结构实验之链表一:顺序建立链表
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head,*tail,*p;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
int n,i;
scanf("%d",&n);
for(i=0; i<n; i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=tail->next;
}
p=head->next;
printf("%d",p->data);
p=p->next;
while(p!=NULL)
{
printf(" %d",p->data);
p=p->next;
}
return 0;
}
B - 数据结构实验之链表二:逆序建立链表
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head,*p;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
int n,i;
scanf("%d",&n);
for(i=0; i<n; i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
}
p=head->next;
printf("%d",p->data);
p=p->next;
while(p!=NULL)
{
printf(" %d",p->data);
p=p->next;
}
return 0;
}
C - 师–链表的结点插入
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
int main()
{
int t,i,m,x;
while(~scanf("%d",&t))
{
struct node *head,*p,*q;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
int n=0;
while(t--)
{
scanf("%d %d",&m,&x);
q=(struct node*)malloc(sizeof(struct node));
q->data=x;
if(m<=n)
{
p=head;
for(i=0; i<m; i++)
{
p=p->next;
}
q->next=p->next;
p->next=q;
n++;
}
else
{
p=head;
for(i=0; i<n; i++)
{
p=p->next;
}
p->next=q;
q->next=NULL;
n++;
}
}
p=head->next;
while(p)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
}
return 0;
}
D - 数据结构实验之链表七:单链表中重复元素的删除
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
int main()
{
int n,i;
struct node *head,*p,*q,*t;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
scanf("%d",&n);
for(i=0; i<n; i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
}
printf("%d\n",n);
p=head->next;
while(p)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
p=head->next;
while(p->next!=NULL)
{
q=p->next;
t=p;
while(q!=NULL)
{
if(q->data==p->data)
{
q=q->next;
t->next=q;
n--;
}
else
{
q=q->next;
t=t->next;
}
}
if(p->next!=NULL)
p=p->next;
}
printf("%d\n",n);
p=head->next;
while(p)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
return 0;
}
E - 数据结构实验之链表三:链表的逆置
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next,*last;
};
int main()
{
int a;
struct node *head,*tail,*p;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
while(~scanf("%d",&a))
{
if(a==-1)
break;
p=(struct node*)malloc(sizeof(struct node));
p->data=a;
p->next=NULL;
tail->next=p;
p->last=tail;
tail=p;
}
printf("%d",p->data);
p=p->last;
while(p!=head)
{
printf(" %d",p->data);
p=p->last;
}
return 0;
}
F - 数据结构实验之链表四:有序链表的归并
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head1,*head2,*tail1,*tail2,*p,*q,*t;
head1=(struct node*)malloc(sizeof(struct node));
head1->next=NULL;
tail1=head1;
head2=(struct node*)malloc(sizeof(struct node));
head2->next=NULL;
tail2=head2;
int m,n,i;
scanf("%d %d",&m,&n);
for(i=0; i<m; i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail1->next=p;
tail1=p;
}
for(i=0; i<n; i++)
{
q=(struct node*)malloc(sizeof(struct node));
scanf("%d",&q->data);
q->next=NULL;
tail2->next=q;
tail2=q;
}
p=head1->next;
q=head2->next;
t=head1;
free(head2);
while(p&&q)
{
if(p->data>q->data)
{
t->next=q;
t=q;
q=q->next;
}
else
{
t->next=p;
t=p;
p=p->next;
}
}
if(p)
{
t->next=p;
t=p;
p=p->next;
}
if(q)
{
t->next=q;
t=q;
q=q->next;
}
p=head1->next;
while(p)
{
if(p->next==NULL)
printf("%d",p->data);
else
printf("%d ",p->data);
p=p->next;
}
return 0;
}
G - 数据结构实验之链表五:单链表的拆分
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head,*head1,*head2,*tail,*p,*q,*t;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
head1=(struct node*)malloc(sizeof(struct node));
head1->next=NULL;
head2=(struct node*)malloc(sizeof(struct node));
head2->next=NULL;
int n,i;
scanf("%d",&n);
for(i=0; i<n; i++)
{
t=(struct node*)malloc(sizeof(struct node));
scanf("%d",&t->data);
t->next=NULL;
tail->next=t;
tail=t;
}
t=head->next;
p=head1;
q=head2;
int m=0;
while(t)
{
if(t->data%2==0)
{
p->next=t;
p=t;
m++;
}
else
{
q->next=t;
q=t;
}
t=t->next;
}
p->next=NULL;
q->next=NULL;
printf("%d %d\n",m,n-m);
p=head1->next;
while(p)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
q=head2->next;
while(q)
{
if(q->next==NULL)
printf("%d",q->data);
else
printf("%d ",q->data);
q=q->next;
}
return 0;
}
H - 数据结构实验之链表九:双向链表
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next,*last;
};
int main()
{
struct node *head,*tail,*p;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
int n,m,i;
scanf("%d %d",&n,&m);
for(i=0; i<n; i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
p->last=tail;
tail=p;
}
int a;
while(m--)
{
scanf("%d",&a);
p=head->next;
for(i=0; i<n; i++)
{
if(p->data==a)
{
if(p->next!=NULL&&p->last!=head)
printf("%d %d\n",p->last->data,p->next->data);
if(p->next!=NULL&&p->last==head)
printf("%d\n",p->next->data);
if(p->next==NULL&&p->last!=head)
printf("%d\n",p->last->data);
break;
}
else
p=p->next;
}
}
return 0;
}
I - 约瑟夫问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next,*last;
};
int main()
{
struct node *head,*tail,*p;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
int n,m,i;
scanf("%d %d",&n,&m);
for(i=1; i<=n; i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=i;
p->next=NULL;
tail->next=p;
tail=p;
}
p->next=head->next;
p=head->next;
tail=head;
i=1;
while(n!=1)
{
if(i%m==0)
{
tail->next=p->next;
p=p->next;
i=1;
n--;
}
else
{
p=p->next;
tail=tail->next;
i++;
}
}
printf("%d",p->data);
return 0;
}
J - 不敢死队问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next,*last;
};
int main()
{
int n,i;
while(~scanf("%d",&n))
{
if(n==0)
break;
struct node *head,*tail,*p,*q;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(i=1; i<=n; i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=i;
p->next=NULL;
tail->next=p;
tail=p;
}
p->next=head->next;
p=head->next;
q=head;
int t=0,num=0;
while(1)
{
p=q->next;
num++;
if(num%5==0)
{
q->next=p->next;
t++;
if(p->data==1)
{
printf("%d\n",t);
break;
}
}
q=p;
}
}
return 0;
}