關於連結串列的總結(C++迴圈實現)
阿新 • • 發佈:2018-12-25
0.目錄
1.連結串列的基本操作
2.結點的基本操作
3.面試題
- 3.1 反轉連結串列
- 3.2 合併兩個單向排序連結串列
- 3.3 查詢兩個連結串列的第一個公共結點
1.連結串列的基本操作
1.1 結點定義
#include <iostream> using namespace std; struct Node { int value; Node* next; };
1.2 建立連結串列
Node* createLinkedList(int data[], int len) { Node* ret = NULL; Node* slider = NULL; for(int i=0; i<len; i++) { Node* n = new Node(); n->value = data[i]; n->next = NULL; if( slider == NULL ) { slider = n; ret = n; } else { slider->next = n; slider = n; } } return ret; }
1.3 銷燬連結串列
void destroyLinkedList(Node* list)
{
while( list )
{
Node* del = list;
list = list->next;
delete del;
}
}
1.4 列印連結串列
void printLinkedList(Node* list) { while( list ) { cout << list->value << "->"; list = list->next; } cout << "NULL" << endl; }
1.5 獲取連結串列長度
int getListLength(Node* list)
{
int ret = 0;
while( list )
{
ret++;
list = list->next;
}
return ret;
}
測試:
int main()
{
int a[] = {1, 5, 3, 2, 4};
Node* list1 = createLinkedList(a, 5);
printLinkedList(list1);
cout << getListLength(list1) << endl;
destroyLinkedList(list1);
cout << endl;
Node* list2 = createLinkedList(NULL, 0);
printLinkedList(list2);
cout << getListLength(list2) << endl;
destroyLinkedList(list2);
cout << endl;
int b[] = {6};
Node* list3 = createLinkedList(b, 1);
printLinkedList(list3);
cout << getListLength(list3) << endl;
destroyLinkedList(list3);
return 0;
}
執行結果為:
1->5->3->2->4->NULL
5
NULL
0
6->NULL
1
2.結點的基本操作
2.1 刪除結點
Node* deleteNode(Node* list, int value)
{
Node* head = list;
Node* slider = NULL;
while( head && (head->value == value) )
{
slider = head;
head = head->next;
slider = NULL;
}
Node* ret = head;
while( ret )
{
slider = ret->next;
if( slider && (slider->value == value) )
{
ret->next = slider->next;
slider = NULL;
}
else
{
ret = ret->next;
}
}
return head;
}
測試:
int main()
{
int a[] = {1, 2, 3, 2, 5};
Node* list1 = createLinkedList(a, 5);
printLinkedList(list1);
printLinkedList(deleteNode(list1, 2));
destroyLinkedList(list1);
cout << endl;
Node* list2 = createLinkedList(NULL, 0);
printLinkedList(list2);
printLinkedList(deleteNode(list2, 2));
destroyLinkedList(list2);
cout << endl;
int b[] = {2, 2, 2, 2, 2};
Node* list3 = createLinkedList(b, 5);
printLinkedList(list3);
printLinkedList(deleteNode(list3, 2));
destroyLinkedList(list3);
cout << endl;
int c[] = {1};
Node* list4 = createLinkedList(c, 1);
printLinkedList(list4);
printLinkedList(deleteNode(list4, 2));
destroyLinkedList(list4);
return 0;
}
執行結果為:
1->2->3->2->5->NULL
1->3->5->NULL
NULL
NULL
2->2->2->2->2->NULL
NULL
1->NULL
1->NULL
2.2 查詢結點
Node* findNode(Node* list, int value)
{
Node* ret = NULL;
Node* slider = list;
while( slider )
{
if( slider->value == value )
{
ret = slider;
break;
}
else
{
slider = slider->next;
}
}
return ret;
}
3.面試題
3.1 反轉連結串列
Node* reverseLinkedList(Node* list)
{
Node* ret = NULL;
Node* slider = list;
Node* next = NULL;
while( slider )
{
next = slider->next;
slider->next = ret;
ret = slider;
slider = next;
}
return ret;
}
測試:
int main()
{
int a[] = {1, 5, 3, 2, 4};
Node* list1 = createLinkedList(a, 5);
printLinkedList(list1);
printLinkedList(reverseLinkedList(list1));
destroyLinkedList(list1);
cout << endl;
Node* list2 = createLinkedList(NULL, 0);
printLinkedList(list2);
printLinkedList(reverseLinkedList(list2));
destroyLinkedList(list2);
cout << endl;
int b[] = {6};
Node* list3 = createLinkedList(b, 1);
printLinkedList(list3);
printLinkedList(reverseLinkedList(list3));
destroyLinkedList(list3);
return 0;
}
執行結果為:
1->5->3->2->4->NULL
4->2->3->5->1->NULL
NULL
NULL
6->NULL
6->NULL
3.2 合併兩個單向排序連結串列
Node* mergeLinkedList(Node* list1, Node* list2)
{
Node* ret = NULL;
if( list1 == NULL )
{
ret = list2;
}
else if( list2 == NULL )
{
ret = list1;
}
else
{
if( list1->value < list2->value )
{
ret = list1;
list1 = list1->next;
}
else
{
ret = list2;
list2 = list2->next;
}
Node* slider = ret;
while( list1 && list2 )
{
if( list1->value < list2->value )
{
slider->next = list1;
list1 = list1->next;
}
else
{
slider->next = list2;
list2 = list2->next;
}
slider = slider->next;
}
if( list1 == NULL )
{
slider->next = list2;
}
else if( list2 == NULL )
{
slider->next = list1;
}
}
return ret;
}
測試:
int main()
{
int a[] = {1, 2, 4, 6, 8};
Node* list1 = createLinkedList(a, 5);
printLinkedList(list1);
int b[] = {2, 2, 3, 3, 7};
Node* list2 = createLinkedList(b, 5);
printLinkedList(list2);
Node* list3 = mergeLinkedList(list1, list2);
printLinkedList(list3);
destroyLinkedList(list3);
return 0;
}
執行結果為:
1->2->4->6->8->NULL
2->2->3->3->7->NULL
1->2->2->2->3->3->4->6->7->8->NULL
3.3 查詢兩個連結串列的第一個公共結點
Node* findFirstCommonNode(Node* list1, Node* list2)
{
int len1 = getListLength(list1);
int len2 = getListLength(list2);
Node* ret = NULL;
if( len1 > len2 )
{
for(int i=0; i<(len1-len2); i++)
{
list1 = list1->next;
}
}
else
{
for(int i=0; i<(len2-len1); i++)
{
list2 = list2->next;
}
}
while( list1 )
{
if( list1 == list2 )
{
ret = list1;
break;
}
else
{
list1 = list1->next;
list2 = list2->next;
}
}
return ret;
}
測試:
int main()
{
int a[] = {1, 2, 3};
Node* list1 = createLinkedList(a, 3);
int b[] = {4, 5};
Node* list2 = createLinkedList(b, 2);
int c[] = {6, 7};
Node* list3 = createLinkedList(c, 2);
Node* ret = NULL;
ret = list1;
while( ret->next )
{
ret = ret->next;
}
ret->next = list3;
ret = list2;
while( ret->next )
{
ret = ret->next;
}
ret->next = list3;
printLinkedList(list1);
printLinkedList(list2);
ret = findFirstCommonNode(list1, list2);
printLinkedList(ret);
return 0;
}
執行結果為:
1->2->3->6->7->NULL
4->5->6->7->NULL
6->7->NULL