您的位置:首页 > 其它

带头节点的循环链表及两个循环链表的合并

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐