#include <stdio.h>#include <stdlib.h>
struct node //定义结点的结构体{ int num; char name[100];
struct node *next;};
typedef struct node Node; //将struct node简单定义为Nodetypedef struct node *Link; //将stcuct 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 ; p = head;
if(head == NULL) { PRintf("Link is empty!/n"); return; }
while(p != NULL) { printf("num = %d/n",p -> num); printf("name = %s/n",p -> name); printf("/n"); p = p -> next; //下一个 }}
void release_link (Link * head) //释放链表{ Link p; p = *head; while(*head != NULL) { p = *head; *head = (*head) -> next; free(p); }
}
void delete_node (Link * head, int number) //删除其中一个结点{ Link p = NULL; Link q = NULL; p = * head; q = * head;
if(*head == NULL) { return; } else { if((*head) -> num == number) { *head = p -> next; free(p); } else { while(p != NULL && p -> num != number) { q = p; p = p -> next; }
if(p == NULL) { printf("no node!/n"); } else if( p->next != NULL) { q -> next = p -> next; free(p); } else { q -> next = NULL; free(p); } } }}
int number_link (Link head) //计算链表长度并返回{ Link p; p = head; int num = 0;
while(p != NULL) { p = p -> next; num++; }
return num;
}
Link find_link (Link head, int n) //查找对应学号所在结点内容{ Link p; p = head;
while(p -> num != n && p != NULL) { p = p -> next; }
return p;}
void Flashback_link (Link *head) //倒叙输出链表{ Link p1; Link p2; Link p3;
p1 = *head; p2 = p1 -> next; p3 = p2 -> next;
if(*head == NULL || p1 -> next == NULL) { return; } else if(p2 -> next == NULL) { p2 -> next = p1; p1 -> next = NULL; *head = p2; } else { p2 -> next = p1;
while(p3 -> next != NULL) { p1 = p2; p2 = p3; p3 = p3 -> next; p2 -> next = p1; } p3 -> next = p2; (*head) -> next = NULL; *head = p3; }}
void enter_link (Link *head, Link enter_node,int number) //插入新的结点{ Link p = NULL; p = * head; if(*head == NULL) //空 { *head = enter_node; enter_node -> next = NULL; } else if(p -> num == number) { enter_node -> next = *head; *head = enter_node; } else { while(p != NULL && p -> num != number) { p = p -> next; } if(p != NULL) { p = *head; while((p->next) -> num != number) { p = p -> next; } (enter_node) -> next = (p -> next); p -> next = enter_node; } else { p = *head; while(p -> next != NULL) { p = p -> next; } p -> next = enter_node; enter_node -> next = NULL; }
}
}
int main(){ Link head = NULL; Link new_node = NULL; Link enter_node = NULL; int i; int n; int num; int number;
create_link(&head);
for(i = 0; i < 3; i++) { new_node = (Link)malloc(sizeof( Node )); //创建一个新的结点
if(new_node == NULL) //检查分配空间 { printf("malloc error!/n"); exit(-1); }
new_node -> num = i + 1; //赋值 scanf("%s", new_node -> name);
// insert_node_head(&head, new_node); insert_node_tail(&head, new_node); }
// display_link(head); //输出/* num = number_link(head); //返回结点数 printf("number = %d/n", num);
printf("Please input the num, find the name:/n"); scanf("%d",&n); printf("%s/n",find_link(head, n) -> name); // 输出查找号对应的名字 printf("/n");
printf("Please input the number of delete:/n"); scanf("%d",&number); delete_node (&head, number); //删除其中一个结点
flashback_link(&head); //倒叙输出*/
enter_node = (Link)malloc(sizeof(Node)); enter_node -> num = i + 1; printf("Please input the name:/n"); scanf("%s",enter_node -> name); printf("Please input the number of enter:/n"); scanf("%d",&number); enter_link(&head, enter_node, number); //在已经输入的结点里再插入一个新的结点
display_link(head); //输出
release_link (&head); //释放 display_link(head);
return 0;}
新闻热点
疑难解答