链表 例程(C++)

搬运工:林子木 /*************************************** 链表管理例程 主要实现功能为: 0、新建链表 1、插入成员 2、查找成员 3、删除成员 4、数据浏览 ***************************************/ #include "StdAfx.h" #include <iostream> #include <cstring> using namespace std; /*类声明*/ typedef struct student //定义student类 { int num; char name[8]; struct student * next; }stud; //用stud 代替 student类 类型 同typedef int NUM一样原理 /* 函数声明 */ stud * create_list(); //创建链表 int insert_list(stud * head,char * name,int n); //插入成员 int del_list(stud * head, char *name); //删除成员 stud * find_list(stud * head, char * name); //查找成员 void brow_list(stud * head); //显示全部 /*主函数*/ void main() { stud * head; //定义stud类型的指针 int choice; char name[8]; head=NULL; //指针赋空值 cout<<"1.建立链表"<<endl; cout<<"2.插入新生"<<endl; cout<<"3.查找学生"<<endl; cout<<"4.删除学生"<<endl; cout<<"5.数据浏览"<<endl; cout<<"0.退出程序"<<endl; do { cout<<"请选择操作,输入0~5\n"; cin>>choice; //输入功能选择 if (choice>5||choice<0) { cout<<"输入错误\n"; continue; } switch (choice) { case 1: //功能1:新建链表 if (head==NULL) head=create_list(); break; case 2: if (head==NULL) //功能2: 插入成员 { cout<<"链表未建立\n"; break; } while (1) { cout<<"姓名(输入0时结束):"; cin>>name; if (strcmp(name,"0")==0) break; insert_list(head,name,-1); } break; case 3: //功能3: 查找成员 cout<<"输入姓名:"; cin>>name; find_list(head,name); break; case 4: //功能4: 删除成员 cout<<"输入姓名:"; cin>>name; del_list(head,name); break; case 5: //功能5: 显示全部成员 brow_list(head); break; default: return; } } while (1); } /*函数实现*/ stud *create_list() //新建链表 { stud * head; head=new stud; if (head!=NULL) cout<<"链表已建立\n"; else cout<<"没有足够存储空间\07\n"; head->next=NULL; head->num=0; return head; } int insert_list(stud * head,char * name,int n) //插入成员 { stud *p, *q, *s; s=new stud; if (s==NULL) { cout<<"没有足够空间!\07\n"; return 0; } q=head; p=head->next; while (p!=NULL&&n!=q->num) { q=p; p=p->next; } q->next=s; s->next=p; strcpy(s->name,name); s->num=q->num+1; return 1; } stud * find_list(stud * head, char * name) //查找成员 { stud * p; p=head; while(p!=NULL&&strcmp(p->name,name)) { p=p->next; } if (p!=NULL) { cout<<"学号:"<<p->num<<"姓名:"<<p->name<<endl; } else cout<<"查无此人!\n"; return p; } int del_list(stud * head, char *name) //删除成员 { stud *p, *q; q=head; p=head->next; while (p!=NULL&&strcmp(p->name,name)) { q=p; p=p->next; } if (p!=NULL) { q->next=p->next; delete p; cout<<"删除完成\n"; return 1; } else { cout<<"查无此人\07\n"; return 0; } } void brow_list(stud * head) //显示全部成员 { stud *P; P=head->next; while (P!=NULL) { cout<<"学号:"<<P->num<<" 姓名:"<<P->name<<endl; P=P->next; } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值