单向链表-1
来源:互联网 发布:怎样用手机淘宝购物 编辑:程序博客网 时间:2024/06/08 08:02
单向链表的排序、查找、插入、删除
/**************************************** 使用单向链表 ** 按学生成绩从高到低的顺序存储学生信息 ** 从中删除不及格学生的信息 ****************************************/#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;struct student /**构建链表的单节点数据元素**/{ char name[20]; float score; struct student *next;};typedef student NODE ;NODE *Search(NODE *head,int key) /**查找关键字小于key的节点的前驱**/{ NODE *p; p=head; while (p->next!=NULL) { if(p->next->score<key) return p; p=p->next; } return p;}void InsertNode(NODE *p,NODE *newp) /**在p之后插入节点newp**/{ newp->next=p->next; p->next=newp;}void DelNode(NODE *p) /**删除p结点的一个后继结点**/{ NODE *q; if(p->next!=NULL) { q=p->next; p->next=q->next; delete q; }}void DelList(NODE *head) /**销毁整个链表**/{ NODE *p; p=head; while(head->next!=NULL) { head=head->next; delete p; p=head; } delete head;}void DispList(NODE *head) /**显示链表各元素**/{ NODE *p; p=head; while (p->next!=NULL) { cout<<p->next->name<<"\t"<<p->next->score<<endl; p=p->next; }}int main (){ NODE *newp,*head,*p; char name[20]; float score,low=60; if((newp=new NODE)==NULL) { cout<<"no NODE fail!"<<endl; exit(0); } head=newp; head->next=NULL; cout<<"input name and score(-1 to exit):"<<endl; cin>>name>>score; while (score>0) { if((newp=new NODE)==NULL) { cout<<"no NODE fail!"<<endl; exit(0); } strcpy(newp->name,name); newp->score=score; newp->next=NULL; p=Search(head,score); InsertNode(p,newp); cin>>name>>score; } cout<<"Before delete:"<<endl; DispList(head); for (p=Search(head,low);p->next!=NULL;p=Search(head,low)) DelNode(p); cout<<"after delete:"<<endl; DispList(head); DelList(head); return 0;}
0 0
- 1 单向链表
- 单向链表-1
- 单向链表(1)
- 数据结构--单向链表1
- 单向链表基本操作1
- Linux C 单向链表1
- 单向链表
- 单向链表倒序
- 单向链表
- 单向链表逆转
- 逆转单向链表
- 单向链表反转
- 单向链表
- 单向链表(ZT)
- 反转单向链表
- 单向链表反转
- 单向链表
- 创建单向链表
- Cocoapods 集成部分命令
- Shell case esac语句
- Java数组4
- unity屏幕模糊
- JS提交中文encodeURI两次
- 单向链表-1
- Qt学习笔记:QDir
- AirGIS相关API
- Shell for循环
- 欢迎使用CSDN-markdown编辑器
- spring-boot中font-awesome在ie8中不显示的问题
- python直接获取API传递回来的参数
- 丹的Python学习笔记Day3
- rman 备份 DB到 asm 磁盘新目录(backup as cpoy)