今天写了两个ppt,一个交换排序的,一个基数排序的
下午写了作业9,列出其中一些
1.有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位.提示:用环形链表实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct circle_node
{
int num;
struct circle_node *next;
};
typedef struct circle_node Cnode;
typedef struct circle_node *Link;
void create_node(Link *node)
{
*node = (Link)malloc(sizeof(Cnode));
if(node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void create_link(Link *head)
{
create_node(head);
(*head)->next = *head;
}
void insert_node_head(Link head,Link new_node)
{
new_node->next = head->next;
head->next = new_node;
}
void insert_node_tail(Link head,Link new_node)
{
Link p;
p = head;
while(p->next != head)
{
p = p->next;
}
p->next = new_node;
new_node->next = head;
}
void delete_node(Link head,Link p)
{
Link q;
q = head;
while(q->next != p)
{
q = q->next;
}
q->next = p->next;
free(p);
}
void release_node(Link head)
{
Link p;
p = head->next;
while(head->next != head)
{
head->next = (head->next)->next;
free(p);
p = head->next;
}
}
void release_link(Link *head)
{
Link p;
release_node(*head);
p = *head;
*head = (*head)->next;
free(p);
}
void display_link(Link head)
{
Link p;
if(head == NULL)
{
printf("no such link!\n");
return;
}
p = head->next;
if((head->next) == head)
{
printf("link is empty!\n");
return;
}
while(p != head)
{
printf("%d\n",p->num);
p = p->next;
}
}
int main()
{
int i;
Link head = NULL;
Link new_node = NULL;
int n;
create_link(&head);
printf("input a number for people:");
scanf("%d",&n);
for(i = 0;i < n;i++)
{
create_node(&new_node);
new_node->num = i+1;
// insert_node_head(head,new_node);
insert_node_tail(head,new_node);
}
display_link(head);
Link p;
p = head;
i = 1;
while(n > 1)
{
if(p == head)
{
p = p->next;
}
if(i % 3 == 0)
{
delete_node(head,p);
n--;
}
p = p->next;
i++;
}
printf("the last one is:\n");
display_link(head);
release_link(&head);
return 0;
}
2.创建一个学生学号及姓名的单链表,即节点包括学生学号、姓名及指向下一个节点的指针,链表按学生的学号排列。再从键盘输入某一学生姓名,将其从链表中删除。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 2
struct node
{
int num;
char name[20];
struct node *next;
};
typedef struct node Node;
typedef struct node *Link;
void create_link(Link *head)
{
*head = NULL;
}
void create_node(Link *new_node)
{
*new_node = (Link)malloc(sizeof(Node));
if(*new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void insert_node_tail(Link *head,Link new_node)
{
Link p;
p = *head;
if(*head == NULL)
{
*head = new_node;
new_node->next = NULL;
}
else
{
while(p->next != NULL)
{
p = p->next;
}
p->next = new_node;
new_node->next = NULL;
}
}
void delete_node(Link *head,char *del)
{
Link p,q;
p = q = *head;
if (!head || !del || !*head)
{
perror("delete");
return;
}
if(strcmp(del,p->name) == 0)
{
p = p->next;
free(p);
p = q = *head;
/* *head = p->next;
free(p);*/
}
else
{
/* q = p;
p = p->next;*/
while(p != NULL && strcmp(del,p->name) != 0 )
{
q = p;
p = p->next;
}
if(p)
{
q->next = p->next;
free(p);
}
}
}
void release_link(Link *head)
{
Link p;
p = *head;
while(*head != NULL)
{
*head = (*head)->next;
free(p);
p =*head;
}
}
void display_link(Link head)
{
Link p;
p = head;
if(p == NULL)
{
printf("link is empty!\n");
return;
}
else
{
while(p != NULL)
{
printf("No.%d\n",p->num);
printf("name:%s\n",p->name);
p = p->next;
}
}
}
int main()
{
int i;
Link head = NULL;
Link new_node = NULL;
int num;
char del[20];
create_link(&head);
for(i = 0;i < N;i++)
{
create_node(&new_node);
insert_node_tail(&head,new_node);
}
Link p;
p = head;
for(i = 0;i < N;i++)
{
printf("input student's number:");
scanf("%d",&p->num);
printf("input student's name:");
scanf("%s",&p->name);
p = p->next;
}
display_link(head);
printf("input the name to delete:");
scanf("%s",del);
delete_node(&head,del);
display_link(head);
release_link(&head);
display_link(head);
return 0;
}
5.题目:编程实现查找两个字符串的最大公共子串
示例:“aocdfe"和"pmcdfa"最大公共子串为"cfd”
#include <stdio.h>
#include <string.h>
void str(char *s1,char *s2,int len)
{
int i,j;
int m = 0;
int p,q;
int length = strlen(s2);
char a[100] = {'\0'};
char b[100] = {'\0'};
for(i = 0;i < len;i++)
{
for(j = 0;j < length;j++)
{
strcpy(a,"0");
p = i;
q = j;
while(s1[p] == s2[q] && s1[p] != '\0')
{
a[m] = s2[q];
p++;
q++;
m++;
}
if(strlen(a) > strlen(b))
{
strcpy(b,a);
}
m = 0;
}
}
printf("最大公共子串:%s\n",b);
}
int main()
{
char str1[100] = {'\0'};
char str2[100] = {'\0'};
int len;
printf("please input str1:");
fgets(str1,100,stdin);
printf("please input str2:");
fgets(str2,100,stdin);
len = strlen(str1) <= strlen(str2) ? strlen(str1) : strlen(str2);
if(len == strlen(str1))
{
str(str1,str2,len);
}
else if(len == strlen(str2))
{
str(str2,str1,len);
}
return 0;
}