刚开博客,初来乍到,请多多指教!(纯客套)
其实开这个博客的原因一方面是因为蛮喜欢来这个网站上找东西看的,一方面是因为自己比较懒,平常有一些笔记都没有做,想通过写博文的形式把一些自己认为对我比较
重要的东西记下来,忘了的时候可以上来看一下,我刚大一,之前从没接触过编程,顶级菜鸟一个,所以有错误希望大家多多提出哈!
上个星期我的一个大四的师兄给我发了大一到大四的所有课程的教程,作业,习题,PPt,及实验报告,What a AMAZING present!正在学数据结构的我立马把数据结构的实验报告打开,争取要在这几周内把他们的实验报告写完哈!马上动手!下面是第一个:
Problem Description
实现一个链表的插入,删除,查找,倒置4个操作。给出初始状态的单向链表,在完成每个给定的操作后,遍历打印出链表当前所有元素。插入操作Insert key(插入到链表头部),删除操作Delete key(空链表删除返回”Error”,删除不存在元素返回”Error”,删除所有满足的节点),查找操作Search key(成功返回”Sucess”, 否则返回”Fail”),Reverse。
Input
First line:N, the number of item in the list in begin;
Second line: N items(int type)
Third line: T, the number of action;
The next T line: one action per line;
Output
After each action, ouput all the list from head to tail;
Sample Input
4
1 2 3 4
Insert 5
Delete 3
Delete 9
Search 1
Search 8
Reverse
Sample Output
5 1 2 3 4 (注意后面有一个空格)
5 1 2 4
Error
Success
Fail
5 4 2 1
第一次做我有点蒙了,平常的链表写的还可以,一写模板我就傻眼了,搞来搞去不是定义错就是指针的问题。。。。。。可能是Java用多了,习惯了没有指针的日子。
经过一晚上(不要笑我!)的折腾,好歹把它弄出来了!下面贴码码了!
#include<iostream>
#include<string>
using namespace std;
template<typename T> //这是一个构造体模板
struct Node
{
Node<T> *Rear_Node; //只有这个指针是用到的
Node<T> *Front_Node; //没注意看题目要求的是单向链表
T Number; //所以定义了两个指向节点的指针
};
template<typename T>
class List_My
{
public:
List_My(int Number);
void insert(T Num); //声明各种操作
void Delete(T Num);
void Search(T Num);
void Reverse();
protected :
Node<T> *Front ;
Node<T> *Rear;
};
//Constructing function
template<typename T>
List_My<T>::List_My(int Number)
{
Node<int> * Itr = NULL;
Front = new Node<T>;
cin>>Front->Number;
Itr = Front;
for(long i=0;i<Number-1;i++)
{
Itr->Rear_Node = new Node<T>;
cin>>(Itr->Rear_Node)->Number;
Itr = (*Itr).Rear_Node;
Itr->Rear_Node = NULL;
}
}
//Insert operation
template<typename T>
void List_My<T>::insert(T Num)
{
Node<T> *Itr;
Itr = new Node<T>;
Itr->Number = Num;
Itr->Rear_Node = Front;
Front = Itr;
while(Itr!=NULL) //You have to use the itraration here
{ //Because you may change the value of Front by accident
cout<<Itr->Number<<" ";
Itr = Itr->Rear_Node;
}
cout<<endl;
cout<<"Ok!"<<endl;
}
//Delete the number you want to delete in the list
template<typename T>
void List_My<T>::Delete(T Num)
{
bool Judgement = false;
while( Front->Number == Num && Front!= NULL )
{
Front = Front->Rear_Node;
Judgement = true;
}
Node<T> *Itr_R = Front;
while( Itr_R->Rear_Node!=NULL && Itr_R!=NULL)
{
if((Itr_R->Rear_Node)->Number == Num)
Itr_R->Rear_Node = (Itr_R->Rear_Node)->Rear_Node;
else
Itr_R = Itr_R->Rear_Node;
}
if(!Judgement)
cout<<"Error"<<endl;
};
template<typename T>
void List_My<T>::Search(T Num)
{
Node<T> *Itr = Front;
bool Judgement = false;
while(Itr != NULL)
{
if(Itr->Number == Num)
{
Judgement = true;
break;
}
Itr = Itr->Rear_Node;
}
if(Judgement)
cout<< "Success"<<endl;
else cout<< "Fail"<<endl;
};
template<typename T>
void List_My<T>::Reverse()
{
T Array[100];
int k=-1;
Node<T> *Itr = Front;
while( Itr!=NULL )
{
k++;
Array[k] = Itr->Number;
Itr = Itr->Rear_Node;
}
Itr = Front;
for(int i=k;i>=0;i--)
{
Itr->Number = Array[i];
Itr = Itr->Rear_Node;
}
Itr = Front;
while(Itr!=NULL) //You have to use the itraration here
{ //Because you may change the value of Front by accident
cout<<Itr->Number<<" ";
Itr = Itr->Rear_Node;
}
}
int main()
{
int Number;
cout<<"Please input the number of the input data: "<<endl;
cin>>Number;
List_My<int> List_Ok(Number);
cin>>Number;
const int Temp_num = Number;
int *Num = new int[Temp_num];
char *Function = new char[Temp_num];
for(int i=0;i<Temp_num;i++)
{
string Temp;
cin>>Temp;
if(Temp=="Delete") Function[i]='D';
else if(Temp == "Insert") Function[i] = 'I';
else if(Temp == "Search") Function[i] = 'S';
else if(Temp == "Reverse") Function[i] = 'R';
else abort();
if(Function[i]!='R')
cin>>Num[i];
}
for(int i=0;i<Temp_num;i++)
{
switch(Function[i]) //switch里无法使用string类
{ // 只好转化为字符
case'D': List_Ok.Delete(Num[i]);break;
case'I': List_Ok.insert(Num[i]);break;
case'S': List_Ok.Search(Num[i]);break;
case'R': List_Ok.Reverse();break;
default:abort();break;
}
}
return 0;
}
虽然很题目很简单,但是也暴露了我太多的问题,对c++的理解不够深入是一个很大的问题,不过不管开头如何,只要坚持下去,我也可以厉害起来的!加油!