双链表--学生成绩表
2017-10-11 21:39
148 查看
#include<iostream> using namespace std; struct Node { int data; Node *prior; Node *next; }; class DoubleList { private: Node *first; public: DoubleList(); DoubleList(int a[],int n); ~DoubleList(); void Insert(int i,int x); int Delete(int i); void PrintList(); }; DoubleList::DoubleList() { first=new Node; first->next=NULL; } DoubleList::DoubleList(int a[],int n) { Node *r,*s; first=new Node; r=first; for(int i=0;i<n;i++) { s=new Node; s->data=a[i]; s->next=NULL; r->next=s; s->prior=r; r=s; } r->next=NULL; } DoubleList::~DoubleList() { Node *q=NULL; while(first!=NULL) { q=first; first=first->next; delete q; } } void DoubleList::Insert(int i,int x) { Node *p=first,*s=NULL; int count=0; while(p!=NULL&&count<i-1) {p=p->next; count++;} if(p==NULL)throw"位置"; else{ s=new Node;s->data=x; s->prior=p; s->next=p->next; p->next->prior=s; p->next=s; } } int DoubleList::Delete(int i) { Node *p=first; int count=0; while(p!=NULL&&count<i) { p=p->next; count++; } if(p==NULL) throw"位置"; else{ (p->prior)->next=p->next; (p->next)->prior=p->prior; free(p); } return 0; } void DoubleList::PrintList() { Node *p=first->next; while(p!=NULL) { cout<<p->data<<" "; p=p->next; } cout<<endl; } int main() { int stu_score[5]={88,92,52,68,78}; DoubleList L(stu_score,5); cout<<"成绩表数据为:"<<endl; L.PrintList(); cout<<"在第三个位置插入成绩"<<endl; try { L.Insert(3,78); } catch(char *s) { cout<<s<<endl; } cout<<"插入后数成绩表为:"<<endl; L.PrintList(); cout<<"删除前成绩表为:"<<endl; L.PrintList(); cout<<"删除第一个成绩"<<endl; try { L.Delete(1); } catch(char *s) { cout<<s<<endl; } cout<<"删除后成绩表为:"<<endl; L.PrintList(); return 0; }
相关文章推荐
- C语言使用单链表的学生成绩管理系统
- 用C++写的学生成绩管理系统(比较简单的没有使用链表,只是用到了类和对象)
- 【C语言】学生成绩链表的录入
- 学生成绩(链表)
- 学生成绩管理系统链表实现
- 实验二 双链表实现学生成绩
- N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。 请编写函数fun,它的功能是:找出学生的最高分,由函数值返回。c语言编程【22题】
- 创建学生成绩的单链表
- 学生成绩管理系统 C语言链表版本
- 用单链表实现学生成绩管理
- 链表《5》使用链表实现学生成绩管理系统
- C语言 链表实现 学生成绩排名
- 链表-简易学生成绩管理
- c用链表实现学生成绩管理系统 MIS
- 链表-简易学生成绩管理
- 链表实现学生成绩管理系统
- 学生成绩学籍管理系统(链表)
- 单链表学生成绩管理系统
- 线性表实验2:学生成绩(单链表)
- 数据结构|创建学生成绩的单链表(实验2.2)