求一道c++链表的例题(任意),并附上详解,,详解!!!新手求教。

作者&投稿:台卞 (若有异议请与网页底部的电邮联系)
一题C++链表问题,新手求教啊,悬赏悬赏100+,在线等急~

没有100+悬赏啊?

输入:学生总数n
输入:
名字1 分数1 分数2 分数3
名字2 分数1 分数2 分数3
名字3 分数1 分数2 分数3
。。。
名字n 分数1 分数2 分数3
------------------------
例如:
5
zhao 60.0 70.0 80.0
qian 50.0 65.0 90.0
sun 100.0 100.0 100.0
li 30.0 40.0 20.0
zhou 100.0 90.0 70.0


#include
#include

typedef struct
{
char name[32];
float mark[3];
} ST;


void main()
{
ST *all_st;
float f1,f2,f3;
int N,i;

printf("Enter the Number of students
N= ");
scanf("%d",&N);

all_st = (ST *) malloc( N * sizeof(ST));
if (!all_st) {
printf("No enough memory .
");
exit(0);
}

for (i=0;i<N;i++){
printf("First_LastName mark1 mark2 mark3:
");
scanf("%s %f %f %f",&all_st[i].name,&f1,&f2,&f3);
all_st[i].mark[0] = f1;
all_st[i].mark[1] = f2;
all_st[i].mark[2] = f3;
}

for (i=0;i<N;i++){
f1 = all_st[i].mark[0] + all_st[i].mark[1] + all_st[i].mark[2] ;
if (f1 >= 180.0) {
printf("%s -- pass exam
",all_st[i].name);
}
}

for (i=0;i<N;i++){
f1 = all_st[i].mark[0] + all_st[i].mark[1] + all_st[i].mark[2] ;
if (f1 < 180.0) {
printf("%s -- not pass exam
",all_st[i].name);
}
}

for (i=0;i<N;i++){
f1 = all_st[i].mark[0] + all_st[i].mark[1] + all_st[i].mark[2] ;
if (f1 >= 300.0) {
printf("%s -- 3 x 100.0
",all_st[i].name);
}
}
exit(0);
}

输出:
zhao -- pass exam
qian -- pass exam
sun -- pass exam
zhou -- pass exam
li -- not pass exam
sun -- 3 x 100.0

#ifndef CLINK_H_
#define CLINK_H_

typedef int elemType;//元素项

struct Item //节点项
{
elemType data;
Item *p_next;
};

class CLink
{
public:
CLink();//构造函数
CLink(const CLink & link); //拷贝构造函数
CLink & operator = (const CLink & otherLink); //赋值函数

void rearAdd(elemType data); //在末尾添加元素;
void headAdd(elemType data); //在头部添加元素;
void print() const; //遍历链表并打印;
void destroy();//销毁链表,将链表成为空链表;
int size() const; //得到单链表的长度;
bool isEmpty();// 判断链表是否为空;
elemType getElement(int pos) const; //获得链表中第pos个节点的元素;
int find(elemType x); //查找x第一次出现的位置,若没有,返回-1;
void modifyElement(int pos, elemType x); //修改pos位置的元素值为x;
void insertElement(int pos, elemType data);//将元素插入在链表的第pos位置上;
void deleteElement(int pos); //删除链表的第pos个节点;

void sort(); //对链表元素进行升序排序;

~CLink();
private:
Item *pHead; //头节点
int length; //链表长度
};

#endif

void CLink::rearAdd(elemType data)
{

if(!pHead) //若添加时为空链表
{
pHead = new Item;
pHead->data = data;
pHead->p_next = NULL;
}
else
{
Item *temp = pHead;
while(temp->p_next)
{
temp = temp->p_next;
}
temp->p_next = new Item;
temp = temp->p_next;

temp->data = data;
temp->p_next = NULL;
}
length++;
}
void CLink::headAdd(elemType data)
{
Item * addElement = new Item;
addElement->data = data;
addElement->p_next = NULL;

if(!pHead)//添加时为空链表
{
pHead = addElement;
}
else
{
addElement->p_next = pHead;
pHead = addElement;
}
}
void CLink::print() const
{
Item *temp = pHead;
int count = 0;
while(temp)
{
cout << temp->data << "\t";
count++;

if(count % 5 == 0)
{
cout << endl;
}

temp = temp->p_next;
}
}
int CLink::size() const
{
return length;
}
bool CLink::isEmpty()
{
return pHead == NULL;
}
elemType CLink::getElement(int pos) const
{
if(pos < 1 || pos > length)
{
throw "获得元素位置非法!";
}
int index = 0;
Item *temp = pHead;
while(temp && ++index < pos)
{
temp = temp->p_next;
}

return temp->data;
}
int CLink::find(elemType x)
{
Item *temp = pHead;
int index = 0;

while(temp && ++index <= length)
{
if(temp->data == x)
{
return index;
}
temp = temp->p_next;
}

return -1;
}
void CLink::modifyElement(int pos, elemType x)
{
if(pos < 1 || pos > length)
{
throw "修改节点位置pos非法!";
}

Item *temp = pHead;
int index = 0;
while(temp && ++index <= length)
{
if(index == pos)
{
temp->data = x;
return ;
}
temp = temp->p_next;
}
}
void CLink::insertElement(int pos, elemType data)
{
if(pos < 1 || pos > length + 1)
{
throw "插入pos位置不合法!";
}

Item * insertElement = new Item;
insertElement->data = data;
insertElement->p_next = NULL;

if(pos == 1)//pos在链表的头位
{
insertElement->p_next = pHead;
pHead = insertElement;
length++;
}
else
{
Item *temp = pHead;
int index = 0;
while(temp && ++index <= length)
{
if(index + 1 == pos)
{
insertElement->p_next = temp->p_next;
temp->p_next = insertElement;

length++;

return ;
}
temp = temp->p_next;
}
}
}
void CLink::deleteElement(int pos)
{
if(pos < 1 || pos > length)
{
throw "删除位置pos非法!";
}

Item * deleteElement;
if(pos == 1)
{
deleteElement = pHead;
pHead = pHead->p_next;
delete deleteElement;
length--;
}
else
{
Item *temp = pHead;
int index = 0;
while(temp && ++index < length)
{
if(index + 1 == pos)
{
deleteElement = temp->p_next;
temp->p_next = temp->p_next->p_next;
delete deleteElement;

length--;
return ;
}
temp = temp->p_next;
}
}
}
void CLink::sort()
{
bool sortflags;
elemType a, b;
for(int i = 1; i < length ; i++)
{
sortflags = true;
for(int j = 1; j <= length - i; j++)
{
if( ( a = getElement(j) ) > (b = getElement(j+1) ) )
{
modifyElement(j, b);
modifyElement(j+1, a);

sortflags = false;
}
}
if(sortflags)
return ;
}
}
void CLink::destroy()
{
Item *temp;
while(pHead)
{
temp = pHead;
pHead = pHead->p_next;
delete temp;
}
length = 0;
}
CLink::CLink(const CLink & otherLink):pHead(NULL),length(0)
{

for(int i = 1; i <= otherLink.length; i++)
{
rearAdd(otherLink.getElement(i));
}
}
CLink & CLink::operator =(const CLink & otherLink)
{
destroy();
for(int i = 1; i <= otherLink.length; i++)
{
rearAdd(otherLink.getElement(i));
}
return *this;
}
CLink::~CLink()
{
Item *temp;
while(pHead)
{
temp = pHead;
pHead = pHead->p_next;
delete temp;
}
}

C语言 链表 考题
答:要求好复杂,好像我们的作业。我给你一个create 建立链表的函数,拿去参考吧,注释全,User Friendly 函数返回 指向链表头结点的指针 / This function create an dynamic linklist input by user.Then comment the formal parameters and return value as the following:return *head the address of s...

再一道C语言的题,也是链表类的...
答:if(x<=L->v[i])L->v[j+1]=L->v[j]L->v[i]=x L->len++ 这个程序有个小的缺陷 当你输入个m 而且m>8时 你运行下就能看见了 所以要完善还需要加点东西 你自己做个吧 锻炼一下 void insertsort(sqlist *L,int x){ int i,j;int k=0;for(i=0;i<L->len;i++){ if(x<...

...编写C语言程序,内容是建立一个链表,还有链表的插入与删除。_百度...
答:include<iostream> using namespace std;typedef struct lnode { int data;lnode *next;}lnode,*linklist;int m;int listinsert(linklist &l,int i,int e)//在带头节点的单链表中第i个元素插入元素e { int j=0;linklist p,s;p=new lnode;p=l;s=new lnode;while(p&&jnext;++j;} ...

...个任意的整数,要求按照升序的规则将其存入单链表中,并输出_百度知 ...
答:include<stdio.h>#include<stdlib.h>#include<string.h>struct node{ int num; node* next;}*h,*p;node *creat(){ node*h,*p,*q,*q1; int x,i; h=(node*)malloc(sizeof(node)); h->next=NULL; for(i=0;i<10;i++) {scanf("%d",&x); q=(node*)malloc(size...

C语言循环链表的题,求指点
答:这个是以前写的,做成一个环,其N=10,M=3,我就是想改了,lz自己改吧 include<stdio.h> struct serial { int num;struct serial next;};void main(){ int i;struct serial peo[100],*p,*q;for(i=0;i<10;i++)peo[i].num=i+1;for(i=0;i<9;i++)peo[i].next=&peo[i+1];...

关于c语言链表的一个题目,没有学过链表,计算机二级要考,请详细解答_百 ...
答:double fun(STREC *h){ if(!h)return 0;STREC *p(NULL);p = h->_next;double dTotal(0),dCount(0);while(p){ lTotal += p->s;p = p->next;++dCount;} if(dCount!=0)return dTotal/dCount;else return 0;}

C语言试题链表部分
答:/*以前写的一个,看看吧...*/ /*单向链表的反向输出*/ include <stdio.h> include <malloc.h> define MAX_LEN 10 typedef struct _LINKNODE { int value;struct _LINKNODE *next;}LINKNODE;/*链表逆置的原理就是前插,把原链表中的元素一个一个的前插到新的链表中*/ LINKNODE* revertLink...

提一个关于实现C语言单链表的问题
答:include<malloc.h> define NULL 0 define LEN sizeof(struct student)struct student { long num;char name[20];char sex[5];long age;long score;struct student*next;};int n;struct student*creat(void){ struct student*head;struct student*p1,*p2;n=0;p1=p2=(struct student*)malloc(...

C语言链表创建问题
答:删除所有结点,释放内存}//===用尾插入法建立带头结点的单链表===LinkList CreatListR1(void){char ch[10];LinkList head=(LinkList)malloc(sizeof(ListNode)); //生成头结点ListNode *s,*pp,*r;r=head;r->next=NULL;printf("Input # to end "); //输入"#"代表输入结束printf("Please ...

c语言的链表问题
答:;} void output(node* head){node* p=head->next;if(!p)printf("error\n");else { while(p){ printf("%s\t%s\t%lf\n",p->name,p->address,p->score);p=p->next;}} printf("%c",10);} void main(){node* head;init(&head);creat(&head);output(head);} ...