mm系列题四

bb_star_bb 2003-10-16 11:38:29
1. 测试Linkedlist类 输入一批数,用链表做插入排序输出
2. 给出2个排好序的链表,将他们合并成一个排好的链表
3. 设计一个学生成绩链表,可以插入,查找,删除,排序,打印
4. 循环链表或双向链表解决约瑟夫问题
5. 用数组实现双循环静态链表


给出源代码,可以编译通过的。

分数不够可以再加。
...全文
37 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bb_star_bb 2003-10-17
  • 打赏
  • 举报
回复
只要能做出一道,就给100分。
bb_star_bb 2003-10-17
  • 打赏
  • 举报
回复
都给你一个人,帮别人发这些贴,被骂死了。
verbal 2003-10-16
  • 打赏
  • 举报
回复
接着上面的:
void Delete(StuNode *pHead)
{
long id;
cout<<"Enter ID:";
cin>>id;

while (pHead->next!=NULL)
{
pHead = pHead->next;
if (pHead->ID==id)
{
if (pHead->next!=NULL)
{
pHead->prior->next = pHead->next;
pHead->next->prior = pHead->prior;

delete pHead;

break;
}
else
{
pHead->prior->next = NULL;

delete pHead;

break;
}
}
else
if (pHead->next==NULL)
{
cout<<"Data Not Found!"<<endl;

break;
}
}
}

void Change(StuNode *pHead)
{
long id;
cout<<"Enter ID:";
cin>>id;

while (pHead->next!=NULL)
{
pHead = pHead->next;
if (pHead->ID==id)
{
cout<<"Data Found!"<<endl;

cout<<"Enter New ID:('0' for No Change)";
long id;
cin>>id;

if (id==0)
;
else
pHead->ID = id;

cout<<"Enter New Name:('0' for No Change)";
char name[20];
cin>>name;

if (name[0]=='0')
;
else
strcpy(pHead->Name,name);

cout<<"Enter New Birthday:('0' for No Change)";
long birthday;
cin>>birthday;

if (birthday==0)
;
else
pHead->Birthday=birthday;

cout<<"Enter New Grade:('0' for No Change)";
float grade;
cin>>grade;

if (grade==0)
;
else
pHead->Grade=grade;

break;
}
else
if (pHead->next==NULL)
{
cout<<"Data Not Found!"<<endl;
break;
}
}
}

void SearchByID(StuNode *pHead)
{
long id;
cout<<"Enter ID:";
cin>>id;

while (pHead->next!=NULL)
{
pHead = pHead->next;
if (pHead->ID==id)
{
cout<<"Data Found!"<<endl;

cout<<"ID Name Birthday Grade"<<endl;
cout<<pHead->ID<<" "<<pHead->Name<<" "
<<pHead->Birthday<<" "<<pHead->Grade<<endl;

break;
}
else
if (pHead->next==NULL)
{
cout<<"Data Not Found!"<<endl;
break;
}
}
}

void SearchByBirthday(StuNode *pHead)
{
long birthday;
cout<<"Enter Birthday:";
cin>>birthday;

while (pHead->next!=NULL)
{
pHead = pHead->next;
if (pHead->Birthday==birthday)
{
cout<<"Data Found!"<<endl;

cout<<"ID Name Birthday Grade"<<endl;
cout<<pHead->ID<<" "<<pHead->Name<<" "
<<pHead->Birthday<<" "<<pHead->Grade<<endl;

break;
}
else
if (pHead->next==NULL)
{
cout<<"Data Not Found!"<<endl;
break;
}
}
}

void SearchByGrade(StuNode *pHead)
{
float grade;
cout<<"Enter Grade:";
cin>>grade;

while (pHead->next!=NULL)
{
pHead = pHead->next;
if (pHead->Grade==grade)
{
cout<<"Data Found!"<<endl;

cout<<"ID Name Birthday Grade"<<endl;
cout<<pHead->ID<<" "<<pHead->Name<<" "
<<pHead->Birthday<<" "<<pHead->Grade<<endl;

break;
}
else
if (pHead->next==NULL)
{
cout<<"Data Not Found!"<<endl;
break;
}
}
}

void SearchByName(StuNode *pHead)
{
char name[20];
cout<<"Enter Name:";
cin>>name;

while (pHead->next!=NULL)
{
pHead = pHead->next;

if (strcmp(pHead->Name,name)==0)
{
cout<<"Data Found!"<<endl;

cout<<"ID Name Birthday Grade"<<endl;
cout<<pHead->ID<<" "<<pHead->Name<<" "
<<pHead->Birthday<<" "<<pHead->Grade<<endl;

break;
}
else
if (pHead->next==NULL)
{
cout<<"Data Not Found!"<<endl;
break;
}
}
}

void Search(StuNode* pHead)
{
char choice[20];
int identify;

while (identify!=0)
{
cout<<endl<<"Search By(0-Return toMainmenu ; 1-Birthday ; 2-Grade ; 3-Name ; 4-ID)";
cin>>choice;

identify = Identify(choice);

switch(identify)
{
case 0: return;
case 1: SearchByBirthday(pHead);break;
case 2: SearchByGrade(pHead);break;
case 3: SearchByName(pHead);break;
case 4: SearchByID(pHead);break;
default: continue;
}
}
}


void main()
{
StuNode *pHead = Init();
char choice[20];
int identify;

while (identify!=0){
cout<<endl
<<"Operation Supported:"<<endl
<<"0-Exit"<<endl
<<"1-Delete Data"<<endl
<<"2-Search Data"<<endl
<<"3-Sort Data"<<endl
<<"4-List All data"<<endl
<<"5-Save Data to File"<<endl
<<"6-Load Data from File"<<endl
<<"7-Add New Data"<<endl
<<"Choose Your Operation:";
cin>>choice;

identify = Identify(choice);

switch(identify)
{
case 0: exit(0);
case 1: Delete(pHead); break;
case 2: Search(pHead); break;
case 3: pHead = QSort(pHead); break;
case 4: List(pHead); break;
case 5: Save(pHead); break;
case 6: pHead = Init(); break;
case 7: Insert(pHead); break;
default: continue;
}
}
}
verbal 2003-10-16
  • 打赏
  • 举报
回复
解决你的第三题!你看这分数是不是好象给我一人都不够啊?呵呵!



#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct StuNode
{
long ID;
char Name[20];
long Birthday;
float Grade;
StuNode *prior;
StuNode *next;
};

StuNode *Init()
{
ifstream fin("stu.txt");

StuNode *Head = new StuNode();
Head->ID = 0;
Head->Birthday = 0;
Head->Grade = 0;
Head->prior = NULL;
Head->next = NULL;

while (!fin.eof())
{
StuNode *p = new StuNode();

fin>>p->ID>>p->Name>>p->Birthday>>p->Grade;

if(Head->next==NULL)
{
p->next = NULL;
p->prior = Head;
Head->next = p;
}
else
{
p->next = Head->next;
Head->next->prior = p;
p->prior = Head;
Head->next = p;
}
}

fin.close();

Head->prior = NULL;

return Head;
}

void Save(StuNode *pHead)
{
ofstream fout("stu.txt");

while (pHead->next!=NULL)
{
pHead = pHead->next;
if(pHead->next!=NULL)
fout<<pHead->ID<<" "<<pHead->Name<<" "
<<pHead->Birthday<<" "<<pHead->Grade<<endl;
else
fout<<pHead->ID<<" "<<pHead->Name<<" "
<<pHead->Birthday<<" "<<pHead->Grade;
}

fout.close();
}

void Swap(StuNode *p,StuNode *q)
{
StuNode *r = new StuNode();

r->ID = p->ID;
strcpy(r->Name,p->Name);
r->Birthday = p->Birthday;
r->Grade = p->Grade;
r->prior = r->next=NULL;

p->ID = q->ID;
strcpy(p->Name,q->Name);
p->Birthday = q->Birthday;
p->Grade = q->Grade;

q->ID = r->ID;
strcpy(q->Name,r->Name);
q->Birthday = r->Birthday;
q->Grade = r->Grade;

delete r;
}

void List(StuNode *pHead)
{
cout<<"ID Name Birthday Grade"<<endl;

while (pHead->next!=NULL)
{
pHead = pHead->next;
cout<<pHead->ID<<" "<<pHead->Name<<" "
<<pHead->Birthday<<" "<<pHead->Grade<<endl;

}
}

int Identify(char c[])
{
int i = atoi(c);
if (i==0)
return 0;
else
return i;
}

int Length(StuNode *pHead){
int count = 0;

while (pHead->next!=NULL){
pHead = pHead->next;
count++;
}

return count;
}

StuNode * QSortByGrade(StuNode *pHead,StuNode *pEnd)
{
StuNode *p = pHead;
StuNode *q = pEnd;

do
{
while (p!=q && p->Grade<=q->Grade)
{
if (q!=pHead)
q = q->prior;
else
break;
}

if (p!=q)
{
Swap(p,q);

if (p!=pEnd)
p = p->next;
else
break;
}

while (p!=q && p->Grade<=q->Grade)
{
if (p!=pEnd)
p = p->next;
else
break;
}

if (p!=q)
{
Swap(p,q);

if (q!=pHead)
q = q->prior;
else
break;
}
}
while (p!=q && p!=pEnd && q!=pHead);

if (q!=pHead)
{
QSortByGrade(pHead,q->prior);
}

if (q!=pEnd)
{
QSortByGrade(q->next,pEnd);
}

return pHead;
}


StuNode * QSortByID(StuNode *pHead,StuNode *pEnd)
{
StuNode *p = pHead;
StuNode *q = pEnd;

do
{
while (p!=q && p->ID<=q->ID)
{
if (q!=pHead)
q = q->prior;
else
break;
}

if (p!=q)
{
Swap(p,q);

if (p!=pEnd)
p = p->next;
else
break;
}

while (p!=q && p->ID<=q->ID)
{
if (p!=pEnd)
p = p->next;
else
break;
}

if (p!=q)
{
Swap(p,q);

if (q!=pHead)
q = q->prior;
else
break;
}
}
while (p!=q && p!=pEnd && q!=pHead);

if(q!=pHead)
{
QSortByID(pHead,q->prior);
}
if(q!=pEnd)
{
QSortByID(q->next,pEnd);
}
return pHead;
}

StuNode * QSortByBirthday(StuNode *pHead,StuNode *pEnd)
{
StuNode *p = pHead;
StuNode *q = pEnd;

do
{
while (p!=q && p->Birthday<=q->Birthday)
{
if (q!=pHead)
q = q->prior;
else
break;
}

if (p!=q)
{
Swap(p,q);

if(p!=pEnd)
p = p->next;
else
break;
}

while (p!=q && p->Birthday<=q->Birthday)
{
if (p!=pEnd)
p = p->next;
else
break;
}

if (p!=q)
{
Swap(p,q);

if (q!=pHead)
q = q->prior;
else
break;
}
}
while (p!=q && p!=pEnd && q!=pHead);

if (q!=pHead)
{
QSortByBirthday(pHead,q->prior);
}

if (q!=pEnd)
{
QSortByBirthday(q->next,pEnd);
}

return pHead;
}

StuNode *QSort(StuNode *pHead)
{
StuNode *pEnd = pHead;
StuNode *pNewHead = new StuNode();
char choice[20];
int identify;

while (pEnd->next!=NULL)
pEnd = pEnd->next;

cout<<"sort By(0-Return to Mainmenu ; 1-Birthday ; 2-Grade ; 3-ID)";
cin>>choice;

identify = Identify(choice);

switch(identify)
{
case 0: return pHead;
case 1: pNewHead = QSortByBirthday(pHead,pEnd); break;
case 2: pNewHead = QSortByGrade(pHead,pEnd); break;
case 3: pNewHead = QSortByID(pHead,pEnd); break;
default: return pHead;
}

List(pNewHead);

return pNewHead;
}

StuNode * Insert(StuNode *pHead)
{
StuNode *NewStu = new StuNode();

cout<<"Enter ID:";
cin>>NewStu->ID;
cout<<"Enter Name:";
cin>>NewStu->Name;
cout<<"Enter Birthday:";
cin>>NewStu->Birthday;
cout<<"Enter Grade:";
cin>>NewStu->Grade;

StuNode *pCurrent = pHead;

while (pCurrent->next!=NULL)
pCurrent = pCurrent->next;

pCurrent = QSortByID(pHead,pCurrent);

pCurrent = pCurrent->next;

while (pCurrent->next!=NULL)
{
if (pCurrent->ID<NewStu->ID)
pCurrent = pCurrent->next;
else
{
NewStu->prior = pCurrent->prior;
NewStu->next = pCurrent;

pCurrent->prior->next = NewStu;
pCurrent->prior = NewStu;

return pHead;
}
}

if (pCurrent->ID>NewStu->ID)
{
NewStu->prior = pCurrent->prior;
NewStu->next = pCurrent;

pCurrent->prior->next = NewStu;
pCurrent->prior = NewStu;
}
else
{
pCurrent->next = NewStu;
NewStu->prior = pCurrent;
NewStu->next = NULL;
}

return pHead;
}

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧