C链表的简单案例

此案例只是简单的使用链表

链表的特点:

  1.不需要提前知道要存入数据的长度

  2.最后结点为NULL

  3.头结点指向下一个结点的结构体指针

#include <stdio.h>
#include <process.h>

struct Student{
    char cName[20];    //姓名 
    int iNumber;    //学号 
    struct Student *pNext;    //指向下一个结点的地址 
}; 

int iCount;    //全局变量,用来表示链表长度 

//创建链表返回链表的头指针 
struct Student *Create(){
    struct Student *pHead = NULL;    //定义头指针
    struct Student *pEnd, *pNew;    //最后一个结点和新的结点
    
    iCount = 0;    //初始化链表长度
    
    //给最后 一个结点和新结点分配内存空间 
    pEnd = pNew = (struct Student *)malloc(sizeof(struct Student)); 
    //提示让其输入姓名和 学号 
     printf("please enter the name,then the number\n");
     scanf("%s", &pNew->cName);
     scanf("%d", &pNew->iNumber);
     
     //判断输入的学号是否为0,不为0就进行处理 
     while(pNew->iNumber    !=0){
         iCount++;    //结点加1
        if(iCount == 1){
            pHead = pNew;    //头指针指向下个结点的地址 
            pNew->pNext = pHead;
            pEnd = pNew;    //pEnd这是跟踪的指针结点 
        }else{
            pEnd->pNext = pNew;    //让之前的元素的指针指向下一个结点的地址 
            pNew->pNext = NULL;    //让当前的元素的指针域指向NULL 
            pEnd = pNew;
        }
        //再给pNew分配内存空间 
        pNew = (struct Student *)malloc(sizeof(struct Student)); 
         printf("please enter the name,then the number\n");
         scanf("%s", &pNew->cName);
         scanf("%d", &pNew->iNumber);
     }
     free(pNew);
     return pHead;
}


//输出链表
void Print(struct Student *pHead){
    struct Student *pTemp;    //定义一个临时指针
    int iIndex = 0;    //用来计算有多少个指针输出
    
    printf("=====the links has %d element======\n", iCount);
    printf("|iIndex|   name   |iNumber|\n");
    pTemp = pHead;
    while(pTemp != NULL){
        iIndex++;
        printf("%6d", iIndex);
        printf("%10s", pTemp->cName);
        printf("%7d\n", pTemp->iNumber);
        pTemp = pTemp->pNext; 
    }
    printf("===================================\n");
} 

//插入元素,只需要头指针就行,并返回头指针 
struct Student *Insert(struct Student *pHead){
    struct Student *pNew;
    pNew = (struct Student *)malloc(sizeof(struct Student));    //分配内存空间
    printf("请输入name和number :\n");
    scanf("%s", &pNew->cName);
    scanf("%d", &pNew->iNumber);
    pNew->pNext = pHead;
    pHead = pNew; 
    iCount++;    
    return pHead;
} 

//删除元素, 传一个头指针,以及第几个元素,无返回值
void Delete(struct Student *pHead, int iIndex){
    int i;    //用于循环
    struct Student *pPre;    //定义前一个结构体指针变量 
    struct Student *pTemp;    //定义要删除的结构体指针变量 
    pPre = pTemp = pHead;    //都指向pHead 
    
    //循环的目的是让pTemp为要删除的结点,pPre为其前面一个结点 
    for(i=1; i<iIndex; i++){
        pPre = pTemp;
        pTemp = pTemp->pNext; 
    }
    
    pPre->pNext = pTemp->pNext;    //删除pTemp结点
    free(pTemp);
    iCount--; 
}

//写入文件
void Save(struct Student *pHead){
    FILE *p;    //定义一个文件指针 
    struct Student *pTemp;    //定义一个结构体指针变量 
    p = fopen("data.txt", "a+");
    if(p == NULL){
        printf("打开文件失败!\n");
    }
    pTemp = pHead;
    while(pTemp!=NULL){
        //fputs(pTemp->cName, p);
        fprintf(p, "%15s", pTemp->cName);
        fprintf(p, "%10d", pTemp->iNumber);
        fputs("\n", p);
        pTemp = pTemp->pNext;
    }
    fclose(p);    //关闭指针 
} 

int main(){
    struct Student *pHead;
    pHead = Create();
    pHead = Insert(pHead);
    Delete(pHead, 2);
    Save(pHead);
    Print(pHead);
    return 0;
}


效果:

 

posted @ 2016-04-29 11:32  天道酬勤,坚持!  阅读(4853)  评论(0编辑  收藏  举报