7-1 两个有序链表序列的合并
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *next;
};
int main()
{
int r;
node *h1=new node;
node *h2=new node;
h1->next=NULL;
h2->next=NULL;
node *p1,*p2,*t1,*t2;
t1=h1;
t2=h2;
while(true)
{
cin >>r;
if(r==-1)break;
p1=new node;
p1->data=r;
p1->next=NULL;
t1->next=p1;
t1=p1;
}
while(true)
{
cin >>r;
if(r==-1)break;
p2=new node;
p2->data=r;
p2->next=NULL;
t2->next=p2;
t2=p2;
}
node *h=new node;
h->next=NULL;
node *p,*t;
t=h;
p1=h1->next;
p2=h2->next;
while(p1!=NULL && p2!=NULL)
{
if(p1->data <= p2->data){
p=new node;
p->data=p1->data;
p->next=NULL;
t->next=p;
t=p;
p1=p1->next;
}
else {
p=new node;
p->data=p2->data;
p->next=NULL;
t->next=p;
t=p;
p2=p2->next;
}
}
while(p1!=NULL)
{
p=new node;
p->data=p1->data;
p->next=NULL;
t->next=p;
t=p;
p1=p1->next;
}
while(p2!=NULL)
{
p=new node;
p->data=p2->data;
p->next=NULL;
t->next=p;
t=p;
p2=p2->next;
}
p=h->next;
if(p==NULL){
printf("NULL");
return 0;}
while(p!=NULL)
{
if(p->next==NULL)cout <<p->data;
else cout <<p->data <<" ";
p = p->next;
}
}
7-2 两个有序链表序列的交集
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *next;
};
int main()
{
int i,n,cnt=0;
node *h1=new node;
node *h2=new node;
h1->next=NULL;
h2->next=NULL;
node *p1,*p2,*t1,*t2;
t1=h1;
t2=h2;
while(true)
{
cin >> n;
if(n==-1)break;
p1=new node;
p1->data=n;
p1->next=NULL;
t1->next=p1;
t1=p1;
}
while(true)
{
cin >> n;
if(n==-1)break;
p2=new node;
p2->data=n;
p2->next=NULL;
t2->next=p2;
t2=p2;
}
p1=h1->next;
p2=h2->next;
while(p1!=NULL&&p2!=NULL)
{
if(p1->data==p2->data){
if(cnt==0)cout <<p1->data;
else cout <<" "<<p1->data;
p2=p2->next;
p1=p1->next;
cnt++;
}
else {
if(p1->data < p2->data) {
p1 = p1->next;
} else {
p2 = p2->next;
}
}
}
if(cnt==0)cout <<"NULL";
}
7-3 重排链表
#include <stdio.h>
struct node
{
int data,next;
}a[100001];
int start,n;
int paixu[100001];
int chongpai[100001];
int main()
{
scanf("%d %d",&start,&n);
for(int i=0;i<n;i++){
int p,q,w;
scanf("%d %d %d",&p,&q,&w);
a[p].data=q;
a[p].next=w;
}
int len=0;
for(int i=start;i>=0;i=a[i].next){
paixu[len++]=i;
}
int left=0,right=len-1;
int t=0;
while(left<=right){
if(left<right){
chongpai[t++]=paixu[right];
right--;
chongpai[t++]=paixu[left];
left++;
}
else{
chongpai[t++]=paixu[left];
left++;
}
}
for(int i=0;i<t;i++){
if(i!=t-1){
printf("%.5d %d %.5d\n",chongpai[i],a[chongpai[i]].data,chongpai[i+1]);
}
else printf("%.5d %d -1\n",chongpai[i],a[chongpai[i]].data);
}
return 0;
}
7-4 约瑟夫环
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int main() {
int n, m;
scanf("%d %d", &n, &m);
struct node *h = NULL, *t = NULL, *p = NULL;
h=(struct node*)malloc(sizeof(struct node*));
h->data=1;
t=h;
for (int i = 2; i <= n; i++) {
p = (struct node*)malloc(sizeof(struct node));
p->data = i;
p->next = NULL;
t->next = p;
t = p;
}
t->next = h;
struct node *q = NULL;
t = h;
while (n > 0) {
for (int count = 1; count < m; count++) {
q = t;
t = t->next;
}
printf("%d", t->data);
n--;
if (n > 0) {
printf(" ");
}
if (q == NULL) {
h = t->next;
} else {
q->next = t->next;
}
struct node *to = t;
t = t->next;
free(to);
}
return 0;
}
单链表的创建及遍历
#include<bits/stdc++.h>
using namespace std;
struct node {
int data;
node *next;
};
int main()
{
int n,i;
node *h = new node;
h->next = NULL;
node *p, *t;
t = h;
cin >> n;
for(i = 0; i < n; i++) {
p = new node;
cin >> p->data;
p->next = NULL;
t->next = p;
t = p;
}
p = h->next;
for(i = 0; i < n; i++) {
if(i == 0) {
cout << p->data;
} else {
cout << " " << p->data;
}
p = p->next;
}
return 0;
}
7-6 链表去重
#include <stdio.h>
struct node
{
int key,next;
}a[100001];
int start;
int diyige[100001];
int dierge[100001];
int repeat[100001];
int main()
{
int n;
scanf("%d %d",&start,&n);
for(int i=0;i<n;i++){
int p,q,w;
scanf("%d %d %d",&p,&q,&w);
a[p].key=q;
a[p].next=w;
}
int len1=0,len2=0;
for(int i=start;i>-1;i=a[i].next){
int key=abs(a[i].key);
if(repeat[key]==0){
repeat[key]=1;
diyige[len1++]=i;
}
else{
dierge[len2++]=i;
}
}
for(int i=0;i<len1;i++){
printf("%.5d %d ",diyige[i],a[diyige[i]].key);
if(i==len1-1)
printf("-1\n");
else
printf("%.5d\n",diyige[i+1]);
}
for(int i=0;i<len2;i++){
printf("%.5d %d ",dierge[i],a[dierge[i]].key);
if(i==len2-1)
printf("-1\n");
else
printf("%.5d\n",dierge[i+1]);
}
return 0;
}
7-7单链表就地逆置
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int data;
struct node *next;
}Lnode,*LinkList;
LinkList createLinkedList()
{
LinkList head=(LinkList)malloc(sizeof(Lnode));
head->next=NULL;
LinkList r=head;
while(1){
int value;
scanf("%d",&value);
if(value==-1){break;}
LinkList p=(LinkList)malloc(sizeof(Lnode));
p->data=value;
p->next=NULL;
r->next=p;
r=p;
}
return head;
}
void nizhi(LinkList head)
{
LinkList p,q;
p=head->next;
head->next=NULL;
while(p){
q=p;
p=p->next;
q->next=head->next;
head->next=q;
}
}
void Print(LinkList head)
{
LinkList p=head->next;
while(p){
if(p->next!=NULL){
printf("%d ",p->data);
p=p->next;
}
else {
printf("%d",p->data);
p=p->next;
}
}
printf("\n");
}
int main()
{
int T;
scanf("%d",&T);
for(int i=0;i<T;i++)
{
LinkList L=NULL;
L=createLinkedList();
nizhi(L);
Print(L);
}
return 0;
}
7-8 带头节点的双向循环链表操作
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node * next;
node *pre;
};
void puf(node *h, node *p)
{
if (h->next == NULL) {
h->next = p;
p->next = h;
p->pre = h;
} else {
p->next = h->next;
h->next->pre = p;
h->next = p;
p->pre = h;
}
}
void pub(node *h, node *p)
{
if (h->next == NULL) {
h->next = p;
p->next = h;
p->pre = h;
} else {
node *t = h->next;
while (t->next != h) {
t = t->next;
}
t->next = p;
p->pre = t;
p->next = h;
h->pre = p;
}
}
void shun(node *h)
{
node *p = h->next;
if (p != h) {
printf("%d", p->data);
p = p->next;
}
while (p != h) {
printf(" %d", p->data);
p = p->next;
}
printf("\n");
}
void ni(node *h)
{
node *p = h->pre;
if (p != h) {
cout << p->data;
p = p->pre;
}
while (p != h) {
printf(" %d", p->data);
p = p->pre;
}
}
int main()
{
int n, i = 0;
node *h = new node;
h->next = h->pre = h;
while (~scanf("%d", &n) && n != -1)
{
node *p = new node;
p->data = n;
if (i % 2 == 0) {
puf(h, p);
} else {
pub(h, p);
}
i++;
}
shun(h);
ni(h);
return 0;
}
7-9 头插法创建单链表、遍历链表、删除链表
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node * next;
};
void deletelist(node *h) {
node *t;
while (h != NULL) {
t = h;
h = h->next;
delete t;
}
}
int main()
{
int k;
scanf("%d",&k);
while(k)
{
int n,i,j;
node * h=new node;
h->next=NULL;
node *p;
while(scanf("%d",&n)!=EOF&&n!=-1)
{
p=new node;
p->data=n;
p->next=h->next;
h->next=p;
}
p=h->next;
while(p!=NULL)
{
cout <<p->data<<" ";
p=p->next;
}
cout << endl;
k--;
deletelist(h);
}
}