链表的一些简单操作

来源:互联网 发布:ai mac中文版免费下载 编辑:程序博客网 时间:2024/06/11 12:39

本文详细介绍了链表的一些简单的操作,包括链表的创建、遍历、查询、插入、删除、排序、逆序、释放。另附运行结果。

#include <stdio.h>#include <stdlib.h>typedef struct student{    int num;int score;char name[20];struct student *next;}STU;void link_create(STU **head, STU *p_new)//创建链表{    STU *p_mov = *head;    if (*head == NULL)    {        *head = p_new;p_new->next = NULL;}else{        while (p_mov->next != NULL)//找到最后的位置        {            p_mov = p_mov->next;}p_mov->next = p_new;p_new->next = NULL;}}void link_print(STU *head)//遍历链表{    STU *p_mov = head;while (p_mov != NULL)//遍历所有的节点{    printf("num: %d,score: %d,name: %s\n",p_mov->num,p_mov->score,p_mov->name);        p_mov = p_mov->next;}}STU *link_search_num(STU *head, int num)//查询链表{    STU *p_mov = head;if (head = NULL){        printf("Sorry,Link is NULL!\n");}else {        while (p_mov != NULL)        {            if (p_mov->num == num)//找到要查找的节点return p_mov;else                p_mov = p_mov->next;}if (p_mov == NULL)printf("No such Node!\n");return NULL;}}void link_delete_num(STU **head, int num)//删除链表{    STU *p_mov = *head;    STU *pf = NULL;if (*head == NULL){        printf("Link is NULL!\n");return ;}while (p_mov->num != num && p_mov->next != NULL){        pf = p_mov;p_mov = p_mov->next;}if (p_mov->num == num){    if (p_mov == *head)//要删除的节点是头结点    {            *head = p_mov->next;free(p_mov);}else//要删除的节点是其它节点{            pf->next = p_mov->next;free(p_mov);}}else{        printf("No such student you want to delete!\n");}}void link_insert_num(STU **head, STU *p_new)//插入链表{    STU *pf,*pb;pb = *head;if (*head == NULL){        *head = p_new;p_new->next = NULL;return ;}while (p_new->num >= pb->num && pb->next != NULL){        pf = pb;pb = pb->next;}if (p_new->num < pb->num)//找到插入的合适的位置{        if (pb == *head)        {            p_new->next = *head;*head = p_new;}else{            pf->next = p_new;p_new->next = pb;}}else{        pb->next = p_new;p_new->next = NULL;}}void link_order(STU *head)//链表排序{    STU *pb = NULL;STU temp;STU *pf = head;if (head == NULL)return ;while (pf->next != NULL){        pb = pf->next;while (pb != NULL){            if (pf->num > pb->num)            {                temp = *pb;*pb = *pf;*pf = temp;temp.next = pb->next;pb->next = pf->next;pf->next = temp.next;}pb = pb->next;}pf = pf->next;}}STU *link_reverse(STU **head)//链表逆序{    STU *pf,*pb,*r;if (*head == NULL)return ;if ((*head)->next == NULL)return ;pf = *head;pb = pf->next;pf->next = NULL;//记录头结点节点的下一个节点,然后将头结点逆序while (pb != NULL)//记录要逆序的节点的下一个节点,然后逆序{        r = pb->next;pb->next = pf;(*head) = pb;//移动头指针,使其始终指向头结点pf = pb;//移动其它指针,为下一次循环准备pb = r;}return (*head);}void link_free(STU **head)//链表释放{    STU *pb;while(*head != NULL){        pb = *head;*head = (*head)->next;free(pb);}}int main(int argc, char * argv [ ]){    int number = 0;int i = 0;char disbuff[50] = "";STU *p_new = NULL;STU *head = NULL;STU *result = NULL;    printf("Please enter the number to create link.\n");scanf("%d",&number);for (i=0;i<number;i++){sprintf(disbuff,"please enter the num of %d message.",i);printf("%s\n",disbuff);p_new = (STU *)malloc(sizeof(STU));scanf("%d %d %s",&p_new->num,&p_new->score,p_new->name);link_create(&head, p_new);}link_print(head);printf("-------------------------------------------------------------\n");link_order(head);link_print(head);printf("-------------------------------------------------------------\n");printf("Please enter the num you want to search:\n");scanf("%d",&number);result = link_search_num(head, number);if (result != NULL){        printf("result:num:%d, score:%d, name:%s\n",result->num,result->score,result->name);}printf("-------------------------------------------------------------\n");printf("Please enter the num you want to delete:\n");scanf("%d",&number);link_delete_num(&head, number);link_print(head);printf("-------------------------------------------------------------\n");printf("Please enter the num score name you want to insert:\n");p_new = (STU *)malloc(sizeof(STU));scanf("%d %d %s",&p_new->num,&p_new->score,p_new->name);link_insert_num(&head,p_new);link_print(head);printf("-------------------------------------------------------------\n");link_reverse(&head);link_print(head);}
一个运行示例:

[link]gcc link_print.c [link]./a.out Please enter the number to create link.4please enter the num of 0 message.11 77 qqplease enter the num of 1 message.22 88 wwplease enter the num of 2 message.33 97 eeplease enter the num of 3 message.21 89 rrnum: 11,score: 77,name: qqnum: 22,score: 88,name: wwnum: 33,score: 97,name: eenum: 21,score: 89,name: rr-------------------------------------------------------------num: 11,score: 77,name: qqnum: 21,score: 89,name: rrnum: 22,score: 88,name: wwnum: 33,score: 97,name: ee-------------------------------------------------------------Please enter the num you want to search:22result:num:22, score:88, name:ww-------------------------------------------------------------Please enter the num you want to delete:33num: 11,score: 77,name: qqnum: 21,score: 89,name: rrnum: 22,score: 88,name: ww-------------------------------------------------------------Please enter the num score name you want to insert:18 99 ttnum: 11,score: 77,name: qqnum: 18,score: 99,name: ttnum: 21,score: 89,name: rrnum: 22,score: 88,name: ww-------------------------------------------------------------num: 22,score: 88,name: wwnum: 21,score: 89,name: rrnum: 18,score: 99,name: ttnum: 11,score: 77,name: qq



0 0