数据结构————链表的C++实现

博主在数据结构课上学习欠佳,为掌握课程,整理记录自己实现链表功能的代码。此次实现了链表的构建、遍历、元素添加、删除和数据查找等功能,因刚接触面向对象大规模代码,代码可能有 bug,且部分代码丢失,后续会补充完善。

写在前面:

这是一个五月三十日的一个普通下午,学习状态欠佳。。。
数据结构课上发现自己就是个菜鸡,毛都听不懂。最后只能自己过过手,看能不能掌握这门高深的课程。在此祈祷:期末放过我吧。。
整理记录一下自己的代码,以便自己日后复习。
此次链表主要简单的实现了几个功能,没有进行实践解题,因为刚刚接触面向对象的大规模代码,明显的觉得有些力不从心。可能代码会有很多bug,留作纪念,日后修改,完善。如果有关与链表的题目,我会之后补在后面。

此次链表的主要功能:

  1. 链表的构建
  2. 链表的遍历
    3. 链表的元素添加
  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时间太久了,找不到当时完整的代码了,少了两个功能。总之先记录下来吧,之后有时间补上吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值