单链表常见操作
来源:互联网 发布: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
- 单链表常见操作
- 单链表常见操作
- 单链表常见操作
- 单链表常见操作
- 单链表常见操作
- 单链表常见操作
- C语言单链表常见操作
- 面试专题 单链表常见操作
- C语言单链表常见操作系列
- 数据结构之单链表常见操作 C
- 常见公历日期操作
- 常见的SREng操作
- 常见的数据库操作
- 字符串常见操作
- TableLayoutPanel 常见操作
- mysql常见操作
- mysql常见操作
- 常见电脑操作快捷键
- 设计模式--6大原则--开闭原则
- IOS写一个可以支持全屏的WebView
- Square Coins<母函数>
- Codeforces 455C —— Civilization(并查集,树上最长链)
- hdu-oj 1008 Elevator
- 单链表常见操作
- 栈
- 树-堆结构练习——合并果子之哈夫曼树
- LeetCode-Combinations
- lightoj 1022
- 继承ViewGroup重写onMeasure方法的详解
- Unity基础开发----用unity自带Animation组件制作动画
- C++11线程指南(5)--线程的移动语义实现
- URAL1008 - Image Encoding - 广度优先搜索