- 题目:创建两个学生链表,含有姓名、年龄的信息,一个链表存放男生,一个链表存放女生,将两个链表合并,按学生的年龄进行排序,合成新的链表
#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;
}
- 题目:将上题中建立的链表进行反转,实现按年龄的逆序排列
#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;
}
- 在上面的实现的新链表中,给定一个年龄,迅速查找和该学生年龄最接近的学生姓名
提示:使用双向链表
#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;
}
- 题目:利用链表实现一个先入后出的栈结构,并提供栈操作的push和pop的接口
#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;
}