带头节点的循环链表及两个循环链表的合并
2017-03-02 11:36
337 查看
#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; 4000 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
相关文章推荐
- 笔试题四:带头节点head两个链表合并,并且有序
- 将两个带头节点链表单链表,合并成一个新的带头节点链表……
- 采用带头节点的单链表保存单词,返回两个链表共同后缀的起始位置
- 不带头节点的双向循环链表的基本操作
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- josephus问题->不带头节点的循环链表
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- 数据结构:设有一个不带头结点的单链表L,设计两个递归算法,del(L,x)删除单链表L中第一个值为x的节点,delall(L,x)删除单链表L中的最小节点值。
- 写程序。设ha和hb分别是两个带头结点的非递减有序单链表的头指针,试设计算法,将这两个有序链表合并成一个非递增有序的单链表。要求使用原链表空间,
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- 数据结构示例之带头节点的双向循环链表
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- 链表的典型题:找到链表倒数K个节点、链表逆转、合并两个链表
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- 将两个递增有序的带头结点的单链表A、B合并成为一个递增有序的带头结点的单链表
- main2-3-2.c 两个仅设表尾指针的循环链表的合并(教科书图2.13)
- 链表面试题(二):冒泡排序、合并两个有序链表、查找中间节点、查找倒数K个节点
- 带头节点的双向循环链表的增删查改
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表