改进!!
单链表分解:
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
typedef struct node
{
int data;
struct node *next;
}*LinkList, ListNode;
void CreateList(LinkList *head);
void Docompose(LinkList *head1, LinkList *head2);//单链表head1分解
void VisitList(LinkList head);
void DestroyList(LinkList *head);
int main(void)
{
LinkList newhead1 = NULL, newhead2 = NULL;
CreateList(&newhead1);
VisitList(newhead1);
Docompose(&newhead1, &newhead2);
VisitList(newhead1);
VisitList(newhead2);
DestroyList(&newhead1);
DestroyList(&newhead2);
getch();
return 0;
}
void CreateList(LinkList *head)
{
ListNode *p, *q;
int item;
q = NULL;
printf("Enter the item:\n");
scanf("%d", &item);
while (item != 0)
{
p = (LinkList)malloc(sizeof(ListNode));
if (p == NULL)
exit(1);
p -> data = item;
if (*head == NULL)
*head = p;
else
q -> next = p;
q = p;
printf("Enter the item:\n");
scanf("%d", &item);
}
if (q != NULL)
q -> next = NULL;
}
void Docompose(LinkList *head1, LinkList *head2)
{
int item;
ListNode *p, *tail1;
p = *head1;
printf("Enter docompose number:\n");
scanf("%d", &item);
while(p -> data != item)
{
p = p -> next;
}
tail1 = p;
p = p -> next;
tail1 -> next = NULL;
*head2 = p;
}
void VisitList(LinkList head)
{
LinkList p;
p = head;
while (p)
{
printf("%d ", p -> data);
p = p -> next;
}
printf("\n");
}
void DestroyList(LinkList *head)
{
LinkList p;
while (*head != NULL)
{
p = *head;
*head = (*head) -> next;
free(p);
}
}