Training5:指针和链表训练

来源:互联网 发布:centos共享文件夹在哪 编辑:程序博客网 时间:2024/04/30 18:35

1. 题目:n个人围成一圈,顺序排号,从第一个开始报数(13报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位.

提示:用环形链表实现

#include <stdio.h>#include <stdlib.h>struct num{int num;struct num *next;};struct num *head;void creat_num(){head=(struct num*)malloc(sizeof(struct num));head=NULL;}void in_num(int num){struct num *p=(struct num *)malloc(sizeof(struct num));p->num=num;p->next=head;head=p;}void rm_num(int n){     struct num *p=NULL;     struct num *temp=NULL;     int i=10;     int count=0;     while(i>1)      {          if(p==NULL){p=head;}count++;if(count==n){if(head==p){temp=head;head=head->next;p=head;free(temp);temp=NULL;}else{                 temp->next=p->next;temp=p;p=p->next;free(temp);temp=NULL;}i--;count=0;}else{        temp=p;p=p->next;}     }}void display_num(){       struct num *p=head;       while(p)       {            printf("%d\t",p->num);    p=p->next;       }       printf("\n");}int main(){int i=0;int delete;printf("please input a num:");scanf("%d",&delete);if(delete<0){printf("input error!\n");}printf("delete=%d\n",delete);creat_num();        for(i=10;i>0;i--){     in_num(i);}display_num();rm_num(delete);printf("output:");display_num();return 0;}


 

2. 题目:创建两个学生链表,含有姓名、年龄的信息,一个链表存放男生,一个链表存放女生

#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct str{     char name[10];     int age;     struct str *next;}str;str *insert_list(str *temp,char name[],int age){     str *p = (str*)malloc(sizeof(str));     strcpy(p->name,name);     p->age = age;     p->next = temp;     temp = p;     return temp;}int display_list(str *p){    printf("out:\n");    while(p)    {         printf("%s\t%d\n",p->name,p->age); p = p->next;    }}int main(){    str *Man = NULL;    str *Woman = NULL;    Man = insert_list(Man,"zhangsan",12);    Man = insert_list(Man,"lisi",12);    Man = insert_list(Man,"zhaosan",13);    Man = insert_list(Man,"wangwu",15);    printf("男");    display_list(Man);    Woman = insert_list(Woman,"zhangli",12);    Woman = insert_list(Woman,"shenli",12);    Woman = insert_list(Woman,"xiaoli",12);    printf("女");    display_list(Woman);    return 0;}

3. 题目:将上面两个链表合并,按学生的年龄进行排序,合成新的链表

#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct str{     char name[10];     int age;     struct str *next;}str;str *insert_list(str *temp,char name[],int age){     str *p = (str*)malloc(sizeof(str));     strcpy(p->name,name);     p->age = age;     p->next = temp;     temp = p;     return temp;}int display_list(str *p){    printf("out:\n");    while(p)    {         printf("%s\t%d\n",p->name,p->age); p = p->next;    }}str *connect(str *q,str *p){    str *temp=NULL;    temp = q;    if(temp==NULL)    {        return p;    }    while(temp->next!=NULL)    {        temp = temp->next;    }    temp->next = p;    return q;}str *rank_list(str *p){   str *temp = p;   str *ptr =  p;   int  between=0;   char string[10];   while(temp->next)   {       while(ptr->next)       {            if((ptr->age)>(ptr->next->age))    {        between = ptr->age;ptr->age = ptr->next->age;ptr->next->age = between;strcpy(string,ptr->name);strcpy(ptr->name,ptr->next->name);strcpy(ptr->next->name,string);    }    ptr = ptr->next;       }       temp = temp->next;       ptr = p;   }   return p;}int main(){    str *student = NULL;    str *Man = NULL;    str *Woman = NULL;    Man = insert_list(Man,"zhangsan",14);    Man = insert_list(Man,"lisi",13);    Man = insert_list(Man,"zhaosan",12);    Man = insert_list(Man,"wangwu",15);    Woman = insert_list(Woman,"zhangli",16);    Woman = insert_list(Woman,"shenli",10);    Woman = insert_list(Woman,"xiaoli",11);    student=connect(Man,Woman);    student=rank_list(student);    printf("学生");    display_list(student);    return 0;}


 

4. 题目:将上题中建立的链表进行反转,实现按年龄的逆序排列

#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct str{     char name[10];     int age;     struct str *next;}str;str *insert_list(str *temp,char name[],int age){     str *p = (str*)malloc(sizeof(str));     strcpy(p->name,name);     p->age = age;     p->next = temp;     temp = p;     return temp;}int display_list(str *p){    printf("out:\n");    while(p)    {         printf("%s\t%d\n",p->name,p->age); p = p->next;    }}str *connect(str *q,str *p){    str *temp=NULL;    temp = q;    if(temp==NULL)    {        return p;    }    while(temp->next!=NULL)    {        temp = temp->next;    }    temp->next = p;    return q;}str *rank_list(str *p){   str *temp = p;   str *ptr =  p;   int  between=0;   char string[10];   while(temp->next)   {       while(ptr->next)       {            if((ptr->age)<(ptr->next->age))    {        between = ptr->age;ptr->age = ptr->next->age;ptr->next->age = between;strcpy(string,ptr->name);strcpy(ptr->name,ptr->next->name);strcpy(ptr->next->name,string);    }    ptr = ptr->next;       }       temp = temp->next;       ptr = p;   }   return p;}int main(){    str *student = NULL;    str *Man = NULL;    str *Woman = NULL;    Man = insert_list(Man,"zhangsan",14);    Man = insert_list(Man,"lisi",13);    Man = insert_list(Man,"zhaosan",12);    Man = insert_list(Man,"wangwu",15);    Woman = insert_list(Woman,"zhangli",16);    Woman = insert_list(Woman,"shenli",10);    Woman = insert_list(Woman,"xiaoli",11);    student=connect(Man,Woman);    student=rank_list(student);    printf("学生");    display_list(student);    return 0;}


 

5. 题目:在上面的实现的新链表中,给定一个年龄,迅速查找和该学生年龄最接近的学生姓名

提示:使用双向链表

#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct stu{     char name[10];     int age;     struct stu *next;     struct stu *before;}stu;stu * insert_list(stu *p,char name[],int age){     stu *temp = (stu*)malloc(sizeof(stu));     strcpy(temp->name,name);     temp->age = age;     if(p==NULL)     {        temp->next = NULL;temp->before = NULL;        p = temp;return p;     }     temp->before=NULL;     temp->next=p;     p->before=temp;     p = temp;     return p;}void find_list(stu *p){     int a[50]={0};     stu *temp = p;     int age=0;     int i=0;     int j=0;     int num=100;     int match=-1;     printf("请输入一个年龄:");     scanf("%d",&age);     while(temp)     {         a[i] = (temp->age) - age; temp = temp->next; if(a[i]<0) {    a[i]=0-a[i]; } i++;     }     for(j=0;j<i;j++)     {         if(a[j]<num) {    num = a[j];    match = j; }     }     temp = p;     for(j=0;j<match;j++)     {         temp = temp ->next;     }     printf("最匹配的同学:%s\n",temp->name);}void display(stu *student){     stu *p = student;     while(p)     {          printf("%s , %d\n",p->name,p->age);  p = p->next;     }}int main(){     stu *student=NULL;     student=insert_list(student,"zhangsan",11);     student=insert_list(student,"lisi",12);     student=insert_list(student,"wangwu",13);     student=insert_list(student,"zhaoyi",14);     student=insert_list(student,"qianer",15);     display(student);     find_list(student);     return 0;}

6. 题目:利用链表实现一个先入后出的栈结构,并提供栈操作的pushpop的接口

#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct node{       int num;       struct node *next;}node;node * Push(node *p,int num){      node *temp= (node *)malloc(sizeof(node));      temp->num=num;      temp->next = p;      p = temp;}node * Pop(node *p,int num){      node *temp = p;      p = p->next;      free(temp);      temp = NULL;}void display(node *p){     node *temp = p;     printf("遍历:");     while(temp)     {         printf("%d\t",p->num); p = p->next;     }     printf("\n");}int main(){     return 0;}




 

原创粉丝点击