刚入门数据结构,尝试一下自己写一个链表。
一下是思路
//InitChain.c
#include "InitChain.h"
Node *Linklist::createList_1()
{
// Head->next = nullptr
int length;
Node *prev;
prev = this->Head;
cout << "How long you want?";
cin >> length;
for (int i = 0; i < length; ++i)
{
Node *temp = new Node;
cout << "Create No." << i+1;
cin >> temp ->data;
temp ->next = prev->next;
prev ->next = temp;
}
return Head ->next;
}
Node *Linklist::createList_2()
{
int length;
Node *prev;
prev = this ->Head;
cout << "How many?" ;
cin >> length;
for (int i = 0; i < length; ++i) {
Node *temp = new Node;
cout << "No." << i + 1 ;
cin >> temp ->data;
prev ->next = temp;
prev = temp;
temp ->next = nullptr;
}
return this ->Head;
// return this ->Head ->next;
// 如果这样写会影响删除,输出的时候第一个结点就会不见
}
Node *Linklist::insert(Node *head, int num)
{
// Node *prev, *enterP;
// 不应该这样写,因为这里只是插入,enterP压根就不用位移
// 在要插入的地方在new出来就行了
Node *prev;
prev = head;
// enterP = head ->next;
// 插入某个逻辑位置
for (int i = 1; i < num; ++i)
{
prev = prev ->next;
// enterP = enterP ->next;
}
// Node *enterP = new Node;
//new是操作符,可以理解为不用new类型就不一样
//Node *enterP; //指针要分配空间以后才能赋值啊。所以必须加上 new Node;
//enterP ->data = 66;
Node *enterP;
enterP = new Node;
enterP ->data = 66;
enterP->next = prev->next;
prev ->next = enterP;
return head ->next;
}
Node *Linklist::erase(Node *head, int num)
{
Node *prev,*delP;
prev = head;
delP = head ->next;
// 按照数据删除
/*while(delP)
{
if(delP->data == num)
break;
delP = delP ->next;
prev = prev ->next;
}*/
// 按逻辑顺序删除
for (int i = 1; i < num ; ++i)
{
delP = delP ->next;
prev = prev ->next;
}
prev ->next = delP ->next;
delete delP;
return head->next;
}
int Linklist::ListLength(Node *head)
{
int i = 0;
while(head)
{
head = head ->next;
i += 1;
}
return i;
}
int Linklist::getList(Node *head, int num)
{
for (int i = 0; i < num - 1; ++i) {
head = head ->next;
}
return head ->data;
}
bool Linklist::findList(Node *head, int num)
{
while(head)
{
if(head ->data == num){
return true;
}else{
head = head ->next;
}
}
return false;
}
void Linklist::display(Node *head)
{
while(head)
{
cout << head->data <<endl;
head = head ->next;
}
}
//InitChian.h
#ifndef CHAINTABLE_INITCHAIN_H
#define CHAINTABLE_INITCHAIN_H
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
class Linklist{
private:
Node *Head = new Node;
public:
Linklist(){Head ->next = nullptr;}
Node *createList_1(); // 头插入法
Node *createList_2(); // 尾插入法
Node *insert(Node *head,int num); // 插入某链表某处
// num代表插入之后,顺数过来是第num个
Node *erase(Node *head,int num); // 删除某链表某处
int getList(Node *head,int num); //获取某链表某处元素
bool findList(Node *head,int num); //查找某链表某处
int ListLength(Node *head); // count某链表
void display(Node *head);
};
#endif //CHAINTABLE_INITCHAIN_H
主函数测试部分
#include "InitChain.h"
int main(void)
{
// Solution test;
// test.show_it(test.initChain(5));
Linklist test_1;
int rec_int;
int rec_it;
// test_1.display(test_1.createList_1());
Node *test_erase , *rec;
Node *test_insert;
// test_erase = test_1.createList_2();
test_insert = test_1.createList_2();
// rec = test_1.erase(test_erase , 2);
rec = test_1.insert(test_insert,3);
rec_int = test_1.ListLength(rec);
rec_it = test_1.getList(rec , 3);
if (test_1.findList(rec , 9))
{
cout << rec_int << endl;
}
cout << rec_it << endl;
test_1.display(rec);
return 0;
}
把CSDN当笔记保存一下,有空再回来补充详细的注释和思路。
写一个链表来测试一下