数据结构 程序设计 用双向循环链表建立一个学生管理系统,要求实现插入,删除,排序,修改等功能。 c语言!!!程序设计:建立一个学生信息链表,包括学号,姓名,...

作者&投稿:赵昌 (若有异议请与网页底部的电邮联系)
用一下我的吧
是我大一时写的
有什么问题可以追问

#include<iostream.h>
#include<string.h>

typedef struct
{
char name[10];
long num;
float score;
}student;

void creat(student stu[]);
void insert(student stu[]);
void Delete(student stu[]);
void lookup(student stu[]);
void update(student stu[]);
void stat(student stu[]);
int length(student stu[]);
void print(student stu[]);

/*********************************
创建学生信息线性表
*********************************/
void creat(student stu[])
{
cout<<"请输入学生人数:";
int n;
cin>>n;
cout<<"姓名"<<'\t'<<"学号"<<'\t'<<"成绩"<<endl;
for(int i=1;i<=n;i++)
{
char newname[10];
long newnum;
float newscore;
cin>>newname;
strcpy(stu[i].name,newname);
cin>>newnum;
stu[i].num=newnum;
cin>>newscore;
stu[i].score=newscore;
}
strcpy(stu[i].name," ");
stu[i].num=0;
stu[i].score=0;
}
/*************************************
依次输出线性表中的学生成绩信息
*************************************/

void print(student stu[])
{
cout<<"姓名"<<'\t'<<"学号"<<'\t'<<"成绩"<<'\n';
int i=1;
while(i<=length(stu))
{
cout<<stu[i].name<<'\t'<<stu[i].num<<'\t'<<stu[i].score<<'\n';
i++;
}
cout<<'\n';
}
/**********************************
根据学号查询线性表中某个学生成绩信息
**********************************/
void lookup(student stu[])
{
cout<<"请输入你要查询的学号:";
long num;
cin>>num;
int i=1;
while(stu[i].num)
{
if(stu[i].num==num)
{
cout<<"姓名"<<'\t'<<"学号"<<'\t'<<"成绩"<<'\n';
cout<<stu[i].name<<'\t'<<stu[i].num<<'\t'<<stu[i].score<<'\n';
break;
}
i++;
}
if(!stu[i].num)
{
cout<<"你要查询的学号不存在,请重新输入!"<<'\n';
lookup(stu);
}
cout<<'\n';
}
/********************************
在线性表的某个位置上插入学生信息
********************************/
void insert(student stu[])
{
if(length(stu)==100)
{
cout<<"存储空间已满,不能进行插入操作!"<<'\n';
}
else
{
cout<<"请输入要插入的位置:";
int m;
cin>>m;
int n=length(stu);
if(m>n+1)
{
cout<<"插入位置不正确,请重新插入!"<<'\n';
insert(stu);
}
else
{
for(int i=n+1;i>=m;i--)
{
strcpy(stu[i+1].name,stu[i].name);
stu[i+1].num=stu[i].num;
stu[i+1].score=stu[i].score;
}
cout<<"请依次输入姓名,学号,成绩"<<'\n';
char newname[10];
long newnum;
float newscore;
cin>>newname;
strcpy(stu[m].name,newname);
cin>>newnum;
stu[m].num=newnum;
cin>>newscore;
stu[m].score=newscore;
}
}
}
/*******************************************
查询线性表中有效数据的长度
*******************************************/
int length(student stu[])
{
int i=1;
while(stu[i].num)
{
i++;
}
return (i-1);
}
/*****************************************
根据学号删除线性表中对应的学生信息
*****************************************/
void Delete(student stu[])
{
cout<<"请输入你要删除的学号:";
long num;
cin>>num;
int i=1;
while(stu[i].num)
{
if(stu[i].num==num)
{
int n=length(stu);
for(int j=i;j<n;j++)
{
strcpy(stu[i].name,stu[i+1].name);
stu[i].num=stu[i+1].num;
stu[i].score=stu[i+1].score;
}
strcpy(stu[j].name," ");
stu[j].num=0;
stu[j].score=0;
break;
}
i++;
}
if(!stu[i].num)
{
cout<<"你要删除的学号不存在,请重新输入!"<<'\n';
Delete(stu);
}
}

/*****************************************
统计各成绩段的学生信息
*****************************************/
void stat(student stu[])
{
cout<<"不及格的学生是:"<<'\n';
cout<<"姓名"<<'\t'<<"学号"<<'\t'<<"成绩"<<'\n';
int i=1;
while(stu[i].num)
{
if(stu[i].score<60)
{
cout<<stu[i].name<<'\t'<<stu[i].num<<'\t'<<stu[i].score<<'\n';
}
i++;
}
cout<<'\n';

cout<<"成绩为‘良’的学生是:"<<'\n';
cout<<"姓名"<<'\t'<<"学号"<<'\t'<<"成绩"<<'\n';
i=1;
while(stu[i].num)
{
if(stu[i].score>=60 && stu[i].score<75)
{
cout<<stu[i].name<<'\t'<<stu[i].num<<'\t'<<stu[i].score<<'\n';
}
i++;
}
cout<<'\n';

cout<<"成绩为‘中’的学生是:"<<'\n';
cout<<"姓名"<<'\t'<<"学号"<<'\t'<<"成绩"<<'\n';
i=1;
while(stu[i].num)
{
if(stu[i].score>=75 && stu[i].score<90)
{
cout<<stu[i].name<<'\t'<<stu[i].num<<'\t'<<stu[i].score<<'\n';
}
i++;
}
cout<<'\n';

cout<<"成绩为‘优’的学生是:"<<'\n';
cout<<"姓名"<<'\t'<<"学号"<<'\t'<<"成绩"<<'\n';
i=1;
while(stu[i].num)
{
if(stu[i].score>=90)
{
cout<<stu[i].name<<'\t'<<stu[i].num<<'\t'<<stu[i].score<<'\n';
}
i++;
}
cout<<'\n';
}
/********************************************
修改线性表中某个位置的学生信息
********************************************/
void update(student stu[])
{
cout<<"请输入要修改的位置:";
int m;
cin>>m;
if(m>length(stu))
{
cout<<"你要修改的位置不存在,请重新输入!"<<'\n';
update(stu);
}
else
{
cout<<"请依次输入更改后的姓名,学号,成绩"<<'\n';
char newname[10];
long newnum;
float newscore;
cin>>newname;
strcpy(stu[m].name,newname);
cin>>newnum;
stu[m].num=newnum;
cin>>newscore;
stu[m].score=newscore;
}
}
/******************************************************
主函数,调用类student的成员函数以实现相应功能
******************************************************/
void main()
{
cout<<"首先建立学生管理系统!"<<'\n';
student stu[101];
creat(stu);
print(stu);
cout<<"len:"<<length(stu)<<endl;
int j=100;
cout<<"请选择您要进行的操作(1为插入,2为删除,3为查找,4为修改,5为统计,0为取消操作)";
while(j)
{
cout<<"请您选择要进行的操作:";
cin>>j;
switch(j)
{
case 1:
{
insert(stu);
print(stu);
break;
}
case 2:
{
Delete(stu);
print(stu);
break;
}
case 3:
{
lookup(stu);
break;
}
case 4:
{
update(stu);
print(stu);
break;
}
case 5:
{
stat(stu);
break;
}
default:
break;
}
}
cout<<"线性表中共有"<<length(stu)<<"个学生";
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
int num;
char name[10];
float score[3];
float total;
struct student *next,*prior;
}DLNode,*DLinkList;

//建立链表
DLinkList Creat_DLinkList()
{
int x;
char y[10];
float s[3];
DLinkList DL=NULL;
DLNode *p=NULL,*q=NULL;
DL=malloc(sizeof(DLNode));
if(DL==NULL)
exit(0);
q=DL;
DL->next=NULL;
DL->prior=NULL;
printf("\nPlease enter students' information:\n");
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
while(x!=0)
{
int i;
p=malloc(sizeof(DLNode));
p->num=x;
strcpy(p->name,y);
for(i=0;i<3;i++)
p->score[i]=s[i];
p->total=p->score[0]+p->score[1]+p->score[2];
q->next=p;
p->prior=q;
p->next=DL;
q=p;
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
}
DL->prior=q;
return q;
}

//查找
int search(DLinkList r,int n)
{
DLNode *p,*h;
h=r->next;
p=h->next;
while(p!=h)
{
if(p->num==n)
return 1;
p=p->next;
}
return 0;
}

//添加
DLinkList add(DLinkList r)
{
int x;
char y[10];
float s[3];
DLNode *h=r->next,*p=NULL;
p=malloc(sizeof(DLNode));
printf("\nPlease enter students' information:\n");
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
while(x!=0)
{
int i;
p=malloc(sizeof(DLNode));
p->num=x;
strcpy(p->name,y);
for(i=0;i<3;i++)
p->score[i]=s[i];
p->total=p->score[0]+p->score[1]+p->score[2];
p->prior=r;
r->next=p;
p->next=h;
h->prior=p;
r=p;
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
}
return r;
}

//删除
void delete(DLinkList r)
{
char na[10];
DLNode *h=r->next;
DLNode *p=h->next;
DLNode *q=NULL;
while(p!=h)
{
q=p->next;
while(q!=h)
{
if(!strcmp(q->name,p->name))
{
q->prior->next=q->next;
q->next->prior=q->prior;
free(q);
}
q=q->next;
}
p=p->next;
}
}

//修改
int modify(DLinkList r,int n)
{
float s[3];
int i;
DLNode *h=r->next;
DLNode *p=h->next;
while(p!=h)
{
if(p->num==n)
{
printf("\nPlease enter new score:\n");
scanf("%f%f%f",&s[0],&s[1],&s[2]);
for(i=0;i<3;i++)
p->score[i]=s[i];
p->total=p->score[0]+p->score[1]+p->score[2];
break;
}
p=p->next;
}
if(p==h)
return 0;
else
return 1;
}

//排序
DLinkList sort(DLinkList r)
{
DLNode *t=NULL,*s=NULL;
DLNode *h=r->next;
DLNode *p=h->next;
DLNode *q=NULL;
while(p!=h)
{
q=h->next->next;
while(q!=h)
{
t=q->prior;
if(t->total<q->total)
{
s=t->prior;
t->next=q->next;
q->next->prior=t;
t->prior=q;
q->next=t;
q->prior=s;
s->next=q;
q=t;
}
q=q->next;
}
p=p->next;
}

return h->prior;
}

//输出
void print_DLinkList(DLinkList r)
{
int i;
DLNode *p,*h;
h=r->next;
p=h->next;
while(p!=h)
{
printf("number:%3d\tname:%s\tscore:%5.2f\t%5.2f\t%5.2f\ttotal:%5.2f\n",p->num,p->name,p->score[0],p->score[1],p->score[2],p->total);
p=p->next;
}
}

//释放内存
void destory(DLinkList r)
{
DLNode *h,*p,*t=NULL;
h=r->next;
p=h->next;
while(p!=h)
{
t=p->next;
free(p);
p=t;
}
free(h);
}

int main()
{
DLinkList r;
int x,n,k;
r=Creat_DLinkList();
print_DLinkList(r);
printf("\nChoose what you want:\n");
printf("1:Search information:\n");
printf("2:Add information:\n");
printf("3:Delete same name:\n");
printf("4:Modify score:\n");
printf("5:Sort degrdation:\n");
scanf("%d",&x);
switch(x)
{
case 1: printf("\nPlease enter a number:");
scanf("%d",&n);
k=search(r,n);
if(k)
print_DLinkList(r);
else
printf("\nerror!!!\n");
break;
case 2: r=add(r);
print_DLinkList(r);
break;
case 3: delete(r);
print_DLinkList(r);
break;
case 4: printf("\nPlease enter a number:");
scanf("%d",&n);
k=modify(r,n);
if(k)
print_DLinkList(r);
else
printf("\nNot found!!!\n");
break;
case 5: r=sort(r);
print_DLinkList(r);
break;
default:printf("\nEnter error!!!\n");
}

destory(r);
return 0;
}

最近在学这里,所以就帮楼主编了一下,顺便巩固巩固所学。lz有什么不懂的尽管问,我会尽量回答的。

双向链表的创建C++中如何编写一个程序实现双向链表的建立,插入和删除~

//CreateList_L.cpp
//To create a LinkList
#include
#include
#include
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2

typedef struct DuLNode
{ int data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;
// 初始条件:L已存在。操作结果:返回L中数据元素个数
int ListLength(DuLinkList L)
{
int i=0;
DuLinkList p=L->next; // p指向第一个结点
while(p!=L) // p没到表头
{
i++;
p=p->next;
}
return i;
}

// 由双链循环线性表L的头结点出发,正序输出每个数据元素
void ListTraverse(DuLinkList L)
{
DuLinkList p=L->next;
while(p!=L)
{
coutdata<<"";
p=p->next;
}
cout<<endl;
}
// 由双链循环线性表L的头结点出发,逆序输出每个数据元素
void ListTraverseBack(DuLinkList L)
{
DuLinkList p=L->prior;
while(p!=L)
{
coutdata<<"";
p=p->prior;
}
cout<<endl;
}

//To Creatre a DuLinkList L with HeadNode
void CreateList_DuL(DuLinkList &L)
{
int n;
int i;
DuLNode *p;
L=(DuLinkList)malloc(sizeof(DuLNode));
L->next=L->prior=L;
cout<<"CreateList_L"<<endl<<"================"<<endl;
cout ";
cin>>n;

cout"<<endl;
for(i=n;i>0;--i)
{
p=(DuLinkList)malloc(sizeof(DuLNode));
cin>>p->data; //Reverse order inputing for Creating a LinkList
p->prior=L;
p->next=L->next;
L->next->prior=p;
L->next=p;
}
if(n)
{
cout<<endl;
cout<<"Success to Create a DuLinkList !"<<endl;
ListTraverse(L);
cout<<endl;
}
else cout<<"A NULL DuLinkList have been created !"<<endl;
}

//ListInsert_Dul()
int ListInsert_DuL(DuLinkList &L)
{
DuLNode *p=L,*s;
int j=0;
int i;
int e;
cout<<"======"<<"before insert:"<<"======"<<endl;
ListTraverse(L);
cout<<"input the location you want to insert:";
cin>>i;
while (iListLength(L)+1)
{
//i值不合法
cout<<"illeagle location,please input the correct location:";
cin>>i;
}

cout<<"input the number you want to insert:";
cin>>e;
while(j<i)
{ p=p->next;
++j;
}
if(!(s=(DuLinkList)malloc(sizeof(DuLNode))))
{
cout<<endl<<"Allocate space failure ! " ;
return (ERROR);
}
s->data=e;
s->prior=p->prior;
s->next=p;
if(i==1)
L->next=s;
p->prior->next=s;
p->prior=s;
cout<<"has insert:"<<e<<endl;
ListTraverse(L);
cout<<"======"<<"after insert:"<<"======"<<endl<<endl;
return (OK);
}

// 删除带头结点的双链循环线性表L的第i个元素,i的合法值为1≤i≤表长
int ListDelete(DuLinkList L)
{
DuLinkList p;
int j=1; // j为计数器
int e;
int i;

cout<<"input the location of the number you want to delete"<<endl;
cin>>i;
while(iListLength(L))
{
//i值不合法
cout<<"illeagle location,please input the correct location:";
cin>>i;
}

p=L->next; // p指向第一个结点
while(p!=L&&j<i) // 顺指针向后查找,直到p指向第i个元素或p指向头结点
{
p=p->next;
j++;
}
if(p==L||j>i) // 第i个元素不存在
{
cout<<"第i个元素不存在"<<endl;
return ERROR;
}
else
{
cout<<"======"<<"before delete:"<<"======"<<endl;
ListTraverse(L);
e=p->data; // 取第i个元素
if(!p) // p=NULL,即第i个元素不存在
return ERROR;
e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
cout<<"has delete:"<<e<<endl;
ListTraverse(L);
cout<<"======"<<"after delete:"<<"======"<<endl<<endl;
return OK;
}

}
void menu(int c)
{
cout<<"================================================="<<endl;
cout<<"1----建立"<<endl;
cout<<"2----插入"<<endl;
cout<<"3----删除"<<endl;
cout<<"4----逆置"<<endl;
//cout<<"5----菜单"<<endl;
cout<<"0----退出"<<endl;
cout<<"================================================="<<endl;
cout <<endl<<"input you choice number:";
}
void main()
{
DuLinkList L;
int c=1;

while(c!=0)
{
switch(c)
{
case 0:
break;
case 1:
CreateList_DuL(L);
break;
case 2:
ListInsert_DuL(L);
break;
case 3:
ListDelete(L);
break;
case 4:
ListTraverseBack(L);
break;
default:
cout<<"infeasible choice,input again:"<<endl;
menu(c);
cin>>c;
}
if(c!=0)
{
menu(c);
cin>>c;
}
}
}

代码如下:
/*用c语言链表编写一个学生信息系统程序,要求输出学生的学号,姓名,性别,学号,姓名,成绩(实现添加,删除,查询,排序,平均)*/
#include
#include
#include
#include
using namespace std;
const int n=5;
/*
* nodeEntry : 节点数据类型
* nodeADT : 节点结构
* linkADT : 链表结构
*/
typedef struct Student
{
int num;
char name[30];
char sex;
float score1;//语文
float score2;//数学
float score3;//英语
//struct Student *next;
}Student;
typedef struct linkCDT {
nodeADT head;
}*linkADT;
/*
* InitLink : 初始化链表
* CreateNode : 创建节点
* AppendLink : 添加数据
*/
nodeADT CreateNode(Student entry) {
nodeADT p=(nodeADT)malloc(sizeof*p);
p->entry=entry,p->next=0;
return p;
}
/*
SortLink : 排序链表
//按学号排序
void SortLinkID(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.num>=p->entry.num)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
//按英语成绩排序
void SortLinkEnglish(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.score3>=p->entry.score3)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的字典序进行排序
void SortLinkName(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.name[0]>=p->entry.name[0])
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的长度进行排序
void SortLinkNameLength(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (strlen(pHead->entry.name)>=strlen(p->entry.name))
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}

循环链表是与单链表一样
是一种链式的存储结构,所不同的是,循环链表的最后一个结点的指针是指向该循环链表的第一个结点或者表头结点,从而构成一个环形的链。
循环链表的运算与单链表的运算基本一致。所不同的有以下几点:
1、在建立一个循环链表时,必须使其最后一个结点的指针指向表头结点,而不是象单链表那样置为NULL。此种情况还使用于在最后一个结点后插入一个新的结点。
2、在判断是否到表尾时,是判断该结点链域的值是否是表头结点,当链域值等于表头指针时,说明已到表尾。而非象单链表那样判断链域值是否为NULL。
以上内容参考:百度百科-链表