c语言实现链表的创建、增、删、改、查

    xiaoxiao2021-03-25  150

    // // main.c // 链表的基本操作 // Created by fzl // Copyright © fzl All rights reserved. // #include <stdio.h> #include <stdlib.h> #define FILENAME "/Users/fzl/Desktop/2.txt" struct Student { int num; char name[20]; }; typedef enum Same { isSame, isDifferent }Same; typedef struct Student Student; struct LINK { Student data; struct LINK *next; }; typedef struct LINK LINK; typedef LINK * pLINK; Same SameOrNot(pLINK head,Student stu) { pLINK temp=head->next; for (; temp!=NULL; temp=temp->next) { if(temp->data.num==stu.num) { return isSame; } } if(temp==NULL) { return isDifferent; } return isSame; } /** 创建链表 */ pLINK createList(pLINK head) { if(NULL==head) { //定义头结点 head=(pLINK)malloc(sizeof(LINK)); head->next=NULL; } printf("创建链表成功\n"); return head; } /** 打印链表 */ void printList(pLINK head) { if(head==NULL) { printf("没有创建链表\n"); return; } if(head->next==NULL) { printf("无信息打印\n"); return; } pLINK temp=head->next; for (; temp!=NULL; temp=temp->next) { printf("[%d %s]--->",temp->data.num,temp->data.name); } printf("NULL\n"); } Student getData(pLINK head) { Student stu; while (1) { printf("请输入插入学生的信息[学号 姓名]:"); scanf("%d%s",&stu.num,stu.name); if(SameOrNot(head, stu)==isSame) { printf("输入重复\n"); } else { return stu; } } return stu; } /** 头插 */ void headInsert(pLINK head) { //新建节点 pLINK p=(pLINK)malloc(sizeof(LINK)); //填补数据域 p->data=getData(head); //将新建的节点连到链表中 p->next=head->next; head->next=p; printf("头插成功\n"); } void tailInsert(pLINK head) { pLINK p=(pLINK)malloc(sizeof(LINK)); p->data=getData(head); if(head->next==NULL) { head->next=p; p->next=NULL; return ; } pLINK tail=head->next; for (;tail->next!=NULL; tail=tail->next) { } //如果循环能跳出,则tail->next==NULL,说明tail就是尾指针 tail->next=p; p->next=NULL; printf("尾插成功\n"); } int getListLength(pLINK head) { pLINK temp=head->next; int length=0; for (; temp!=NULL; temp=temp->next) { length++; } return length; } void randomInsert(pLINK head) { int index=0; printf("请输入要插入的位置:"); scanf("%d",&index); if(index<=0||index>getListLength(head)+1) { printf("你输入的位置有误\n"); } if(index==1) { headInsert(head); } else if (index==getListLength(head)+1) { tailInsert(head); } else { pLINK front; int i; for (front=head->next,i=1; i<index-1; front=front->next,i++) { } pLINK p=(pLINK)malloc(sizeof(LINK)); p->data=getData(head); p->next=front->next; front->next=p; } printf("任意插入数据成功\n"); } pLINK insertData(pLINK head) { if(head==NULL) { head=createList(head); } int select; while (1) { printf("=========\n"); printf("1.头插\n"); printf("2.尾插\n"); printf("3.任意位置插入\n"); printf("4.返回上一层\n"); printf("=========\n"); scanf("%d",&select); switch (select) { case 1: headInsert(head); break; case 2: tailInsert(head); break; case 3: randomInsert(head); break; case 4: return head; default: break; } } return head; } void tailDelete(pLINK head) { if(head==NULL||head->next==NULL) { printf("无数据可删\n"); return; } pLINK front=head; for (; front->next->next!=NULL; front=front->next) { } //front->next->next==NULL pLINK tail=front->next; front->next=NULL; free(tail); printf("尾删成功\n"); } void headDelete(pLINK head) { if(head==NULL||head->next==NULL) { printf("无数据可删\n"); return; } pLINK current=head->next; head->next=current->next; free(current); printf("删除成功\n"); } void randomDelete(pLINK head) { if(head==NULL||head->next==NULL) { printf("无数据可删\n"); return; } printf("请输入要删除的位置:"); int index=0; scanf("%d",&index); if(index<=0||index>getListLength(head)) { printf("输入位置有误\n"); return; } if(index==1) { headDelete(head); } else if (index==getListLength(head)) { tailDelete(head); } else { pLINK front; int i; for (front=head->next,i=1; i<index-1; front=front->next,i++) { } pLINK current=front->next; front->next=current->next; free(current); } printf("任意删除数据成功\n"); } void deleteData(pLINK head) { if(head==NULL||head->next==NULL) { printf("无数据可删\n"); return; } int select; while (1) { printf("=========\n"); printf("1.头删\n"); printf("2.尾删\n"); printf("3.任意位置删除\n"); printf("4.返回上一层\n"); printf("=========\n"); scanf("%d",&select); switch (select) { case 1: headDelete(head); break; case 2: tailDelete(head); break; case 3: randomDelete(head); break; case 4: return; default: break; } } } void searchData(pLINK head) { if(head==NULL||head->next==NULL) { printf("无数据查询\n"); return; } printf("请输入要查询的学生的学号:"); int num; scanf("%d",&num); pLINK temp=head->next; for (; temp!=NULL; temp=temp->next) { if(temp->data.num==num) { printf("这个学生的信息[%d %s]\n",temp->data.num,temp->data.name); return; } } if(temp==NULL) { printf("查无此人\n"); return; } } void changeData(pLINK head) { if(head==NULL||head->next==NULL) { printf("无数据查询\n"); return; } printf("请输入要修改的学生的学号:"); int num; scanf("%d",&num); pLINK temp=head->next; for (; temp!=NULL; temp=temp->next) { if(temp->data.num==num) { printf("这个学生原信息为[%d %s]\n",temp->data.num,temp->data.name); printf("请输入要修改的信息[学号 姓名]:"); scanf("%d%s",&temp->data.num,temp->data.name); return; } } if(temp==NULL) { printf("查无此人\n"); return; } } pLINK readFileToMemory() { pLINK head=(pLINK)malloc(sizeof(LINK)); pLINK p=head; //1.打开文件 FILE *fp=fopen(FILENAME, "r"); if(fp==NULL) { perror("fopen failed"); return NULL; } //2. int length=0; fread(&length, sizeof(int), 1, fp); int i=1; for (; i<=length; i++) { pLINK q=(pLINK)malloc(sizeof(LINK)); fread(&q->data, sizeof(Student), 1, fp); p->next=q; q->next=NULL; p=p->next; } //3 fclose(fp); return head; } void saveDataToFile(pLINK head) { if(head==NULL||head->next==NULL) { printf("无数据可保存\n"); return; } //1.打开文件 FILE *fp=fopen(FILENAME, "w"); if(fp==NULL) { perror("fopen failed"); return; } int length=getListLength(head); fwrite(&length, sizeof(int), 1, fp); //2.对文件进行写 pLINK temp=head->next; for (; temp!=NULL; temp=temp->next) { fwrite(&temp->data, sizeof(Student), 1, fp); } //3.关闭文件 fclose(fp); } int main() { pLINK head=NULL; int select; while (1) { printf("=========\n"); printf("1.创建链表\n"); printf("2.插入数据\n"); printf("3.删除数据\n"); printf("4.打印链表\n"); printf("5.查询数据\n"); printf("6.修改数据\n"); printf("7.保存数据到文件\n"); printf("8.读取文件\n"); printf("0退出\n"); printf("=========\n"); scanf("%d",&select); switch (select) { case 1: head=createList(head); break; case 2: head=insertData(head); break; case 3: deleteData(head); break; case 4: printList(head); break; case 5: searchData(head); break; case 6: changeData(head); break; case 7: saveDataToFile(head); break; case 8: head=readFileToMemory(); break; case 0: return 0; default: break; } } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-2605.html

    最新回复(0)