【题目1】有一个带头结点的单链表L,其数据元素递增有序。插入任一数据e使其元素仍然有序。
代码实现:
#include "stdio.h"
#include "stdlib.h"
#define error -1
#define ok 1
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void create_list(LNode *L)
{
printf("Please input numbers until '9999' is inputed.\n");
int x;
L->next=NULL;
LNode *s,*r;
s=L;
scanf("%d",&x);
while(x!=9999)
{
r=(LNode*)malloc(sizeof(LNode));
r->data=x;
s->next=r;
s=r;
scanf("%d",&x);
}
s->next=NULL;
}
void print(LNode *L)
{
LNode *p;
p=L->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
}
int insert_list(LNode *L,int n,int e)
{
if(n<0) return error;
LNode *p,*r,*s;
r=L;
int i;
p=(LNode*)malloc(sizeof(LNode));
p->data=e;
for(i=0;i<n-1&&r!=NULL;i++)
{
r=r->next;
}
s=r->next;
r->next=p;
p->next=s;
return ok;
}
int length_list(LNode *L)
{
LNode *p;
int len=0;
p=L->next;
while(p)
{
p=p->next;
len++;
}
return len;
}
int sort_list(LNode *L,int length)
{
int len=length,i;
LNode *p,*s;
int tmp;
p=L->next;
while(len>1)
{
for(i=0;i<len-1;i++,p=p->next)
{
s=p->next;
if(p->data>s->data)
{
tmp=p->data;
p->data=s->data;
s->data=tmp;
}
}
p=L->next;
len--;
}
return ok;
}
int main()
{
int n,e,tmp;
LNode L;
printf("0->Quit the program;\n");
printf("1->Create a new list;\n");
printf("2->Insert a new number;\n");
printf("3->Sort the list;\n");
printf("4->Print the list;\n");
printf("5->Get the length of list;\n");
printf("Please input a number:");
int number,len;
while(1)
{
scanf("%d",&number);
if(number==0) {
printf("Thanks for using!\n");
break;
}
if(number==1)
{
create_list(&L);
len=length_list(&L);
printf("The list has been created successfully!\n");
continue;
}
if(number==2)
{
printf("Please input the position and number:\n");
scanf("%d %d",&n,&e);
tmp=insert_list(&L,n,e);
if(tmp==error)
printf("Failed to insert!Wrong data!\n");
else
printf("Inserted successfully!\n");
continue;
}
if(number==3)
{
len=length_list(&L);
sort_list(&L,len);
printf("Sorted successfully!\n");
continue;
}
if(number==4)
{
print(&L);
printf("\n");
continue;
}
if(number==5)
{
len=length_list(&L);
printf("The length of list is %d\n",len);
continue;
}
}
return 0;
}
【题目2】假设有一个带头结点的单链表L,每个结点值由单个数字、小写字母和大写字母构成。设计一个算法将其拆分成3个带头结点的单链表L1、L2和L3,L1包含L中的所有数字结点,L2包含L中的所有小写字母结点,L3包含L中的所有大写字母结点。
代码实现:
#include "stdio.h"
#include "stdlib.h"
#define error -1
#define ok 1
typedef struct LNode{
char data;
struct LNode *next;
}LNode;
void create_list(LNode *L)
{
printf("Please input numbers until 'ENTER' is inputed.\n");
int x;
L->next=NULL;
LNode *s,*r;
s=L;
scanf("%c",&x);
while(1)
{
r=(LNode*)malloc(sizeof(LNode));
r->data=x;
s->next=r;
s=r;
EX:
scanf("%c",&x);
if(x==' ') goto EX;
if(x=='\n') break;
}
s->next=NULL;
}
void print(LNode *L)
{
LNode *p;
p=L->next;
while(p)
{
printf("%c ",p->data);
p=p->next;
}
}
void Separate(LNode *L,LNode *l1,LNode *l2,LNode *l3)
{
l1->next=NULL;
l2->next=NULL;
l3->next=NULL;
LNode *p,*r1,*r2,*r3,*s1,*s2,*s3;
p=L;
s1=l1;
s2=l2;
s3=l3;
int i;
while(p)
{
if(p->data>='0'&&p->data<='9')
{
r1=(LNode*)malloc(sizeof(LNode));
r1->data=p->data;
s1->next=r1;
s1=r1;
s1->next=NULL;
}
else if(p->data>='a'&&p->data<='z')
{
r2=(LNode*)malloc(sizeof(LNode));
r2->data=p->data;
s2->next=r2;
s2=r2;
s2->next=NULL;
}
else if(p->data>='A'&&p->data<='Z')
{
r3=(LNode*)malloc(sizeof(LNode));
r3->data=p->data;
s3->next=r3;
s3=r3;
s3->next=NULL;
}
p=p->next;
}
}
void create(LNode *l)
{
l->next=NULL;
}
int main()
{
int n,e,tmp;
LNode L,l1,l2,l3;
create(&l1);
create(&l2);
create(&l3);
printf("0->Quit the program;\n");
printf("1->Create a new list;\n");
printf("2->Separate the list;\n");
printf("3->Print L1;\n");
printf("4->Print L2;\n");
printf("5->Print L3;\n");
printf("6->Print List;\n");
while(1)
{
printf("Please input a number:\n");
scanf("%d",&n);
getchar();
if(n==0)
{
printf("Thanks for using!\n");
break;
}
if(n==1)
{
create_list(&L);
printf("The list has been created successfully!\n");
continue;
}
if(n==2)
{
Separate(&L,&l1,&l2,&l3);
printf("The list has been separated successfully!\n");
continue;
}
if(n==3)
{
printf("The list1 is:\n");
print(&l1);
printf("\n");
continue;
}
if(n==4)
{
printf("The list2 is:\n");
print(&l2);
printf("\n");
continue;
}
if(n==5)
{
printf("The list3 is:\n");
print(&l3);
printf("\n");
continue;
}
if(n==6)
{
printf("The list is:\n");
print(&L);
printf("\n");
continue;
}
}
return 0;
}