您好,登錄后才能下訂單哦!
本篇內容主要講解“c++帶頭節點的循環鏈表及兩個循環鏈表的合并方法是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“c++帶頭節點的循環鏈表及兩個循環鏈表的合并方法是什么”吧!
#include <iostream> using namespace std; bool cir=0; #if 1 // 0 為隊列一樣的鏈表 typedef struct cirlist { int data; struct cirlist *next; }CIR_LIST; CIR_LIST* create_list() { CIR_LIST* list = new (CIR_LIST); list->data = 999; list->next = list; } void list_append(CIR_LIST** list,int data) //鏈表的后面加入數據 { CIR_LIST* newlist = new(CIR_LIST); newlist->data = data; newlist->next =(*list)->next; (*list)->next = newlist; *list = newlist; // 類似隊列存儲 out 1 2 3 4 } void list_delete(CIR_LIST* list, int data) { CIR_LIST* front = NULL; CIR_LIST* now = list->next->next; while(now !=list->next) { if(data ==now->data) { if(now ==list->next->next) { list->next->next = now->next; delete(now); now = list->next->next ; // break; } else { front->next = now->next; delete(now); now = front->next; // break; } } else { front = now; now = now->next; } } } void print(CIR_LIST* list) { CIR_LIST*node = list->next; // while(node != list) //4 3 2 1 // { // cout<< node->data <<" "; // node = node->next; // } while(node->next != list->next) //1 2 3 4 { cout<< node->next->data <<" "; node = node->next; } cout<<endl; } CIR_LIST* destroy_node(CIR_LIST* node) { CIR_LIST* next = node->next; delete(node); node=NULL; return next; } void destroy_list(CIR_LIST* list) { CIR_LIST* node = list->next->next; while(node !=list->next) { node = destroy_node(node); } } CIR_LIST* list_add(CIR_LIST*list, CIR_LIST*list1) { CIR_LIST* head =list->next; list->next = list1->next->next; CIR_LIST* head1 = list1->next; list1->next = head; delete(head1); return list1; } int list_size(CIR_LIST* list) { int i=0; CIR_LIST* node = list->next; while(node->next !=list->next) { i++; node=node->next; } return i; } int main () { CIR_LIST* list = create_list(); list_append(&list, 1); list_append(&list, 2); list_append(&list, 3); list_append(&list, 4); cout<<"list size:"<<" "; cout<<list_size(list)<<" "; cout<<"print the list:"<<endl; print(list); CIR_LIST* list1 = create_list(); list_append(&list1, -1); list_append(&list1, -2); list_append(&list1, -3); list_append(&list1, -4); cout<<"list size:"<<" "; cout<<list_size(list1)<<" "; cout<<"print the list1:"<<endl; print(list1); CIR_LIST*list3 = list_add(list, list1); cout<<"list size:"<<" "; cout<<list_size(list3)<<" "; cout<<"print the list3:"<<endl; print(list3); cout<<"delete 1 and -4:"<<endl; list_delete(list3,1); list_delete(list3,-4); print(list3); destroy_list(list3); int a=1; return 0; } #else typedef int ElemType; typedef struct Node { ElemType elem; struct Node *next; }Node,*linklist; //創建循環鏈表 Node *createList(Node *head,int n) { Node *p; int a[]={1,2,3,4,5,6,7,8,9}; for(int i=0;i<n;i++) { p=new Node; p->elem=a[i]; p->next=head->next; head->next=p; } return head; } //遍歷循環鏈表 void printList(Node *head) { Node *p; p=head->next; while(p!=head) { cout<<p->elem<<" "; p=p->next; } cout<<endl; } Node* destroy_node(Node *node) { Node* next = node->next; delete(node); return next; } void destroy_list(Node *head) { Node *p; p=head->next; while(p!=head) { p=destroy_node(p); } } int main() { Node *head,*p,*q; head=new Node; head->elem = 0; head->next=head; createList(head,4); printList(head); destroy_list(head); return 0; } #endif
到此,相信大家對“c++帶頭節點的循環鏈表及兩個循環鏈表的合并方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。