写在秋招之前~开始学数据结构 不知道能学多少,但是能学夺少算多少~奥里给!!!`
这个单链表是跟着这位博主的思路写的!超级清晰~有关一系列单链表的操作
emmmm 首先是C++的单链表啊~主要是内存空间的分配 太容易混淆了,用结构体做的,没有用类做那么清晰~但是理解就OK了哈哈哈哈
struct Node //节点
{
int data;
Node *next;
};
struct Linklist //指向节点的指针
{
Node* head;
};
void Insert(Linklist &L,int i,int elem)
{
Node *n=new Node();
n->next = NULL;
n->data = elem;//首先分配一个内存给新进来的元素~
if (L.head==NULL)
{
L.head = n;//插入的是第一个元素就认为是头节点
return;
}
if (i==0)
{
n->next = L.head->next;
L.head = n;
return;
}
Node *Currnode = L.head;
int count = 0;
while (Currnode->next != NULL && count<i-1)
{
Currnode = Currnode->next;
count++;
}
if (count ==i-1)
{
n->next = Currnode->next;
Currnode->next = n;
}
}
void PrintList(Linklist L)
{
if (L.head==NULL)
{
return;
}
Node* Currnode = L.head;
while (Currnode!= NULL)
{
cout << Currnode->data << " ";
Currnode = Currnode->next;
}
cout << endl;
}
void InitialList(Linklist *L)
{
Node *n = new Node();
n->next = NULL;
L->head = n;
}
int GetListLLength(Linklist L)
{
int count = 0;
while (L.head != NULL)
{
count++;
L.head = L.head->next;
}
return count;
}
void DeleteList(Linklist *L,int i)
{
Node *n = new Node();
Linklist L2;
InitialList(&L2);
L2 = *L;
if (L->head==NULL||i>GetListLLength(*L)-1)
{
return;
}
int count = 0;
while (count<i-1)
{
L2.head = L2.head->next;
count++;
}
n = L2.head->next;
L2.head->next = L2.head->next->next;
delete n;
}
void test03()
{
Linklist List;
InitialList(&List);
for (int i = 0; i < 10; i++)
{
Insert(List, i, i);
}
PrintList(List);
cout << "链表目前长度为:" << GetListLLength(List) << endl;
DeleteList(&List, 2);
PrintList(List);
cout << "链表目前长度为:" << GetListLLength(List) << endl;
int main()
{
cout << "正在测试我的代码。。" << endl;
srand((unsigned int)time(NULL));
test03();
system("pause");
}
接下来是循环链表~这个指针的指向顺序好容易弄混 😔主要就在于要把当前链表的头节点赋值给新的一个节点,然后慢慢移动,CurrNode太重要拉!
//循环链表
void InitialCircleList(Linklist *L)
{
Node *n = new Node();
n->next = n;
L->head = n;
}
int GetCircleLength(Linklist L)
{
int count = 1;
Node *current = new Node();
current = L.head;
while (current->next !=L.head)
{
count++;
current = current->next;
}
//delete current;
return count;
}
void InsertCircle(Linklist *L, int i, int elem)
{
Node *n = new Node();
n->data = elem;
n->next = NULL;
if (i<0 )
{
return;
}
int count = 0;
if (i==0)
{
n->next = n ;
L->head = n;
return;
}
Node *CurrNode = new Node();
CurrNode = NULL;
CurrNode = L->head;
while ((CurrNode->next != L->head) &&(count<i-1))
{
CurrNode = CurrNode->next;
count++;
}
if (count==i-1)
{
n->next= CurrNode->next;
CurrNode->next = n;
L;
}
//delete CurrNode;
}
void PrintCircle(Linklist L)
{
int index = 0;
if (GetCircleLength(L)==0)
{
return;
}
Node *CurrNode = new Node();
CurrNode = L.head;
while (index< GetCircleLength(L))
{
cout << CurrNode->data;
CurrNode = CurrNode->next;
index++;
}
cout << endl;
}
void DeleteCircleByIndex(Linklist *L, int i)
{
if (i<0||i>GetCircleLength(*L))
{
return;
}
Node *n = new Node();
Node *CurrNode = new Node();
CurrNode = L->head;
int count = 0;
while (CurrNode->next!= L->head && count<i-1)
{
CurrNode = CurrNode->next;
count++;
}
n=CurrNode->next;
CurrNode->next = CurrNode->next->next;
delete n;
}
void test05()
{
Linklist L;
InitialCircleList(&L);
for (int i=0;i<10;i++)
{
InsertCircle(&L, i, i);
}
cout << "现在的长度为:" << GetCircleLength(L) << endl;
cout << "循环链表为:" << endl;
PrintCircle(L);
DeleteCircleByIndex(&L,2);
cout << "现在的长度为:" << GetCircleLength(L) << endl;
PrintCircle(L);
}
int main()
{
cout << "正在测试我的代码。。" << endl;
srand((unsigned int)time(NULL));
test05();
system("pause");
}
还有个疑问就是 我不delete掉 会不会内存泄漏了呢💦