单链表常见操作

来源:互联网 发布:sql美化工具 编辑:程序博客网 时间:2024/06/05 07:34
//student.h//定义学生类型结构体#include <stdio.h>typedef struct student{int num;  //学号char *name[32]; //姓名struct student *next;}*pStu , Stu;//int LEN = sizeof(Stu);
//UI.h//界面相关#include <stdio.h>//显示菜单void ShowMenu(void);
UI.c

#include <stdlib.h>#ifndef _H_UI_#define _H_UI_#include "UI.h"#endif//显示菜单void ShowMenu(void){system("cls");puts("\t+=================================================================+");puts("\t|                                                                 |");puts("\t|                              Menu                               |");puts("\t|                                                                 |");puts("\t|-----------------------------------------------------------------|");puts("\t|                                                                 |");puts("\t|      1.创建链表                                                 |");puts("\t|                                                                 |");puts("\t|      2.显示所有结点                                             |");puts("\t|                                                                 |");puts("\t|      3.删除指定学号结点                                         |");puts("\t|                                                                 |");puts("\t|      4.插入学生                                                 |");puts("\t|                                                                 |");puts("\t|      5.退出                                                     |");puts("\t|                                                                 |");puts("\t|                               【请输入相应操作编号并回车】      |");puts("\t|                                                                 |");puts("\t+=================================================================+");//system("pause");}
//LinkedList.h//声明链表操作相关函数#ifndef _H_STUDENT_#define _H_STUDENT_#include "student.h"#endifpStu Create();             //创建链表void PrintAll(pStu head);  //打印所有结点pStu Delete(pStu head , int num); //删除学号为num的学生结点pStu Insert(pStu head , pStu newStu);//插入新结点
<pre name="code" class="cpp">//LinkedLIst.c
#include "LinkedList.h"#include <stdio.h>#include <stdlib.h>const int LEN = sizeof(Stu);//创建链表//每次输入的值赋给p1,然后把p1加入以p2结尾的链表尾pStu Create(){pStu p1,p2,head;head = NULL;p1 = (pStu)malloc(LEN);  //申请内存p2 = NULL;printf("请输入学号和姓名:");scanf("%d%s" , &p1->num , p1->name);while(p1->num){if(!head)   //如果链表为空,即插入第一个结点{head = p1;}else{p2->next = p1;}p2 = p1;p1 = (pStu)malloc(LEN);  //申请内存printf("请输入学号和姓名(学号为0时结束):");scanf("%d%s" , &p1->num , p1->name);}p2->next = NULL;return head;}//打印所有链表结点void PrintAll(pStu head){if(!head){printf("链表为空!\n");return;}printf("学号\t姓名\n\n");while(head){printf("%d\t%s\n" , head->num , head->name);head = head->next;}}//删除学号为num的学生结点pStu Delete(pStu head , int num){pStu p1,p2;if(!head || num<=0) //验证数据有效性{printf("链表为空或学号为负数!\n");return head;}if(head->num == num)   //如果头结点是要删除的点{p2 = head;head = head->next;printf("删除成功!\n");return head;}p1 = head;while(p1){if(p1->num > num) //如果当前遍历的结点的学号大于num,表明此链表中没有要找的结点,所以直接退出循环return head;if(p1->next && p1->next->num == num){p2 = p1->next;  //把将要删除的结点赋给p2p1->next = p1->next->next;  //改变p1向后链接的指针//return p2;return head;}p1 = p1->next;}}//插入新结点pStu Insert(pStu head , pStu newStu){pStu p1;if(!head || !newStu){printf("参数错误!\n");return head;}if(head->num > newStu->num){newStu->next = head;head = newStu;printf("插入成功!\n");return head;}p1 = head;while(p1){if(p1->num == newStu->num) //是否存在学号相等结点{printf("此学号结点已经存在!\n");return head;}if( p1->next)//如果不是最后一个结点{if(p1->num < newStu->num && p1->next->num > newStu->num){newStu->next = p1->next;p1->next = newStu;printf("插入成功!\n");return head;}}else  //如果是最后一个结点{p1->next = newStu;p1 = newStu;printf("插入成功!\n");return head;}p1 = p1->next;}}
//main.c
#include <stdio.h>#include <stdlib.h>#include "LinkedList.h"#include "UI.h"int main(int argc , char *argv[]){pStu head = NULL;   //保存链表头结点pStu p1 = NULL;     //保存删除的结点pStu newStu = NULL;   //插入新结点int c;              //记录菜单选号int flag = 1;       //循环标记int num;            //学号while(flag){ShowMenu();      //显示菜单scanf("%d",&c);  //输入选项switch(c){case 1:head = Create();      //创建链表printf("创建成功\n");system("pause");break;case 2:                  //打印所有结点if(!head){printf("请先创建链表!\n");system("pause");}else{PrintAll(head);       system("pause");}break;case 3:                         //删除指定学号结点if(!head){printf("请先创建链表!\n");system("pause");}else{printf("输入要删除的学号:\n");scanf("%d" , &num);head = Delete(head , num);   if(head)printf("删除成功!");//\n删除的学号:%d\t姓名:%s\n" , p1->num , p1->name);elseprintf("没有此学号!\n");system("pause");}break;case 4:                       //插入结点if(!head){printf("请先创建链表!\n");system("pause");}else{newStu = (pStu)malloc(sizeof(Stu));printf("请输入插入结点学号和姓名:\n");scanf("%d%s" , &newStu->num , newStu->name);newStu->next = NULL;head = Insert(head ,  newStu);break;case 5:exit(1);break;default:printf("请输入有效选项!\n");break;}}}}


                                             
0 0
原创粉丝点击