数据结构与算法学习之c++实现链表

//c++建立单链表及其基本操作//
#include<iostream>
#include<Windows.h>
using namespace std;
class Node
{
public:
int Data; //节点所保存的数据类型
Node* next;//指向下一个节点的指针
};
typedef int DataTpye;
class List_user
{
public:
List_user();//构造函数
~List_user();//析构函数
void CreatList(int n);//创建一个n个节点的链表
void LocationInsert(int n, DataTpye num);//指定位置n后插入一个节点
void DeleteData(DataTpye num);//删除节点中的指定数
void DeleteLocation(int n);//删除指定位置处的节点
void Insert(DataTpye num);//链表尾部插入一个节点
void DeleteAll();//删除所有节点
int Length();//返回链表的大小
DataTpye LocationData(int n);//返回指定位置处的数据


private:
Node* head;//一个指向链表头节点的指针
};


List_user::List_user()
{
head = new Node;
head->Data = 0;
head->next = NULL;
}
List_user::~List_user()
{
   delete head;   
}
void List_user::CreatList(int n)
{
Node *Ptemp;//定义一个临时节点指针
Node *Pnew;//分配一个新节点的指针
Ptemp = head;
int temp;
for (int i = 0; i<n; i++){
Pnew = new Node;
cout << "请输入第" << i + 1 << "个数:";
cin >> temp;
Pnew->Data = temp;//将数据存入节点数据区
Pnew->next = NULL;//新节点的指针指向NULL
Ptemp->next = Pnew;//临时节点的next指针指向新节点
Ptemp = Pnew;//同步新节点指针到临时节点指针
}
}
void List_user::LocationInsert(int n, DataTpye num)
{
if (n<0){
cout << "您要插入的位置有误!" << endl;
return;
}
int max = Length();
if (n>0 && n<max)
{
Node* newnode = new Node();
newnode->Data = num;
Node* loc=new Node();
Node* h = head;
for (int i = 0; i<n; i++){
h->next = loc;//从头节点遍历将第n个位置赋给loc
h = loc;
}
newnode->next = loc->next;//将新节点的next指针指向n+1位置节点处
loc->next = newnode;//将n位置处的节点指向新节点
}
if (n >= max)
{
Node* newnode = new Node;
newnode->Data = num;
Node* h = head;
Node* loc=new Node();
for (int i = 0; i<n; i++)
{
h->next = loc;   
h = loc;
}
newnode->next = NULL;
loc->next = newnode;
}
}
void List_user::DeleteData(DataTpye num)
{
Node* h = head;
Node* temp=new Node;//定义一个临时节点指针,指向满足条件的节点
int len = Length();
for (int i = 0; i<len; i++)
{
h->next = temp;
if (temp->Data == num && i != len - 1)//满足条件并且不是最后一个节点
{
h->next = temp->next;//让该节点的前一个节点指向该节点的后一个节点
delete(temp);//释放该节点空间
}
if (temp->Data == num && i == len - 1)//满足条件并且是最后一个节点
{
h->next = NULL;//该节点前一个节点指针将指向NULL
delete(temp);
}
h = temp;
}


}

void List_user::DeleteLocation(int n)
{
int len = Length();
if (n <= 0 || n>len)
{
cout << "您要删除的节点位置不正确!" << endl;
return;
}
Node* temp=new Node;
Node* h = head;
if (n<len)//如果删除的不是最后一个节点
{
for (int i = 0; i<n; i++)
{
h=h->next;
h = temp;//得到链表中第n个位置处的节点指针
}
h->next = temp->next;
delete(temp);
}
if (n == len)
{
for (int i = 0; i<n; i++)
{
h->next = temp;
h = temp;//得到链表中第n个位置处的节点指针
}
h->next = NULL;
delete(temp);
}
}
void List_user::Insert(DataTpye num)
{
Node* h = head;
int len = Length();
for (int i = 0; i<len; i++)
{
h=h->next ;
}
Node* newnode = new Node();//为新节点分配空间
h->next = newnode;
newnode->Data = num;
}
void List_user::DeleteAll()
{
Node* h = head;
int len = Length();
for (int i = 0; i<len; i++)
{
h=h->next;
delete (h);
}
}
int List_user::Length()
{
Node* h = head->next;
int length = 1;
while (h->next != NULL)
{
h=h->next;
length++;
}
return length;
}
DataTpye List_user::LocationData(int n)
{
if (n<0 || n>Length())
{
        cout << "您要返回的位置不正确!" << endl;
exit(0);
}

Node* h = head;
for (int i = 0; i<n; i++)
{
h=h->next;
}
return h->Data;
}

//链表接口功能测试
int main()
{
List_user list;
list.CreatList(5);
int leng=list.Length();
cout << leng << endl;
//cout << "请输入你要返回的节点位置:";
//int n;
//cin >> n;
//cout << "节点的第"<<n<<"个数是:"<<list.LocationData(n) << endl;
int data;
cout << "输入你要插入的数:";
cin >> data;
list.Insert(data);
cout << "此时节点个数为:"<<list.Length() << endl;
system("pause");
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38323666/article/details/80516280