问题遇到的现象和发生背景
拆分单链表(要求单数节点顺序,双数节点逆置)输出拆分后的链表,我的代码只能输出单数节点,无法输出双数节点,求解答
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;//声明节点类型
}LinkNode;
/*void CreateListF(LinkNode *&L,ElemType a[],int n)//头插法
{
LinkNode *s;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
for(int i=0;i<n;i++)
{
s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=a[i];
s->next=L->next;
L->next=s;
}
}*/
/*void CreateListR(LinkNode *&L,ElemType a[],int n)//尾插法
{
LinkNode *s,*r;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
r=L;
for(int i=0;i<n;i++)
{
s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}*/
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
}void DestoryList(LinkNode *&L)
{
LinkNode *p;
while(L)
{
p=L;
L=L->next;
free(p);
}
}
bool ListEmpty(LinkNode*L)
{
return(L->next==NULL);
}
int Listlength(LinkNode *L)
{
int i=0;
LinkNode *p=L->next;
while(p!=NULL)
{
i++;
p=p->next;
}
return (i);
}
void DispList(LinkNode *L)
{
LinkNode *p=L->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(LinkNode *L,int i,ElemType &e)
{
/*int j=0;
LinkNode *p=L;
if(i<=0) return false;
while(j<i&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
e=p->data;
return e;
}*/
LinkNode *p;
p=L->next;
int j=1;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p||j>i)
return false;
e=p->data;
return e;
}
int LocateElem(LinkNode *L,ElemType e)//查找第一个值域为e的元素符号
{
int i=1;
LinkNode *p=L->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return(0);
else
return i;
}
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *p=L,*s;
if(i<=0) return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}
bool ListDelete(LinkNode *&L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L,*q;
if(i<=0) return false;
while (j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
q=p->next;
if(q==NULL)
return false;
e=q->data;
p->next=q->next;
free(q);
return true;
}
}
void delmaxnode(LinkNode *&L)
{
LinkNode *p=L->next,*pre=L,*maxp=p,*maxpre=pre;
while(p!=NULL){
if(maxp->data<p->data)
{
maxp=p;
maxpre=pre;
}
pre=p;
p=p->next;
}
maxpre->next=maxp->next;
free(maxp);
}
void sort(LinkNode *&L)
{
LinkNode *p,*pre,*q;
p=L->next->next;
L->next->next=NULL;
while(p!=NULL)
{
q=p->next;
pre=L;
while(pre->next!=NULL&&pre->next->data<p->data)
pre=pre->next;
p->next=pre->next;
pre->next=p;
p=q;
}
}
void convers(LinkNode *L)
{
LinkNode *p,*q;
p=L->next;
L->next=NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=L->next;
L->next=q;
}
}
void combine(LinkNode *L1, LinkNode *L2)
{
LinkNode *C;
LinkNode *p,*q;
p = L1->next;
q = L2->next;
LinkNode *r;
LinkNode *s;
C = L1;
C->next = NULL;
r = C;
free(L2);
while (p && q)
{
if (p->data < q->data)
{
s = p;
p = p->next;
}
else if(p->data < q->data)
{
s = q;
q = q->next;
}
else
{
s = q;
q = q->next;
p = p->next;
}
s->next = r->next;
r->next = s;
r = r->next;
}
if (p)
r->next = p;
else
r->next = q;
return;
}
LinkNode Split1(LinkNode *&L1){
LinkNode *L2 ;
L2= (LinkNode*)malloc(sizeof(LinkNode));
L2->next = NULL;
LinkNode *p,*q,*ra;
p = L1->next;
ra = L1;
int count = 0;
while(p != NULL){
count++;
if(count%2!=0){
ra->next = p;
ra = p;
p = p->next;
}else{
q = p->next;
p->next = L2->next;
L2->next = p;
p = q;
}
}
ra->next = NULL;
return *L2;
}
LinkNode Split2(LinkNode *&L1){
LinkNode *L2 ;
L2= (LinkNode*)malloc(sizeof(LinkNode));
L2->next = NULL;
LinkNode *p,*q,*ra;
p = L1->next;
ra = L1;
int count = 0;
while(p != NULL){
count++;
if(count%2!=0){
ra->next = p;
ra = p;
p = p->next;
}else{
q = p->next;
p->next = L2->next;
L2->next = p;
p = q;
}
}
ra->next = NULL;
return *L2;
}
int main()
{
LinkNode *L1,*L2,*L3;
ElemType e;
printf("单链表的基本运算如下:\n");
printf(" (1)初始化单链表L1\n");
InitList(L1);
InitList(L2);
printf(" (2)依次采用尾插法插入元素\n");
ListInsert(L1,1,'8');
ListInsert(L1,2,'9');
ListInsert(L1,3,'7');
ListInsert(L1,4,'4');
ListInsert(L1,5,'6');
ListInsert(L2,1,'a');
ListInsert(L2,2,'c');
ListInsert(L2,3,'b');
ListInsert(L2,4,'h');
ListInsert(L2,5,'i');
printf(" (3)输出单链表L1:");
DispList(L1);
printf(" (3)输出单链表L2:");
DispList(L2);
combine(L1,L2);
printf(" (4)合并输出单链表");
DispList(L1);
printf(" (3.1)拆分单链表L1:");
Split1(L1);
DispList(L1);
printf(" (4.1)排序输出");
sort(L1);
DispList(L1);
convers(L1);
printf(" (5)逆序输出单链表L1: ");
DispList(L1);
printf(" (6)单链表h长度:%d\n",Listlength(L1));
printf(" (7)单链表h为%s\n",(ListEmpty(L1)?"空":"非空"));
GetElem(L1,3,e);
printf(" (8)单链表h的第三个元素:%c\n",e);
printf(" (9)元素a的位置:%d\n",LocateElem(L1,'a'));
printf(" (10)在第四个元素位置上插入f元素\n");
ListInsert(L1,4,'f');
printf(" (11)输出单链表:");DispList(L1);
printf(" (12)删除h的第三个元素\n");
ListDelete(L1,3,e);
printf(" (13)输出单链表h:");DispList(L1);
delmaxnode(L1);
printf(" (14)删除元素值的最大结点:");DispList(L1);
printf(" (15)释放单链表");
DestoryList(L1);
return 1;
}