当前位置:编程学习 > C/C++ >>

C语言实现链表

  最近闲暇之际,重新看了一下C语言和数据结构,感觉链表很有意思,按照书上的思路自己写了一个重新,实现单向链表的操作,支持增加,删除,查找,读文件操作。先自己保留着,以后用的时候参考。
 
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(q); 
    } 

 
/* 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 li

补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,