写在前面:
这是一个五月三十日的一个普通下午,学习状态欠佳。。。
数据结构课上发现自己就是个菜鸡,毛都听不懂。最后只能自己过过手,看能不能掌握这门高深的课程。在此祈祷:期末放过我吧。。
整理记录一下自己的代码,以便自己日后复习。
此次链表主要简单的实现了几个功能,没有进行实践解题,因为刚刚接触面向对象的大规模代码,明显的觉得有些力不从心。可能代码会有很多bug,留作纪念,日后修改,完善。如果有关与链表的题目,我会之后补在后面。
此次链表的主要功能:
- 链表的构建
- 链表的遍历
3. 链表的元素添加 - 链表的元素删除
5. 链表的数据查找
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int sum=0;
struct Node
{
int data;
Node *next;
};
class list
{
protected:
Node A;
Node *head;
//Node *first,*end;
public:
//list();
~list();
Node* Create();
void Output();
int Length();
void Delete(const int aDate);
//void CreatList(Node *l,int i);
//void print();
};
void list::Output()
{
cout<<"\n==========输出刚才的数据=============\n"<<endl;
Node *p = head;
while(p != NULL)
{
cout<<p->data<<endl;
p=p->next;
}
}
Node* list::Create()
{
Node *p,*s;
head=NULL;
int x=0,cycle=1;
while (cin>>x)
{
s = new Node;
s->data = x;
if (NULL == head)
{
head = s;
}
else
{
p->next = s;
}
p = s;
}
p->next=NULL;
if(NULL!=head)
cout<<"创建成功!"<<endl;
else
cout<<"没有数据输入!"<<endl;
//cout<<&head;
return head;
}
int list::Length()
{
int cnt = 0;
Node *p = head;
while(p != NULL){
p=p->next;
++cnt;
}
return cnt;
}
void list::Delete(const int aDate)
{
Node *p = head,*s;
while (aDate!=p->data&&p->next!=NULL)
{
s=p;// 直到找出相等的结点跳出循环 s存储前一个结点,p存储当前结点
p=p->next;
}
if (aDate == p->data)
{
if (p == head)
{
head = p->next;
}
else
{
s->next = p->next;
}
delete p;
}
else
{
cout<<"没有找到这个数据!"<<endl;
}
}
int main()
{
list *T=new list;
Node *first;
//T->head=NULL;
first=T->Create();
T->Output();
cout<<T->Length();
//cout<<&T<<" "<<&first<<" ";
system("pause");
return 0;
}
emmmmm时间太久了,找不到当时完整的代码了,少了两个功能。总之先记录下来吧,之后有时间补上吧