/*设C={a1,b1,a2,b2,...,an,bn}为线性表,采用带头结点的hc单链表存放,设计一个就地算法,将其
拆分为两个线性表,使得A={a1,a2,...,an},B={bn,...,b2,b1}。*/
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList createHeadList(LinkList &L)
{
LNode *r;
L = (LinkList)malloc(sizeof(LNode));
r = L;
ElemType x;
scanf("%d",&x);
while(x != 9999)
{
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d",&x);
}
r->next = NULL;
return L;
}
LinkList dividePart(LinkList &A)
{
LinkList B = (LinkList)malloc(sizeof(LNode));
B->next = NULL;
LNode *p = A->next;
LNode *ra=A,*q;
while(p)
{
ra->next = p;
ra = p;
p = p->next;
if(p)
q = p->next;
p->next = B->next;
B->next = p;
p = q;
}
ra->next = NULL;
return B;
}
bool printList(LinkList L)
{
LNode *p = L->next;
while(p)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return true;
}
void main()
{
LinkList A,B;
createHeadList(A);
printList(A);
printf("After:\n");
B = dividePart(A);
printList(A);
printList(B);
}
就地分解奇偶序号元素
最新推荐文章于 2024-11-10 23:39:48 发布