- 题目:设指针la和lb分别指向两个无头结点单链表中的首元结点,试设计算法,从表la中删除自第i个元素起共len个元素,并将它们插入表lb的第j个元素之后
- 构造结构体
typedef struct node
{
int data;
struct node *next;
}LNode,*LinkList;
LinkList CreatList()
{
LinkList H = (LinkList)malloc(sizeof(LNode));
H -> next = NULL;
LNode *s,*r = H;
int x;
printf("请输入第一个数据:");
scanf("%d",&x);
while(x != -1)
{
s = (LinkList)malloc(sizeof(LNode));
s -> data = x;
r -> next = s;
r = s;
printf("请输入下一个数据,若需要结束输入,则输入-1:");
scanf("%d",&x);
}
r -> next = NULL;
return H;
}
void PrintList(LinkList L)
{
L = L -> next;
while(L != NULL)
{
printf("%d ",L -> data);
L = L -> next;
}
}
LinkList Listsreach(LinkList L,int i)
{
LNode *p = L;
int j = 0;
while(p -> next != NULL && j < i)
{
p = p -> next;
j ++;
}
if(j == i) return p;
else return NULL;
}
LinkList SaveDelete(LinkList L,int i,int len)
{
LinkList p,q;
LinkList delete = init();
LNode *s,*r;
p = Listsreach(L,i - 1);
delete = p ;
s = p -> next;
p -> next = delete -> next;
r = Listsreach(delete,len);
r -> next = NULL;
return delete;
}
LinkList ListDelete(LinkList L,int i,int len)
{
LinkList p,q;
int j;
p = Listsreach(L,i - 1);
for(j = i;j < i + len;j ++)
{
q = p -> next;
p -> next = q -> next;
free(q);
}
return L;
}
LinkList ListInsert(LinkList L,int j,LinkList delete,int len)
{
LNode *p,*s,*r;
int i;
p = Listsreach(L,j);
s = p -> next;
p -> next = delete -> next;
r = Listsreach(delete,len);
r -> next = s;
return L;
}
LinkList copyNode(LinkList L)
{
LinkList Lnew = init();
LinkList pb = Lnew ;
LinkList pa = L->next;
while(pa != NULL){
LinkList n =(LinkList)malloc(sizeof(LNode));
n->data = pa->data;
n->next = NULL;
pb->next = n;
pb = pb->next;
pa = pa->next;
}
return Lnew ;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}LNode,*LinkList;
LinkList CreatList()
{
LinkList H = (LinkList)malloc(sizeof(LNode));
H -> next = NULL;
LNode *s,*r = H;
int x;
printf("请输入第一个数据:");
scanf("%d",&x);
while(x != -1)
{
s = (LinkList)malloc(sizeof(LNode));
s -> data = x;
r -> next = s;
r = s;
printf("请输入下一个数据,若需要结束输入,则输入-1:");
scanf("%d",&x);
}
r -> next = NULL;
return H;
}
void PrintList(LinkList L)
{
L = L -> next;
while(L != NULL)
{
printf("%d ",L -> data);
L = L -> next;
}
}
LinkList Listsreach(LinkList L,int i)
{
LNode *p = L;
int j = 0;
while(p -> next != NULL && j < i)
{
p = p -> next;
j ++;
}
if(j == i) return p;
else return NULL;
}
LinkList init()
{
LinkList head =(LinkList)malloc(sizeof(LNode));
head->data = 0;
head->next = NULL;
return head;
}
LinkList SaveDelete(LinkList L,int i,int len)
{
LinkList p,q;
LinkList delete = init();
LNode *s,*r;
p = Listsreach(L,i - 1);
delete = p ;
s = p -> next;
p -> next = delete -> next;
r = Listsreach(delete,len);
r -> next = NULL;
return delete;
}
LinkList ListDelete(LinkList L,int i,int len)
{
LinkList p,q;
int j;
p = Listsreach(L,i - 1);
for(j = i;j < i + len;j ++)
{
q = p -> next;
p -> next = q -> next;
free(q);
}
return L;
}
LinkList ListInsert(LinkList L,int j,LinkList delete,int len)
{
LNode *p,*s,*r;
int i;
p = Listsreach(L,j);
s = p -> next;
p -> next = delete -> next;
r = Listsreach(delete,len);
r -> next = s;
return L;
}
LinkList copyNode(LinkList L)
{
LinkList Lnew = init();
LinkList pb = Lnew ;
LinkList pa = L->next;
while(pa != NULL){
LinkList n =(LinkList)malloc(sizeof(LNode));
n->data = pa->data;
n->next = NULL;
pb->next = n;
pb = pb->next;
pa = pa->next;
}
return Lnew ;
}
void main()
{
LinkList La,Lb,Lanew,Lbnew;
LinkList delete;
int i,j,len;
printf("请输入单链表La的数据");
La = CreatList();
printf("请输入单链表Lb的数据");
Lb = CreatList();
Lanew = copyNode(La);
printf("请输入i:");
scanf("%d",&i);
printf("请输入len:");
scanf("%d",&len);
delete = SaveDelete(La,i,len);
printf("删除的链表数据:");
PrintList(delete);
printf("\n");
printf("此时链表La数据为:");
Lanew = ListDelete(Lanew,i,len);
PrintList(Lanew);
printf("\n");
printf("请输入j:");
scanf("%d",&j);
printf("此时链表Lb的数据为:");
Lbnew = ListInsert(Lb,j,delete,len);
PrintList(Lb);
}
|