实验11_15_拆分链表
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
char ch;
struct Node* next;
};
void SelectTheList(struct Node* head, char c)
{
struct Node* current;
struct Node* temp;
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
p->ch = c;
p->next = NULL;
temp = head;
current = head->next;
while (current != NULL && c > current->ch)
{
temp = current;
current = current->next;
}
temp->next = p;
p->next = current;
}
void ShowTheList(struct Node* head, char ch)
{
struct Node* temp = head->next;
if (temp == NULL)
{
printf("There is no item in %c list.", ch);
goto A;
}
printf("The list %c is: ", ch);
while (1)
{
if (temp == NULL)
break;
printf("%c", temp->ch);
if (temp->next != NULL)
printf(" ");
temp = temp->next;
}
A: printf("\n");
return;
}
void FreeTheList(struct Node* head)
{
struct Node* tmp,*headP=head;
while (headP != NULL)
{
tmp = headP;
headP = headP->next;
free (tmp);
}
head = NULL;
}
int main()
{
struct Node* headL = (struct Node*)malloc(sizeof(struct Node));
struct Node* headA = (struct Node*)malloc(sizeof(struct Node));
struct Node* headB = (struct Node*)malloc(sizeof(struct Node));
struct Node* headC = (struct Node*)malloc(sizeof(struct Node));
headL->next = NULL;
headA->next = NULL;
headB->next = NULL;
headC->next = NULL;
struct Node* temp = headL;
char s[3] = "\0";
while (1)
{
scanf("%s", s);
if (strcmp(s, "-1") == 0)
{
temp=temp->next;
break;
}
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
p->ch = s[0];
p->next = NULL;
temp->next = p;
temp = p;
}
temp = headL->next;
while (1)
{
if (temp == NULL)
break;
if ((temp->ch >= 'a' && temp->ch <= 'z') || (temp->ch >= 'A' && temp->ch <= 'Z'))
SelectTheList(headA, temp->ch);
else if (temp->ch >= '0' && temp->ch <= '9')
SelectTheList(headB, temp->ch);
else
SelectTheList(headC, temp->ch);
temp = temp->next;
}
FreeTheList(headL);
ShowTheList(headA, 'A');
FreeTheList(headA);
ShowTheList(headB, 'B');
FreeTheList(headB);
ShowTheList(headC, 'C');
FreeTheList(headC);
}