(本人新人,各位大牛看到这么基础的东西我都弄错千万莫怪。这是我们OPP实习课上的题目。我写出来也是纪念一下,同时保留一下当时的思路以及错误。不喜欢者绕道行之。谢!)
描述: | 建立一个链表,每个节点包括学生的学号、姓名、性别、年龄。先输入5个学生的数据,再输入一个年龄,如果链表中有年龄等于此年龄的记录,则删除所有年龄等于此年龄的记录,否则在链表的最后增加一个新节点,学号为180姓名为"aaa",性别为"male"。。 |
输入: | 创建链表时输入5个职工的职工号和工资,学号为大于100且小于200的整数,姓名为长度小于20的字符串,性别为长度小于10的字符串,年龄为大于等于0且小于200的整数。 |
输出: | 按顺序输出链表中的所有数据,每个数据占一行。 |
输入样例: | 101 zhangsan male 30 103 lisi female 18 105 wangwu male 25 107 maliu male 28 109 niuqi female 22 28 |
输出样例: | 101 zhangsan male 30 103 lisi female 18 105 wangwu male 25 109 niuqi female 22 |
提示: |
要求用动态内存分配实现,注意new和delete的使用。 |
刚开始的时候,写起来很顺手。基本上写代码就这样,只有在最后调试编译的时候,仿佛突然一下子全世界上所有的error 都出现在刚写完的代码里了。这个时候就感觉2012提前来到一样。不过即使提前来到,程序猿也会大喝一声:“等我调完这个代码再死!”汗啊~~~
在这将我开始的code 粘一下
#include <iostream>
#include <string>
using namespace std; struct Student //Student class { int _num; string _name; string _gender; int _age; Student *next; }; /*Student StuCreate(void) //创建了一个头节点,作为指针初始位置 { Student *head = new Student; head->next = NULL; return *head; }*/
Student *StuCreate(void) //返回指针时,应该这么创建
{
Student *head = new Student;
head->next = NULL;
return head;
}
Student StuInput(Student *head) //插入五个元素
{
/*{int i;
/*Student *pStu2 = new Student;
if(head->next == NULL)
{*/
Student *pTail = new Student;
head->next = pTail;
pTail->next = NULL;
//Student *pStu1 = new Student;
//cin>>pStu1->_num>>pStu1->_name>>pStu1->_gender>>pStu1->_age;
//head->next = pStu1;
//pStu1->next = NULL;
for(i = 0; i < 5; i++) // 在这里插入的时候,没有区分 head 後有没没有元素。代码改了好几遍,这还不是最初的代码
{
Student *p = new Student;
p->next = NULL;
cin>>p->_num>>p->_name>>p->_gender>>p->_age;
pTail->next = p;
pTail = p;
//p = p->next;
}
}*/
//改为如下
int i;
//Student *pTail = new Student;
//head->next = pTail;
//pTail->next = NULL;
Student *p = new Student;
p->next = NULL;
for(i = 0; i < 5; i++)
{
if(head->next == NULL)
{
cin >> p->_num >> p->_name >> p->_gender >> p->_age;
head->next = p;
}
else
{
Student *q = new Student;
q->next = NULL;
cin >> q->_num >> q->_name >> q->_gender >> q->_age;
p->next = q;
p = q;
}
//pTail->next = p;
//pTail = p;
// Student *p = new Student;
// p->next = NULL;
}
return *head;
}
Student StuRefer(Student *head, int age_1)
{
Student *pRefer = new Student;
Student *p1 = new Student;
pRefer = head->next; // 指针可不能指错了
int k = 0; p1 = head->next; // 指针可不能指错了
/*while(pRefer->next != NULL)
{
if(age_1 == pRefer->_age)
{
//StuDelete(head,pRefer);
//cin >> pRefer->_num;
while(p1->next->_num != pRefer->_num && p1->next != NULL) // 在这里,也可以通过一个指针来实现
{
p1 = p1->next;
}
p1->next = pRefer->next;
delete pRefer;
k = 1;
pRefer = pRefer->next;
}
else pRefer = pRefer->next;
}*/
while(pRefer->next != NULL) { if(age_1 == pRefer->next->_age) {/* while(p1->next->_num != pRefer->_num && p1->next != NULL) { p1 = p1->next; } p1->next = pRefer->next; */ p1=pRefer->next; pRefer->next = pRefer->next->next; delete p1; k = 1; pRefer = pRefer->next; } else pRefer = pRefer->next; }
if(k == 0)
{
Student *StuAdd = new Student;
StuAdd->next = NULL;
StuAdd->_num = 180;
StuAdd->_name = "aaa";
StuAdd->_gender = "male";
StuAdd->_age = age_1;
while(pRefer->next != NULL)
{
pRefer = pRefer->next;
}
pRefer->next = StuAdd;
}
return *head;
}
void Display(Student *head)
{
Student *pCurr = new Student;
head->next = pCurr;
while(pCurr->next != NULL)
{
cout<<pCurr->_num<<endl<<pCurr->_name<<endl<<pCurr->_gender<<endl<<pCurr->_age<<endl;
}
}
int main()
{
Student *Head = new Student;
//Student *pRefer = new Student;
int age_1;
//Student pRefer;
StuCreate(); // 这样可不行,没把 head 传过来
Head = StuCreate(); // 改为这样
StuInput(Head);
//StuDelete(Head,pRefer); cin >> age_1; StuRefer(Head,age_1); Display(Head); //cout << "Hello world!" << endl; return 0; }
这刚开始啊,连 输入输出流 的 << >> 都是错误。我狂晕啊。这还让不让我在这行混下去了啊。后来加上了 #include <string> 这个库之后。问题解决了。
不过在建立链表的时候貌似也出问题了。继续改之。
好吧,到此总算改完了。这个是二十一世纪最伟大的工程之一了! ^_^ 不过每次写完代码,并且改好,总是很有成就感的。