#include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList; //尾插法 LinkList List_TailInsert(LinkList &L) { ElemType x; L=(LinkList)malloc(sizeof(LNode)); LNode *s,*r=L; printf("请输入单链表各个节点,以9999结束!\n"); scanf("%d",&x); while(x!=9999) { s=(LNode*)malloc(sizeof(LNode)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=NULL; return L; } int Length(LinkList L) { LNode *p=L; int count=0; while(p->next!=NULL) { p=p->next; count++; } return count; } //将带头结点的单链表A按序号奇偶分解到A或B中 LinkList DisCreate_A_B(LinkList &A) { int i=0; LinkList B=(LinkList)malloc(sizeof(LNode)); B->next=NULL; LNode *ra=A,*rb=B;//分别指向两个空链表 LNode *p=A->next;//标记A的真实节点数据 A->next=NULL;//断开A与后面元素的链接 while(p!=NULL) { i++; if(i%2==0) { rb->next=p; rb=p; } else { ra->next=p; ra=p; } p=p->next; } ra->next=NULL; rb->next=NULL; return B; } int main(){ LinkList L; LinkList R,S;//接收输入数据和处理完的B R=List_TailInsert(L); S=DisCreate_A_B(R); LNode *p=S; while(p->next!=NULL){ p=p->next; printf("->%d",p->data); } }
浙公网安备 33010602011771号