数据结构
定义
1 2 3 4 5 6 7 8
| #include<iostream> #include<cstdlib> #include<assert.h> #define ElemType int typedef struct node{ ElemType val ; struct node* next ; }LinkList ;
|
头插尾插创建链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| LinkList* create_List_Head(int n){ LinkList* head = nullptr ; for(int i=0; i<n; i++){ int v ; scanf("%d",&v);
LinkList* newNode = (LinkList*)malloc(sizeof(LinkList)) ; newNode->val = v ; newNode->next= head ; head = newNode ; } return head ; }
LinkList* create_List_Tail(int n){ LinkList* head ; LinkList* cur = head ; head->next = nullptr ; for(int i=0; i<n; i++){ int v; scanf("%d",&v) ; LinkList* newNode = (LinkList*)malloc(sizeof(LinkList)) ; newNode->val = v ; newNode->next = nullptr ; head->next = newNode ; head = head->next ; } return cur->next ; }
|
反转链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| LinkList* Reverse_LinkList(LinkList* head){ LinkList* prev = nullptr ; LinkList* cur ; cur = head ; while(cur){ LinkList* tmp = cur->next ; cur->next = prev ; prev = cur ; cur = tmp ; } return prev ; }
LinkList* Reverse_LinkList_Recursion(LinkList* head){ if(head==nullptr || head->next == nullptr){ return head ; } LinkList* cur = Reverse_LinkList_Recursion(head->next) ; head->next->next = head ; head->next = nullptr ; return cur ; }
|
👋关于链表反转的推荐讲解 :Leetcode
头删尾删
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| LinkList* Delete_Head(LinkList* head){ if(head == nullptr){ printf("head == nullptr !") ; exit(-1) ; } LinkList* cur = (LinkList*)malloc(sizeof(LinkList)) ; cur = head->next ; head->next = nullptr ; return cur ; }
void Delete_Tail(LinkList* head){ LinkList* cur = head ; while(cur->next->next) cur = cur->next ; cur->next = cur->next->next ; }
|
增加结点
1 2 3 4 5 6 7 8 9
| LinkList* ApplyNode(LinkList* head, int v){ LinkList* cur = head ; while (cur->next) cur = cur->next ; LinkList* newNode = (LinkList*)malloc(sizeof(LinkList)) ; cur->next = newNode ; newNode->val = v ; newNode->next = nullptr ; return newNode ; }
|
打印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| void Print(LinkList* head){ if(head == nullptr){ printf("head == nullptr !\n") ; exit(-1) ; } LinkList* cur = head ; while(cur){ printf("cur->val = %d ",cur->val) ; printf("cur->next_add = %0X ",cur->next) ; cur = cur->next ; printf("\n") ; } }
void Print_m_n(LinkList* head,int m, int n){ int cnt ; LinkList* m_node = head ; LinkList* n_node = head ; LinkList* cur ; for(cnt=1;cnt<m;cnt++) m_node = m_node->next ; for(cnt=1;cnt<n;cnt++) n_node = n_node->next ; for(cur=m_node; cur!=n_node->next; cur = cur->next){ printf("cur->val = %d \n",cur->val) ; } }
|
销毁链表
1 2 3 4 5 6 7 8
| void Destroy_List(LinkList** head){ while(*head) { LinkList* next = (*head) -> next ; free(head) ; *head = next ; } }
|
在函数中,如果需要修改指针变量本身的值,那么就需要传递指向指针变量的指针,即二级指针。
测试程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include"linklist.h" using namespace std ;
int main() { LinkList h ; h.val = 2 ; h.next = nullptr ;
LinkList* phead = create_List_Tail(3) ; Print(phead) ; printf("------------------------\n"); Print_m_n(phead,1,3); return EXIT_SUCCESS ; }
|