链表的应用

顺序建立链表,代码如下:

#include #include struct node { int data; struct node *next; }; struct node *create(int n) { int i; struct node *head,*tail,*p; head = (struct node*)malloc(sizeof(struct node)); head->next=NULL; tail=head; for(i=0; idata); p->next=NULL; tail->next = p; tail = p; } return head; } int main() { int n; scanf("%d",&n); struct node *q,*head; head=create(n); q=head; while(q->next!=NULL) { //为了保证输出的最后一个数后面没有空格 if(q->next->next==NULL) { printf("%d",q->next->data); } else { printf("%d ",q->next->data); } q=q->next; } return 0; }

运用链表对数据进行从小到大排序,代码如下:
#include #include struct student { int num; float score; struct student *next; }; typedef struct student STU; STU *delete_(STU *head,float Score) { STU *p; while(head!=NULL) { if( head->score <=Score) { head = head->next; } else { break; } } p = head; if(p!=NULL) { while( p->next !=NULL) { if( p->next->score <=Score) { p->next = p->next->next; } else { p = p->next; } }} return head; } STU *sort_( STU *head ) { STU *newhead = NULL,*end_of_new; STU *p; STU *findhead; int firstfind = 1; float minscore; while( head !=NULL) //旧的链表不为空 { p = head; findhead = head; minscore = p->score; while( p!=NULL) { if(p->score < minscore) // 找到最小节点 { minscore = p->score; findhead = p; } p = p->next; }if(findhead == head)//第1次搜索最小,最为新链表头 { head = findhead->next; } else//否则,搜索节点链接到新链表后 { p = head; while(p->next!=NULL) { if(p->next->score == minscore) { p->next = p->next->next; break; //无需继续搜索,避免重复删除 } p = p->next; } }//构建新链表 if(firstfind == 1) //第一次构建,构建新链表头 { newhead = findhead; end_of_new = newhead; end_of_new->next = NULL; firstfind = 0; } else { end_of_new->next = findhead; end_of_new = findhead; end_of_new->next = NULL; } } return newhead; } int main() { STU st1,st2,st3; STU *head,*p,*t; float sst; st1.num = 101; st1.score=92; st2.num = 102; st2.score=82; st3.num = 103; st3.score=72; head = &st1; st1.next = &st2; st2.next = &st3; st3.next = NULL; t = (STU *)malloc(sizeof(STU)); if(t!=NULL) { t->num = 104; t->score = 62; t->next = NULL; } else { exit(0); } st3.next = t; p = head; while(p != NULL) { printf("\n%d\t%.2f",p->num,p->score); p = p->next; }/printf(" \n 排序结果:\n"); p =sort_(head); if(p == NULL) { printf("\n空链表"); } else { while(p != NULL) { printf("\n%d\t%.2f",p->num,p->score); p = p->next; } }return 0; }

删除比一个特定数值小的链表节点,代码如下:
#include #include struct student { int num; float score; struct student *next; }; typedef struct student STU; STU *delete_(STU *head,float Score) { STU *p; while(head!=NULL) { if( head->score <=Score) { head = head->next; } else { break; } } p = head; if(p!=NULL) { while( p->next !=NULL) { if( p->next->score <=Score) { p->next = p->next->next; } else { p = p->next; } }} return head; } int main() { STU st1,st2,st3; STU *head,*p,*t; float sst; st1.num = 101; st1.score=92; st2.num = 102; st2.score=91; st3.num = 103; st3.score=82; head = &st1; st1.next = &st2; st2.next = &st3; st3.next = NULL; t = (STU *)malloc(sizeof(STU)); if(t!=NULL) { t->num = 104; t->score = 72; t->next = NULL; } else { exit(0); } st3.next = t; p = head; while(p != NULL) { printf("\n%d\t%.2f",p->num,p->score); p = p->next; }/ printf(" \n 请输入限定值:\n"); scanf("%f",&sst); printf(" \n delete:\n"); p =delete_(head,sst); if(p == NULL) { printf("\n空链表"); } else { while(p != NULL) { printf("\n%d\t%.2f",p->num,p->score); p = p->next; } }return 0; }

【链表的应用】在尾部插入一个数值,代码如下:
#include #include struct student { int num; float score; struct student *next; }; typedef struct student STU; int main() { STU st1,st2,st3; STU *head,*p,*temp; st1.num = 1001; st1.score = 98; st2.num = 1002; st2.score = 100; st3.num = 1003; st3.score = 87.6; head = &st1; st1.next = &st2; st2.next = &st3; st3.next = NULL; temp =( STU *)malloc(sizeof(STU)); if( temp != NULL) { temp->num = 1004; temp->score = 86.9; temp->next = NULL; st3.next = temp; }p = head; while(p!=NULL) { printf("%d\t%.2f\n",p->num,p->score); p = p->next; }return 0; }

    推荐阅读