作业9
1.题目:创建单链表并赋值,要求遍历能输出1~9
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node Node;
typedef struct node * Link;
struct node
{
int num;
Link next;
};
void creat_node(Link *node)
{
*node = (Link)malloc(sizeof(Node));
is_malloc_ok(*node);
}
void is_malloc_ok(Link node)
{
if(node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
creat_node(head);
(*head)->next = NULL;
}
void incret_node_head(Link head,Link new_node)
{
new_node->next = head->next;
head->next = new_node;
}
void incret_node_tail(Link head,Link new_node)
{
Link p;
p = head;
while(p->next != NULL)
{
p = p->next;
}
new_node->next = p->next;
p->next = new_node;
}
void incret_node_mid_qian(Link head,Link new_node)
{
Link p,q;
p = q = head->next;
if(head->next == NULL)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
if(head->next->num > new_node->num)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
while(p != NULL && p->num < new_node->num)
{
q = p;
p = p->next;
}
q->next = new_node;
new_node->next = p;
}
}
}
void incret_num(Link head,Link new_node,int loc)
{
Link p;
if(head->next->num == loc)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
p = head->next;
while(p->next != NULL && p->num != loc)
{
p = p->next;
}
new_node->next = p->next;
p->next = new_node;
}
}
void delet_node(Link head,int dnum)
{
Link p,q;
q = p = head->next;
if(head->next->num == dnum)
{
head->next = p->next;
}
else
{
while(p != NULL && p->num != dnum)
{
q = p;
p = p->next;
}
if(p == NULL)
{
printf("no found!\n");
}
else
{
q->next = p->next;
}
}
}
void display_node(Link head)
{
Link p;
if(head == NULL)
{
printf("The node is free!\n");
return;
}
if(head->next == NULL)
{
printf("The node is empty!\n");
return;
}
p = head->next;
while(p != NULL)
{
printf("%3d",p->num);
p = p->next;
}
printf("\n");
}
void empty_node(Link head)
{
Link p;
p = head;
while(head->next != NULL)
{
p = head->next;
head->next = p->next;
free(p);
}
}
void release_head(Link *head)
{
Link p = NULL;
while((*head) != NULL)
{
p = *head;
*head = p->next;
free(p);
}
}
int main()
{
Link head = NULL;
Link new_node = NULL;
int i;
int n,loc;
int dnum;
creat_link(&head);
srand(time(NULL));
for(i = 0; i < 10; i++)
{
creat_node(&new_node);
new_node->num = i + 1;
incret_node_tail(head,new_node);
}
display_node(head);
printf("\n");
empty_node(head);
display_node(head);
release_head(&head);
display_node(head);
return 0;
}
结果:
2.题目:读取一个5*5数组,然后显示每行的和与每列的和
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include<time.h>
int main()
{
int a[5][5];
int i,j;
long sum1 = 0,sum2 = 0;
srand(time(NULL));
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
a[i][j] = rand() % 100;
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("\n");
for(i = 0; i < 5; i++)
{
sum2 = 0;
for(j = 0; j < 5; j++)
{
sum2 = sum2 + a[i][j];
}
printf("第%d行的和:%ld\n",i+1,sum2);
}
printf("\n");
for(j = 0; j < 5; j++)
{
sum1 = 0;
for(i = 0; i < 5; i++)
{
sum1 = sum1 + a[i][j];
}
printf("第%d列的和:%ld\n",j+1,sum1);
}
return 0;
}
结果:
3.题目:编程判断字符串是否为回文
判断一个字符串是否是回文,例如单词‘level’
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
int main()
{
char str[100],ptr[100];
int i = 0,j,t = 0;
int flag;
printf("Please input a word:");
gets(str);
while(str[i] != '\0')
{
i++;
}
for(j = i - 1;j >= 0;j--)
{
ptr[t++] = str[j];
}
ptr[t] = '\0';
for(j = 0;j < i;j++)
{
if(str[j] != ptr[j])
{
flag = 1;
}
}
if(flag == 1)
{
printf("不是回文!\n");
}
else
{
printf("是回文!\n");
}
return 0;
}
4.题目:生日蜡烛
某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。
现在算起来,他一共吹熄了236根蜡烛。
请问,他从多少岁开始过生日party的? (第七届蓝桥杯B组真题(第二题))
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
int main()
{
int i,j;
int sum = 0;
for(i = 0; i < 236; i++)
{
j = i;
sum = 0;
while(sum < 236)
{
sum = sum + j;
j++;
}
if(sum == 236)
{
printf("%d\n",i);
break;
}
}
return 0;
}
结果:
5.题目:编程实现查找两个字符串的最大公共子串
示例:“aocdfe"和"pmcdfa"最大公共子串为"cfd”
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <string.h>
void fun(char *str,char *ptr)
{
char *p = str;
char *q = ptr;
int len1 = strlen(str);
int len2 = strlen(ptr);
char a[100] = "\0";
char b[100] = "\0";
char c[100];
int i;
int count = 0;
if(len2 > len1)
{
strcpy(c,str);
strcpy(str,ptr);
strcpy(ptr,c);
}
for(i = 0; i < len1; i++)
{
if(*p == *q)
{
while(*p == *q)
{
a[count] = *q;
count++;
q++;
p++;
}
}
else
{
p++;
}
if(strlen(a) > strlen(b))
{
strcpy(b,a);
}
}
printf("The max is:%s\n",b);
}
int main()
{
char str[100];
char ptr[100];
printf("Please input the str:");
gets(str);
getchar();
printf("Please input the ptr:");
gets(ptr);
fun(str,ptr);
return 0;
}
结果:
6.题目:有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位.
提示:用环形链表实现
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
int main()
{
int a[100],n,i,m,j;
printf("Please input the number of people:");
scanf("%d",&n);
for(i=0;i<n;i++) /*这个是给每个人编号的*/
{
a[i]=i+1;
}
i=0; /*i作为每次循环记数的*/
m=0; /*m是按1 2 3 报数的*/
j=0; /*j为退出的人数*/
while(j<n-1) /*当未退出的人为1时,退出循环*/
{
if(a[i]!=0) /*开始报数*/
{
m++;
}
if(m==3) /*当报到3时*/
{
a[i]=0; /*将这个编号的人赋值为0*/
m=0; /*重新从1开始报数*/
j++; /*记录退出的人的数目*/
}
i++;
if(i==n)
{
i=0; /*报数到最后是i恢复到0*/
}
}
for(i=0;i<n;i++)
if(a[i]!=0)
printf("%d\n",a[i]);
return 0;
}
7.题目:创建一个学生学号及姓名的单链表,即节点包括学生学号、姓名及指向下一个节点的指针,链表按学生的学号排列。再从键盘输入某一学生姓名,将其从链表中删除。
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node STUDENT;
typedef struct node * Link;
struct node
{
long num;
char name[10];
Link next;
};
void is_malloc_ok(Link new_node)
{
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
*head = NULL;
}
void creat_node(Link *new_node)
{
*new_node = (Link)malloc(sizeof(STUDENT));
is_malloc_ok(*new_node);
}
void incret_node(Link *head,Link new_node)
{
Link p,q;
q = p = *head;
if(*head == NULL)
{
new_node -> next = *head;
*head = new_node;
}
else
{
if(p -> num <= new_node -> num)
{
new_node -> next = p;
*head = new_node;
}
else
{
while(p != NULL && p -> num > new_node -> num)
{
q = p;
p = p -> next;
}
q -> next = new_node;
new_node -> next = p;
}
}
}
void release_node(Link *head)
{
Link p;
p = *head;
while(*head != NULL)
{
*head = (*head) -> next;
free(p);
p = *head;
}
}
void delet_name(Link *head,char m[])
{
Link p,q;
q = p = *head;
if(strcmp((*head)->name,m) == 0)
{
*head = p->next;
}
while(p != NULL && strcmp(p->name,m) != 0)
{
q = p;
p = p -> next;
}
if(p == NULL)
{
printf("没有找到!\n");
}
else
{
q->next = p->next;
}
}
void display_node(Link head)
{
Link p;
p = head;
if(head == NULL)
{
printf("The node is empty!\n");
return;
}
while(p != NULL)
{
printf("学号:%ld 姓名:%s\n",p->num,p -> name);
p = p -> next;
}
printf("\n");
}
int main()
{
Link head = NULL;
Link new_node = NULL;
int i,n;
char m[10];
creat_link(&head);
printf("请输入班级有多少男生:");
scanf("%d",&n);
printf("请输入学生学号和姓名:");
for(i = 0; i < n; i++)
{
creat_node(&new_node);
scanf("%ld%s",&new_node->num,&new_node->name);
incret_node(&head,new_node);
}
display_node(head);
printf("\n");
printf("请输入要删除的学生的姓名:");
scanf("%s",&m);
delet_name(&head,m);
display_node(head);
release_node(&head);
display_node(head);
return 0;
}
结果:
作业10:
1、题目:创建两个学生链表,含有姓名、年龄的信息,一个链表存放男生,一个链表存放女生
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct node STUDENT;
typedef struct node * Link;
struct node
{
int year;
char name[10];
char sex[10];
Link next;
};
void is_malloc_ok(Link new_node)
{
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
*head = NULL;
}
void creat_node(Link *new_node)
{
*new_node = (Link)malloc(sizeof(STUDENT));
is_malloc_ok(*new_node);
}
void incret_node(Link *head,Link new_node)
{
Link p,q;
q = p = *head;
if(*head == NULL)
{
new_node -> next = *head;
*head = new_node;
}
else
{
if(p -> year <= new_node -> year)
{
new_node -> next = p;
*head = new_node;
}
else
{
while(p != NULL && p -> year > new_node -> year)
{
q = p;
p = p -> next;
}
q -> next = new_node;
new_node -> next = p;
}
}
}
void release_node(Link *head)
{
Link p;
p = *head;
while(*head != NULL)
{
*head = (*head) -> next;
free(p);
p = *head;
}
}
/*void search_score(Link head,int m)
{
Link p;
p = head;
int i = 0;
while(p != NULL && p -> score != m)
{
i++;
p = p -> next;
}
if(p == NULL)
{
printf("没有找到!\n");
}
else
{
printf("姓名:%s 分数:%3d 名次:%d\n",p -> name,p -> score,i+1);
}
}*/
void display_node(Link head)
{
Link p;
p = head;
if(head == NULL)
{
printf("The node is empty!\n");
return;
}
while(p != NULL)
{
printf("年纪:%3d 姓名:%s 性别:%s\n",p->year,p -> name,p->sex);
p = p -> next;
}
printf("\n");
}
int main()
{
Link head1 = NULL;
Link head2 = NULL;
Link new_node = NULL;
int i,n,m;
int g;
creat_link(&head1);
creat_link(&head2);
printf("请输入班级有多少男生:");
scanf("%d",&n);
printf("请输入学生年纪和姓名:");
for(i = 0; i < n; i++)
{
creat_node(&new_node);
scanf("%d%s%s",&new_node->year,&new_node->name,&new_node->sex);
incret_node(&head1,new_node);
}
printf("\n");
printf("请输入班级有多少女生:");
scanf("%d",&g);
printf("请输入学生年纪和姓名:");
for(i = 0; i < g; i++)
{
creat_node(&new_node);
scanf("%d%s%s",&new_node->year,&new_node->name,&new_node->sex);
incret_node(&head2,new_node);
}
display_node(head1);
display_node(head2);
/* printf("请输入要查找的分数:");
scanf("%d",&m);
search_score(head,m);*/
release_node(&head1);
display_node(head1);
release_node(&head2);
display_node(head2);
return 0;
}
结果:
2、题目:将上面两个链表合并,按学生的年龄进行排序,合成新的链表
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node Node;
typedef struct node * Link;
struct node
{
int year;
char name[10];
Link next;
};
void creat_node(Link *node)
{
*node = (Link)malloc(sizeof(Node));
if(*node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
creat_node(head);
(*head)->next = NULL;
}
void incret_node(Link head,Link new_node)
{
Link p,q;
q = p = head->next;
if(head->next == NULL)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
if(head->next->year > new_node->year)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
while(p != NULL && p->year < new_node->year)
{
q = p;
p = p->next;
}
q->next = new_node;
new_node->next = p;
}
}
}
void conest_node(Link head,Link head1,Link head2)
{
Link p,q,t;
Link new_node;
p = head1->next;
q = head2->next;
while(p != NULL)
{
creat_node(&new_node);
new_node->year = p->year;
strcpy(new_node->name,p->name);
incret_node(head,new_node);
p = p->next;
}
while(q != NULL)
{
creat_node(&new_node);
new_node->year = q->year;
strcpy(new_node->name,q->name);
incret_node(head,new_node);
q = q->next;
}
}
void empty_node(Link head)
{
Link p;
p = head->next;
while(head->next != NULL)
{
head->next = p->next;
free(p);
p = head->next;
}
}
void release_head(Link *head)
{
Link p;
p = *head;
while(*head != NULL)
{
*head = (*head)->next;
free(p);
p = *head;
}
}
void display_node(Link head)
{
Link p;
if(head == NULL)
{
printf("The node is release!\n");
return;
}
if(head->next == NULL)
{
printf("The node is empty!\n");
return;
}
p = head->next;
while(p != NULL)
{
printf("year=%d,name=%s\n",p->year,p->name);
p = p->next;
}
}
int main()
{
Link head1 = NULL;
Link head2 = NULL;
Link head = NULL;
Link new_node = NULL;
int i,n,m;
creat_link(&head1);
creat_link(&head2);
creat_link(&head);
printf("Please input the number of the boys:");
scanf("%d",&n);
printf("Please input the year and name about the boys:\n");
for(i = 0; i < n; i++)
{
creat_node(&new_node);
scanf("%d %s",&new_node->year,&new_node->name);
incret_node(head1,new_node);
}
printf("Please input the number of the girls:");
scanf("%d",&m);
printf("Please input the year and name about the girls:\n");
for(i = 0; i < m; i++)
{
creat_node(&new_node);
scanf("%d %s",&new_node->year,&new_node->name);
incret_node(head2,new_node);
}
display_node(head1);
printf("\n");
display_node(head2);
printf("\n");
conest_node(head,head2,head1);
display_node(head);
printf("\n");
empty_node(head1);
display_node(head1);
empty_node(head2);
display_node(head2);
empty_node(head);
display_node(head);
release_head(&head1);
display_node(head);
release_head(&head2);
display_node(head);
release_head(&head);
display_node(head);
return 0;
}
结果:
3、题目:将上题中建立的链表进行反转,实现按年龄的逆序排列
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node Node;
typedef struct node * Link;
struct node
{
int year;
char name[10];
Link next;
};
void creat_node(Link *node)
{
*node = (Link)malloc(sizeof(Node));
if(*node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
creat_node(head);
(*head)->next = NULL;
}
void incret_node(Link head,Link new_node)
{
Link p,q;
q = p = head->next;
if(head->next == NULL)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
if(head->next->year >= new_node->year)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
while(p != NULL && p->year < new_node->year)
{
q = p;
p = p->next;
}
q->next = new_node;
new_node->next = p;
}
}
}
void conest_node(Link head,Link head1,Link head2)
{
Link p,q,t;
Link new_node;
p = head1->next;
q = head2->next;
while(p != NULL)
{
creat_node(&new_node);
new_node->year = p->year;
strcpy(new_node->name,p->name);
incret_node(head,new_node);
p = p->next;
}
while(q != NULL)
{
creat_node(&new_node);
new_node->year = q->year;
strcpy(new_node->name,q->name);
incret_node(head,new_node);
q = q->next;
}
}
void empty_node(Link head)
{
Link p;
p = head->next;
while(head->next != NULL)
{
head->next = p->next;
free(p);
p = head->next;
}
}
void release_head(Link *head)
{
Link p;
p = *head;
while(*head != NULL)
{
*head = (*head)->next;
free(p);
p = *head;
}
}
void nixu_node(Link head)
{
Link p1,p2,p3;
int i = 0;
p1 = head->next;
p3 = p2 = head->next->next;
while(p1 != NULL)
{
i++;
p1 = p1->next;
}
p1 = head->next;
if(i == 0)
{
return;
}
if(i == 1)
{
return;
}
if(i == 2)
{
p2 = p2->next;
p2->next = p1;
p1->next = NULL;
head->next = p2;
}
else
{
p1 = head->next;
p3 = head->next->next;
while(p3 != NULL)
{
p2 = p3;
p3 = p3->next;
p2->next = p1;
p1 = p2;
}
head->next->next = NULL;
head->next = p2;
}
}
void display_node(Link head)
{
Link p;
if(head == NULL)
{
printf("The node is release!\n");
return;
}
if(head->next == NULL)
{
printf("The node is empty!\n");
return;
}
p = head->next;
while(p != NULL)
{
printf("year=%d,name=%s\n",p->year,p->name);
p = p->next;
}
}
int main()
{
Link head1 = NULL;
Link head2 = NULL;
Link head = NULL;
Link new_node = NULL;
int i,n,m;
creat_link(&head1);
creat_link(&head2);
creat_link(&head);
printf("Please input the number of the boys:");
scanf("%d",&n);
printf("Please input the year and name about the boys:\n");
for(i = 0; i < n; i++)
{
creat_node(&new_node);
scanf("%d %s",&new_node->year,&new_node->name);
incret_node(head1,new_node);
}
printf("Please input the number of the girls:");
scanf("%d",&m);
printf("Please input the year and name about the girls:\n");
for(i = 0; i < m; i++)
{
creat_node(&new_node);
scanf("%d %s",&new_node->year,&new_node->name);
incret_node(head2,new_node);
}
display_node(head1);
printf("\n");
display_node(head2);
printf("\n");
conest_node(head,head2,head1);
display_node(head);
printf("\n");
nixu_node(head);
display_node(head);
empty_node(head1);
display_node(head1);
empty_node(head2);
display_node(head2);
empty_node(head);
display_node(head);
release_head(&head1);
display_node(head1);
release_head(&head2);
display_node(head2);
release_head(&head);
display_node(head);
return 0;
}
结果:
6、题目:实现一个函数,把两个从大到小的有序链表合并成一个链表,新的链表是一个从小到大的有序链表。
可以分成两个思路:
(1)先把两个链表合并成一个链表,合并后的链表从大到小排序
(2)将链表逆置,得到从小到达排序的链表
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node Node;
typedef struct node * Link;
struct node
{
int num;
Link next;
};
void creat_node(Link *node)
{
*node = (Link)malloc(sizeof(Node));
if(*node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
creat_node(head);
(*head)->next = NULL;
}
void incret_node(Link head,Link new_node)
{
Link p,q;
q = p = head->next;
if(head->next == NULL)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
if(head->next->num <= new_node->num)
{
new_node->next = head->next;
head->next = new_node;
}
else
{
while(p != NULL && p->num > new_node->num)
{
q = p;
p = p->next;
}
q->next = new_node;
new_node->next = p;
}
}
}
void conest_node(Link head,Link head1,Link head2)
{
Link p,q,t;
Link new_node;
p = head1->next;
q = head2->next;
while(p != NULL)
{
creat_node(&new_node);
new_node->num = p->num;
incret_node(head,new_node);
p = p->next;
}
while(q != NULL)
{
creat_node(&new_node);
new_node->num = q->num;
incret_node(head,new_node);
q = q->next;
}
}
void empty_node(Link head)
{
Link p;
p = head->next;
while(head->next != NULL)
{
head->next = p->next;
free(p);
p = head->next;
}
}
void release_head(Link *head)
{
Link p;
p = *head;
while(*head != NULL)
{
*head = (*head)->next;
free(p);
p = *head;
}
}
void nixu_node(Link head)
{
Link p1,p2,p3;
int i = 0;
p1 = head->next;
p3 = p2 = head->next->next;
while(p1 != NULL)
{
i++;
p1 = p1->next;
}
p1 = head->next;
if(i == 0)
{
return;
}
if(i == 1)
{
return;
}
if(i == 2)
{
p2 = p2->next;
p2->next = p1;
p1->next = NULL;
head->next = p2;
}
else
{
p1 = head->next;
p3 = head->next->next;
while(p3 != NULL)
{
p2 = p3;
p3 = p3->next;
p2->next = p1;
p1 = p2;
}
head->next->next = NULL;
head->next = p2;
}
}
void display_node(Link head)
{
Link p;
if(head == NULL)
{
printf("The node is release!\n");
return;
}
if(head->next == NULL)
{
printf("The node is empty!\n");
return;
}
p = head->next;
while(p != NULL)
{
printf("%3d",p->num);
p = p->next;
}
printf("\n");
}
int main()
{
Link head1 = NULL;
Link head2 = NULL;
Link head = NULL;
Link new_node = NULL;
int i,n,m;
creat_link(&head1);
creat_link(&head2);
creat_link(&head);
srand(time(NULL));
printf("Please input the number of the first:");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
creat_node(&new_node);
new_node->num = rand() % 100;
incret_node(head1,new_node);
}
printf("Please input the number of the towth:");
scanf("%d",&m);
for(i = 0; i < m; i++)
{
creat_node(&new_node);
new_node->num = rand() % 100;
incret_node(head2,new_node);
}
display_node(head1);
printf("\n");
display_node(head2);
printf("\n");
conest_node(head,head2,head1);
display_node(head);
printf("\n");
nixu_node(head);
display_node(head);
empty_node(head1);
display_node(head1);
empty_node(head2);
display_node(head2);
empty_node(head);
display_node(head);
release_head(&head1);
display_node(head1);
release_head(&head2);
display_node(head2);
release_head(&head);
display_node(head);
return 0;
}
结果:
7、题目:设单链表的表头指针为h,结点结构由data和next两个域构成,其中data域为字符型。
试设计算法判断该链表的前n个字符是否中心对称。例如xyx,xyyx都是中心对称。
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node Node;
typedef struct node * Link;
struct node
{
char c;
Link next;
Link prior;
};
void creat_node(Link *node)
{
*node = (Link)malloc(sizeof(Node));
if(*node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_link(Link *head)
{
creat_node(head);
(*head)->next = *head;
(*head)->prior = *head;
}
void incret_node(Link head,Link new_node)
{
Link p = NULL;
p = head->next;
if(head->next == head)
{
new_node->next = head->next;
new_node->prior = head;
head->next = new_node;
new_node->next->prior = new_node;
}
else
{
while(p->next != head)
{
p = p->next;
}
new_node->prior = p;
new_node->next = p->next;
p->next = new_node;
new_node->next->prior = new_node;
}
}
void check_node(Link head,int m)
{
Link p1,p2,p3;
p3 = p1 = p2 = head->next;
int t = 0;
int i = 1;
if(m % 2 != 0)
{
t = (m / 2) + 1;
p1 = head->next;
while(i != t)
{
p2 = p1;
p1 = p1->next;
i = i + 1;
}
p3 = p1->next;
while(p2->prior != head && p3->next != head && (p2->c) - (p3->c) == 0)
{
p2 = p2->prior;
p3 = p3->next;
}
if(p2->prior == head && p3->next == head)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
else
{
t = m / 2;
p1 = head->next;
i = 1;
while(i != t)
{
p1 = p1->next;
i++;
}
p2 = p1->next;
while(p1->prior != head && p2->next != head && (p2->c) - (p1->c) == 0)
{
p1 = p1->prior;
p2 = p2->next;
}
if(p2->next == head && p1->prior == head)
{
printf("yes!\n");
}
else
{
printf("no!\n");
}
}
}
void display_node(Link head)
{
Link p;
p = head->prior;
while(p != head)
{
printf("%c",p->c);
p = p->prior;
}
printf("\n");
}
int main()
{
Link head = NULL;
Link new_node = NULL;
int i,n,m;
creat_link(&head);
printf("Please input the number you want to input:");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
creat_node(&new_node);
scanf(" %c",&new_node->c);
incret_node(head,new_node);
}
display_node(head);
printf("Please input the m(m <= n):");
scanf("%d",&m);
check_node(head,m);
return 0;
}
结果: