百韵网 >>  正文

用C语言编写利用线性表实现学生信息表,查找和排序

来源:www.baiyundou.net   日期:较早时间
线性表 是链表还是数组???你没说清楚噢!!!

估计你要的是链表把 我倒是做过一个类似的 把数据换了就可以 但是C++的 不过没关系 你把语法简单的转换一下就可以了 自己去修改吧 写的比较乱 请见谅

#include<iostream.h>
class dlb //单链表声明
{//01
private:
struct lb
{//02
int no; //值
lb *next;//地址存放
};//02-
int size;//保存链表长度
lb *hand;
bool pdpx();//判断链表是否有序
public:
dlb();//构造函数,用于初始化
int fhcd();//返回链表长度
bool dqys();//遍历链表
bool czysdz(int i);//查找链表元素(按地址)
bool czysz(int i);//查找链表元素(按值)
bool crys(int pos,int i);//插入元素
bool scys(int pos,int i);//删除元素
bool px();//排序
~dlb();//析构函数,用于释放空间
};//01-

////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
//长度初始化
//int dlb::size =0;
///////////////////////////////////////////////////////////////
//构造函数,用于初始化
dlb::dlb()
{//01
size=0;
hand=new lb;
hand->next =hand;
}//01-
////////////////////////////////////////////////////////////////
//返回链表长度
int dlb::fhcd()
{//01
return size;
}//01-
///////////////////////////////////////////////////////////////
//遍历链表
bool dlb::dqys()
{//01
lb *p;
p=hand;
if (hand->next ==hand)
{//on
return false;
}//off
for (;p->next != hand;)
{//on
p=p->next ;
cout<<p->no<<" ";

}//0ff
cout<<"\n";
return true;
}//01-
////////////////////////////////////////////////////////////////
//查找链表元素(按地址)
bool dlb::czysdz(int i)
{//01
if (i<1)
{//no
return false;
}//off
int n;
lb *p=hand;
for (n=0;p->next!=hand;n++)
{//no
if (n==i)
{//on
cout<<"第"<<n<<"个元素得值为:"<<"\n";
cout<<p->no<<"\n";
return true;
}//off
p=p->next ;
}//off
return false;
}//01-
///////////////////////////////////////////////////////////////
//查找链表元素(按值)
bool dlb::czysz (int i)
{//no
int y;
lb *p=hand;
for (y=0;p->next !=hand;y++)
{//no
if (p->no ==i)
{//no
cout<<"你查找得值在第"<<y<<"位"<<"\n";
return true;
}//off
p= p->next;
}//no
return false;
}//off
////////////////////////////////////////////////////////////////
//插入元素
bool dlb::crys(int pos,int i)
{//01
//---------------------------------
if (pos<-1)
{//->1
return false;
}//end-1
//---------------------------------
//分配空间
lb *newjd=new lb;
newjd->no=i;
lb *p1,*p2;
p1=p2=hand;
//临时指针
lb *ls;
//---------------------------------
//插入到表尾
if (pos==-1)
{//->2
for (;p1->next != hand;)
{p1=p1->next; }
p1->next = newjd;
newjd->next = hand;
size+=1;
return true;
}//end-2
//------------------------------------
//有序插入
if (pos==0)
{//->3
if (pdpx()==false)
{//3.1
delete newjd;
return false;
}//3.1-
else
{//3.1
p1=hand;
for (;p1->next != hand;)
{//3.2
if (p1->next->no > newjd->no )
{//3.3
ls=p2->next ;
newjd->next =ls->next ;
ls->next =newjd ;

size+=1;
return true;
}//3.3-
p2=p1;
p1=p1->next;
}//3.2-
if (p1->next ==hand)
{//3.3
newjd->next=hand;
p1->next =newjd;
return true;
}//3.3-

}//3.1-
}//end-3
//-------------------------------------
//按位置插入
else
{//->4
p1=hand;
if (hand->next == hand && pos==1)
{//4.1
p1->next=newjd;
newjd->next=hand;
size+=1;

return true;
}//4.1-
else if (hand->next == hand)
{//4.1
delete newjd;
return false;
}//4.1-
else
{//4.1
int jishu;
for (jishu=0;p1->next != hand;jishu++)
{//4.1.1
if(jishu==pos-1)
{//4.1.1.1
newjd->next =p1->next;
p1->next =newjd;
size+=1;
return true;
}//4.1.1.1-
p1=p1->next ;
}//4.1.1-
if (p1->next ==hand && pos<=jishu+1)
{//4.2
p1->next=newjd;
newjd->next =hand;
size+=1;
return true;
}//4.2-
else
{//4.3
delete newjd;
return false;
}//4.3-
}//4.1-
}//end-4
return false;
}//01-
///////////////////////////////////////////////////////////////
//删除元素(-1删除表尾,0为按值删除,其他为按地址删除)
bool dlb::scys(int pos,int i)
{//->1
//------------------
lb *p1,*p2;
p1=hand->next ;
p2=hand;
lb *ls;
//-----------------
if (pos<-1 || pos>size || hand->next ==hand)
{return false;}
//-----------------
else
{//2
if (pos==-1)//删除表尾
{//2.1
for (;p1->next->next!=hand;)
{//2.1.1
p1=p1->next;
p2=p2->next ;
}//2.1.1
delete p1->next;
p1->next=hand;
size-=1;
return true;
}//2.1-
//------------------------
else if (pos==0)//0为按值删除
{//2.2
for (;p1!=hand;)
{//2.1.2
if(p1->no == i )
{//2.1.2.1
ls=p1;
break;
}//2.1.2.1-
p1=p1->next ;
p2=p2->next ;
}//2.1.2-
if (p1==hand)
{//2.1.3
return false;
}//2.1.3-
}//2.2-
//------------------------
else//其他为按地址删除
{//2.3
int jishu;
for (jishu=1;p1!=hand;jishu++)
{//2.3.1
if(jishu==i)
{//2.3.1.1
ls=p1;
break;
}//2.3.1.1-
p1=p1->next ;
p2=p2->next ;
}//2.3.1
if (p1==hand)
{//2.3.2
return false;
}//2.3.2-
}//2.3-
//------------------------
p2->next =ls->next ;
delete ls;
size-=1;
}//2-
return true;
}//end->1
///////////////////////////////////////////////////////////////
//排序 //换值
bool dlb::px()
{//01
//-----------
lb *p1,*p2;
int t;
//------------
p1=hand->next ;
p2=hand ;
if (hand->next ==hand)
{return false;}
for (;p1 !=hand;)
{//02
p2=p1;
for(;p2!=hand;)
{//2.1
if (p2 ->no < p1->no )
{//2.1.1
t=p2->no;
p2->no =p1->no ;
p1->no =t;
}//2.1.1-
p2=p2->next;
}//2.1-
p1=p1->next ;
}//02-
return true;
}//01-
//////////////////////////////////////////////////////////////
dlb::~dlb()
{//01
if (hand->next ==hand)
{delete hand;}
else
{//02
lb *p1,*p2;
p1=hand->next ;
for (;p1!=hand;)
{ p2=p1->next ;
delete p1;
p1=p2;
}
cout<<"空间已全部释放完毕!"<<"\n";
}//02-
}//01-
/////////////////////////////////////////////////////////////
//判断链表是否有序
bool dlb::pdpx ()
{//no
lb *p1=hand->next ;
lb *p2=hand;
if (hand->next ==hand)
{//3.15
return false;
}//3.15
for (;p1->next != hand;)
{//no
if (p2 ->no > p1->no)
{//no
return false;
}//off
p1=p1->next ;
p2=p2->next ;
}//off

return true;
}//off
//////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////

//链表排序 //按地址(载我另外一个程序,可以参考下
void jh::px(jhlb *ht)
{//01
jhlb *p1,*t1,*t2,*t,*ls;
p1=ht->next ;
//ls=new jhlb;
ls=ht;
if (ht->next!=ht)
{//02
ls->next =ls;
for (;p1!=ht;)
{//2.1

t=p1;
p1=p1->next;
t1=ls->next;
t2=ls;
for(;t1!=ls;)
{//2.1.1
if (t->zhi<t1->zhi)
{break;}
else
{//2.1.1.1
t2=t1;
t1=t1->next;
}//2.1.1.1-
}//2.1.1-
if (ls->next==ls)
{//2.1.1.2
ls->next=t;
t->next=ls;
}//2.1.1.2-
else
{//2.1.1.2
t2->next=t;
t->next=t1;
}//2.1.1.2-

}//2.1-
blys(ht);
}//02-
}//01-

//主程序 略

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
typedef int ElemType;

typedef struct LNode {
ElemType number;
ElemType result;
struct LNode *next;
}linklist,*link;

/*构造链表*//////////////////////////////////////
void IinitList(link &L)
{
if(L)delete L;
L= (link)malloc(sizeof(LNode)) ;
if (!L) exit(1);
L->next=NULL;
cout<<"数据已经建立\n";
}

/////////////////////////////////////////////////////
////*显示数据*///////// ////////////////////////////////
void show(link l)
{ link p; int j;
p=l;j=0;
cout<<"数据的值为:\n";
while(p->next)
{
cout<<p->next->number<<" "<<p->next->result<<endl;
p=p->next;
}
}
//////////////////////// /////////////////////////////////
//////销毁链表////// ////////////////////////////////////////
void destorylinst(link &L)
{
while(L)
{ link p=L;
L=L->next;

free(p) ;
}
L=NULL;
}

////////////////////////////////////////////// /////////
// /*插入结点*/////////////// ///////////////////////
int listinsert(link &L,int i,ElemType num,ElemType res)
{
link p,q;
int j;
p=L;j=0;
while(p&&j<i-1)
{
p=p->next;++j;
}
q= (link)malloc(sizeof(LNode));
q->number=num;
q->result=res;
q->next=p->next;
p->next=q; cout<<"数据已经插入\n"; cout<<"----------\n";
return 1;
}

////// 打印表头///////////////////////////////////////
void print()
{
cout<<"------------------------\n";
cout<<"------------------------\n";
}

void putline(link &l)
{
if(l==NULL ||l->next==NULL )
cout<<"数据为空,请先建立\n" ;
else{
link p,q;
p=l->next;
while(p!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->result<q->result)
{ ElemType t1,t2;
t1=p->number;
t2=p->result ;
p->number=q->number;
p->result=q->result ;
q->number=t1;
q->result=t2;
}
q=q->next;
}
p=p->next;

} cout<<"已经按成绩排序 \n";}}

void lookfor(link l,int e)
{
if(l==NULL)
cout<<"链表未建立,请先构造链表\n" ;
else{
link p; int i=0,j=0;
p=l->next;
cout<<"你查找值的位置是:\n " ;
while(p)
{ if(p->number==e)
{ j++;
cout<<i+1<<endl;
}
p=p->next; i++;
}cout<<"查找完毕\n";
if(j==0)
cout<<"你查找的值不在链表中 、\n";

} }

//////测试函数///// /////////////////////

void main()
{ link L=NULL; int k;
while(1)
{
cout<<"按0退出程序\n"<<"按1建立成绩记录\n"
<<"按2进行排序\n"<<"按3进行查找\n" ;
print();
int a,i,j;
cin>>a;
switch(a)
{
case 0: if(L!=NULL)
destorylinst(L) ;
exit(1);

case 1:
IinitList(L);
k=0;
show(L) ;
cout<<"数据为空\n";
cout<<"学生人数为: "<<k<<endl;
print();

cout<<"请输入学号和成绩,输入为0时结束\n";
int number ,result;
cout<<"输入学号:";
cin>>number;
cout<<"输入成绩";
cin>>result;
while(number!=0)
{ k++;
listinsert(L,k,number,result) ;
cout<<"输入学号:";
cin>>number;
cout<<"输入成绩";
cin>>result;}
print();
show(L) ; cout<<"学生人数: "<<k<<endl;
print();
break;

case 2:
putline(L);
if(L!=NULL)
show(L);
print();
break;

case 3:
print();
cout<<"输入要查找的学号;\n";
int z;
cin>>z;
lookfor(L,z);
print();
break;
default:
break ;}}delete L;}

C语言 顺序线性表 实现学生成绩管理系统~

楼主要问什么问题啊

你好!
你的要求不是很明确,有个类似的你看看吧

相关要点总结:

15598143929:c语言 线性表(急)
里妻答:完整的程序,用链表://--- include <stdio.h> include <stdlib.h> typedef struct node{ int id;double score;struct node *next;} node,list;void init(list **a,list **b) /*创建两个原链表*/ { double score[]={70,85,75,90,60,80,76,50};list *now,*ne;int i=0;b=*a=...

15598143929:线性表的基本操作c语言实现
里妻答://创建线性表 SeqList * SeqList_Create(int capacity); //销毁线性表 void SeqList_DesTroy(SeqList * list); void SeqList_Clear(SeqList* list); int SeqList_Length(SeqList* list); int SeqList_Capacity(SeqList* list); int SeqList_Insert(SeqList* list, SeqListNode* node, int pos); Seq...

15598143929:c语言!!!程序设计:建立一个学生信息链表,包括学号,姓名,成绩.(实现添...
里妻答:代码如下:/*用c语言链表编写一个学生信息系统程序,要求输出学生的学号,姓名,性别,学号,姓名,成绩(实现添加,删除,查询,排序,平均)*/ include <stdio.h> include <iostream> include <string.h> include <stdlib.h> using namespace std;const int n=5;/ nodeEntry : 节点数据类型 nodeADT...

15598143929:学生成绩记录薄 c语言 课程设计报告
里妻答:本程序为一个学生成绩管理系统,对学生的成绩进行管理,学生的信息包括学号,姓名,学期,三门课程的成绩,输入这些信息,本程序可以自动计算总成绩,可以按高分到低分进行排名,并对输入信息的人数进行汇总.2 数据结构设计: (1)结构体; (2)数组的设计:运用指针代替数组,使用指针来建立线性表,使程序更加简洁,可读性更强....

15598143929:C语言 线性表的实现
里妻答:printf("%c",L.data[i-1]);printf("\n");} void main(){ int i;ElemType e;SqList L;InitList(L);InsElem(L,'a',1);InsElem(L,'c',2);InsElem(L,'a',3);InsElem(L,'e',4);InsElem(L,'d',5);InsElem(L,'b',6);printf("xian xing biao:");DispList(L);printf...

15598143929:谁能给一个简单的线性表操作C语言完整程序?
里妻答:void InitList_Sq (SqList& l) { l.elem=new ElemType [LIST_INIT_SIZE];l.length=0;l.listsize=LIST_INIT_SIZE;}//初始化顺序表 然后SqList La;InitList_Sq(La);就可以 typedef struct Lnode{ int data;struct Lnode *next;}Lnode,*LinkList;//线性链表 //单链表可以有效的利用主存...

15598143929:C语言线性表急求大神解
里妻答:include<stdlib.h> typedef struct { int *data,len;}SL;void showlist(SL *l){ int i;for(i=0;i<(*l).len-1;i++){ printf("%d ",(*l).data[i]);} printf("%d\n",(*l).data[(*l).len-1]);} void createlist(SL *l){ int i,n;printf("请输入线性表的长度:");...

15598143929:一个数据结构线性表方面的题,请用C语言编出来,并能实现,望有志者帮...
里妻答:include <stdio.h> include <stdlib.h> define CF "%d\t"typedef int datatype;typedef struct NODE{ datatype data;struct NODE *next;} node;node *mer(node *la,node *lb)/*合并表*/ { node *t1,*t2,*lrc,*lnew=NULL;if (la->data<=lb->data){ lnew=la;la=la->next ;lb=...

15598143929:作业要求: 用c语言编写一个完整的程序,功能如下: 1,创建一个线性表,采...
里妻答://这里不能加p=p->next;因为 p=head;已经是相当是p==head了,不能用谁指向谁来理解 while(p!=NULL){ printf("%d",p->num);printf("\n");p=p->next;} } struct link *delet(struct link *head,int i)//有问题 { struct link *p,*q;int j=0;p=head;if(i==1){ p=p->...

15598143929:C语言中怎么定义个线性表
里妻答:1、定义结构体类型,这里需要利用指针和结构体,其中m和n分别表示矩阵的行和列。2、为矩阵申请储存空间,注意这里使用了malloc()函数。3、初始化矩阵,这里将矩阵初始化为m*n的数组,且矩阵中的每一个元素的值均为0。4、释放存储空间。5、一般在定义阶段就确定数组的大小,输入数字即为数组大小。6、...

(编辑:本站网友)
相关推荐
关于我们 | 客户服务 | 服务条款 | 联系我们 | 免责声明 | 网站地图
@ 百韵网