C语言实现链表

来源:http://www.prospettivedarte.com 作者:计算机教程 人气:62 发布时间:2019-11-15
摘要:C语言实现链表 - c语言程序开发技术文章 - 红黑联盟 http://www.BkJia.com/kf/201109/104029.html C语言实现链表 - c语言程序开发技术文章 - 红黑联盟 http://www.2cto.com/kf/201109/104029.html stdio.hstdlib.hco

C语言实现链表 - c语言程序开发技术文章 - 红黑联盟
http://www.BkJia.com/kf/201109/104029.html

C语言实现链表 - c语言程序开发技术文章 - 红黑联盟
http://www.2cto.com/kf/201109/104029.html

<stdio.h><stdlib.h><conio.h> 
 NULL 0 


 student *prior;  
     student * 
 student * student *head;  
     student * student *= ( student*)malloc((=->next =,&(x!== ( student*)malloc((->num =->next =->next =->prior ==,& 
 print( student * student *= head->(p==(p!=, p->= p-> 
 del( student * student * student * j=, &(i<, &=(j<=i- && p!== p->  (p===->prior->next = p->->next->prior = p->= p-> 
 insert( student * student * student *q = j = = ( student*)malloc((, &p->,p->, &(i<, &(j<=i- && q->next!== q->  (q->next==NULL && j!=->next =->prior =->next =->prior->next =->prior = q->->next =->prior = 
 search( student * student * number, i=, j=, &= head->(p!=(p->num=== = p->  (j== 
 student * student * student * student * i = * file[] = ((fp=fopen(file, ))=== ( student*)malloc((*== ( student*)malloc((*(fread(p1, (*p1), , fp)==  ->next ==->next == ( student*)malloc((*(i== 
 savefile( student* student ** file[] = ((fp=fopen(file,))=== head->(p1!=( student), = p1-> 

 a= student *= 
    (, & : insert(la);  : del(la);  : search(la);  : filein();  : savefile(la);  : print(la);  : showMenu();  : exit(: printf();
C代码 /*  * @author: lizhenbin * @date: 2011-09-14 * @descrption: list operation */ #include <stdio.h> #include <stdlib.h> #include <conio.h> #define NULL 0  /* define a link node strut */ struct student  {     long num;     struct student *prior; /* define point to the struct */     struct student *next; };  /* create struct */ struct student *create() {     struct student *head; /* link head and node point */     struct student *p;     struct student *q;     long x;      head = (struct student*)malloc(sizeof(struct student));     q = head;     head->next = NULL;     printf("nPlease input node numbers to the list: n");     scanf("%ld",&x);     while(x!=0)      {         /* create node */         p = (struct student*)malloc(sizeof(struct student));         p->num = x;         p->next = NULL;         q->next = p;         p->prior = q;         q = p;         scanf("%ld",&x);     }     return head; }  /* Output list all datas */ void print(struct student *head) {     struct student *p;     p = head->next;     printf("nThe list is: ");     if(p==NULL)      {         printf("nInput list is NULL!");     }     while(p!=NULL)      {         printf("%ld ", p->num);         p = p->next;     }     printf("n"); }  /* delete list node */ void del(struct student *head) {     struct student *p;     struct student *q;     int i;     int j=0;      printf("Please input delete position: ");     scanf("%d", &i);     if(i<1)      {         printf("You input %d error.", i);         printf("nTry again, Input nnum: ");         scanf("%d", &i);     }     p = head;     while(j<=i-1 && p!=NULL)      {         p = p->next;         j  ;     }     if(p==NULL)     {         printf("nThere is no %d in the list.", i);         exit(1);     }     else     {         /* delete list node */         q = p;         p->prior->next = p->next;         p->next->prior = p->prior;         p = p->next;         free;     } }  /* insert list node */ void insert(struct student *head) {     struct student *p;     struct student *q = head;     int i;     int j = 0;          p = (struct student*)malloc(sizeof(struct student));     printf("nPlease input the student No you want to insert: ");     scanf("%ld", &p->num);     printf("nInput the student %d position to insert: ",p->num);     scanf("%d", &i);     if(i<1)      {         printf("nYou input %d error.", i);         printf("nTry again, Input nnum: ");         scanf("%d", &i);     }     while(j<=i-1 && q->next!=NULL)      {         q = q->next;         j  ;     }     if(q->next==NULL && j!=i)     {         p->next = NULL;         p->prior = q;         q->next = p;     }     else     {         q->prior->next = p;         p->prior = q->prior;         p->next = q;         q->prior = q;     } }  /* search list node position and data */ void search(struct student *head) {     struct student *p;     long number, i=1, j=0;      printf("nPlease input the index you want to search: ");     scanf("%d", &number);     p = head->next;     printf("nThe number in the list position is: ");      while(p!=NULL)     {         if(p->num==number)         {             printf("%dn",i);             j = 1;         }         p = p->next;         i  ;     }     if(j==0)      {         printf("There is no %ld in the list.n",number);     } }  /* read data from file input */ struct student *filein()  {     struct student *p1;     struct student *p2;     struct student *head;     int i = 0;     FILE *fp;      char file[20] = "d:/list/node.txt";     printf("nFileName is: %sn", file);     if((fp=fopen(file, "r"))==NULL)     {         printf("Can not open file: %s", file);         exit(0);     }     p2 = (struct student*)malloc(sizeof(*p1));     head = p2;     p1 = (struct student*)malloc(sizeof(*p1));     while(fread(p1, sizeof, 1, fp)==1)     {         i  ;         p2->next = p1;         p2 = p1;         p1->next = NULL;         p1 = (struct student*)malloc(sizeof(*p1));     }     free;     fclose;     if(i==0)     {         printf("nNo recored in %s ", file);     }     return head; }  /* save the list result to file */ void savefile(struct student* head) {     struct student *p1;     FILE *fp;     char file[20] = "d:/list/node.txt";     printf("nFileName: %s", file);     printf("nPlease wait.../n");     if((fp=fopen(file,"w"))==NULL)     {         printf("nCan not open file: %s", file);         exit(0);     }     p1 = head->next;     while(p1!=NULL)     {         fwrite(p1, sizeof(struct student), 1, fp);         p1 = p1->next;     }     fclose; }  /* print the operate menu */ void showMenu() {     printf("|-------------------------------------------------|n");     printf("|            Link List Opearte Menu               |n");     printf("|-------------------------------------------------|n");     printf("|                 1: Insert                       |n");     printf("|                 2: Delete                       |n");     printf("|                 3: Search                       |n");     printf("|                 4: FileIn                       |n");     printf("|                 5: Savefile                     |n");     printf("|                 6: Print List                   |n");     printf("|                 7: Print Menu                   |n");     printf("|                 8: Exit                         |n");     printf("|-------------------------------------------------|n"); }  /* main funtion */ void main() {     int a=0;     struct student *la;     la = create();     /*    la = (struct student*)malloc(sizeof(struct student));    la->next = NULL;    */     while(1)     {         showMenu();         scanf("n%d", &a);         switch         {             case 1: insert; break;             case 2: del; break;             case 3: search; break;             case 4: filein(); break;             case 5: savefile; break;             case 6: print; break;             case 7: showMenu(); break;             case 8: exit(1);             default: printf("nInput error,Try again.");break;          }     } }作者“萧萧易水寒”

本文由皇牌天下投注网发布于计算机教程,转载请注明出处:C语言实现链表

关键词:

最火资讯