程序一、排序,边插边排
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
char name[20];
int score;
struct node *next;
};
typedef struct node Node;
typedef struct node *Link;
void create_link (Link * head)
{
*head = NULL;
}
void insert_node_head (Link *head, Link new_node) //头插
{
new_node -> next = *head;
*head = new_node;
}
void insert_rank_node (Link *head, Link new_node) //插入的时候排序
{
Link p = NULL;
p = *head;
if(*head == NULL) //空
{
*head = new_node;
new_node -> next = NULL;
}
else if(p -> next == NULL) //一个结点
{
if(new_node -> num > p -> num)
{
p -> next = new_node;
new_node -> next = NULL;
}
else
{
new_node -> next = *head;
(*head) = new_node;
}
}
else //多个结点
{
if((p -> num) > (new_node -> num)) //第一个结点的学号大于new_node的学号,大于则new_node插最前面
{
new_node -> next = *head;
*head = new_node;
}
else
{
while(p -> next != NULL) //循环到最后一个结点
{
p = p -> next;
}
if((p -> num) < (new_node -> num)) //比较最后一个结点的学号是否小于new_node的学号,小于则new_node插最后
{
p -> next = new_node;
new_node -> next = NULL;
}
else
{
p = *head;
while(((p -> next) -> num) < (new_node -> num)) //查找的结点在中间
{
p = p -> next;
}
new_node -> next = p -> next;
p -> next = new_node;
}
}
}
}
void display_link(Link head) //输出
{
Link p = NULL;
p = head;
if(head == NULL)
{
printf("Link is NULL!\n");
return;
}
while(p != NULL)
{
printf("num = %d\n", p -> num);
printf("name is %s\n", p -> name);
printf("score = %d\n", p -> score);
printf("\n");
p = p -> next;
}
}
void release_link (Link *head) //释放
{
Link p;
while(*head != NULL)
{
p = *head;
*head = (*head) -> next;
free(p);
}
}
int main()
{
Link head = NULL;
Link new_node = NULL;
// int i;
int flag = 1; //标志,当输入为1 时继续输入,0时退出
create_link(&head);
while(flag == 1)
// for(i = 0; i < 2; i++)
{
new_node = (Link)malloc(sizeof(Node));
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
scanf("%d", &(new_node -> num));
scanf("%s", new_node -> name);
scanf("%d", &(new_node -> score));
insert_rank_node(&head, new_node);
// insert_node_head(&head, new_node);
scanf("%d",&flag);
}
display_link(head);
release_link(&head);
display_link(head);
return 0;
}
程序二、查找其中一个结点并替换
#include <stdio.h>
#include <stdlib.h>
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 insert_node_head(Link *head, Link new_node) //头插
{
new_node->next = *head;
*head = new_node;
}
void insert_node_tail(Link *head, Link new_node) //尾插
{
Link p = NULL;
p = *head;
if(p == 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 display_link (Link head) //输出
{
Link p = NULL;
p = head;
if(head == NULL)
{
printf("head is NULL!");
return;
}
while(p != NULL)
{
printf("%d\t",p->num);
printf("%s\t",p->name);
printf("\n");
p = p -> next;
}
}
void release_link (Link *head) //释放
{
Link p = NULL;
while(*head != NULL)
{
p = *head;
*head = (*head) -> next;
free(p);
}
}
void search_change_node (Link *head, Link change_node, int num) //查找并修改
{
Link p = NULL;
Link q = NULL;
q = *head;
p = *head;
while(p -> num != num && p != NULL)
{
q = p;
p = p -> next;
}
if(p == NULL)
{
printf("No find!");
return;
}
else if(p -> next == NULL)
{
change_node -> next = NULL;
q -> next = change_node;
}
else
{
change_node -> next = p -> next;
q -> next = change_node;
}
}
int main()
{
Link head = NULL;
Link new_node = NULL;
Link change_node = NULL;
int flag = 1;
int num;
create_link(&head);
while(flag == 1)
{
new_node = (Link)malloc(sizeof(Node));
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
scanf("%d",&(new_node -> num));
scanf("%s", new_node -> name);
insert_node_tail(&head, new_node);
// insert_node_head(&head, new_node);
scanf("%d",&flag);
}
printf("Please input the number of search and change:");
scanf("%d",&num); //查找的学号
change_node = (Link)malloc(sizeof(Node));
printf("Please input the number and name:");
scanf("%d",&(change_node->num)); //修改的学号
scanf("%s",change_node->name); //修改的名字
search_change_node(&head, change_node, num); //查找并修改
display_link(head);
release_link(&head);
display_link(head);
return 0;
}