#include <iostream>
using namespace std;
typedef struct listnode
{
int data;
struct listnode *next;
}LIST_NODE;
typedef struct list
{
LIST_NODE* head;
}LIST;
LIST* create_list()
{
LIST* list = new (LIST);
list->head = NULL;
return list;
}
LIST_NODE* create_node(int data)
{
LIST_NODE* node = new (LIST_NODE);
node->data = data;
node->next = NULL;
}
void list_append(LIST* list, int data) //类似于 链式存储栈一样
{
LIST_NODE* node = create_node(data);
if(NULL==list->head)
{
list->head = node;
}
else
{
node->next = list->head;
list->head = node;
}
}
void list_end_append(LIST* list, int data)
{
LIST_NODE* node =NULL;
for(node=list->head; node; node=node->next)
{
if(NULL==node->next)
{
break;
}
}
if(node)
{
node->next = create_node(data);
}
else
{
list->head = create_node(data);
}
}
void print(LIST* list)
{
LIST_NODE* node = NULL;
for(node = list->head;node;node =node->next)
{
cout<< node->data <<" ";
}
cout<<endl;
}
bool list_insert(LIST* list,int after, int now) //now->after
{
LIST_NODE* node = NULL;
for(node=list->head;node;node=node->next)
{
if(now == node->data)
{
break;
}
}
if(node)
{
LIST_NODE*node_new = create_node(after);
node_new->next =node->next;
node->next = node_new;
return true;
}
return false;
}
void list_del(LIST* list,int data)
{
LIST_NODE* front =NULL;
LIST_NODE* node =list->head;
while(node)
{
if(data == node->data)
{
if(node ==list->head)
{
list->head = node->next;
delete(node);
node =NULL;
node = list->head;//值为data的node全部删除
}
else
{
front->next =node->next;
delete(node);
node =NULL;
node = front->next; //值为data的node全部删除
}
}
else
{
front =node;
node=node->next;
}
}
}
LIST_NODE* destroy_node(LIST_NODE* node)
{
LIST_NODE* next = node->next;
delete(node);
return next;
}
void destroy_list(LIST* list)
{
while(list->head)
{
list->head = destroy_node(list->head);
}
delete(list);
}
void list_reverse(LIST* list)
{
LIST_NODE* temp = NULL;
LIST_NODE* new_head = NULL;
LIST_NODE* node = list->head;
while(node)
{
temp = node;
node = node->next;
temp->next =new_head;
new_head = temp;
}
list->head = new_head;
}
LIST_NODE* middle_node(LIST* list)
{
LIST_NODE* slow = list->head;
LIST_NODE* fast = list->head;
while(fast->next && fast->next->next)
{
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
bool isCircle(LIST* list)
{
NODE* slow = list->head;
NODE* fast = list->head;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
{
break;
}
}
return ! (fast == NULL || fast -> next == NULL);
}
int main()
{
LIST* list = create_list();
for(int i=10; i<=50;i+=10)
{
list_append(list,1);
}
cout<<"print the list:"<<endl;
print(list);
list_insert(list,5,10);
cout<<" insert 5 after 10:"<<endl;
print(list);
list_del(list,30);
cout<<" after delete 30:"<<endl;
print(list);
list_del(list,50);
cout<<" after delete 50:"<<endl;
print(list);
list_end_append(list,3);
list_end_append(list,4);
list_end_append(list,5);
cout<<" list end append:"<<endl;
print(list);
list_reverse(list);
cout<<" reverse the list:"<<endl;
print(list);
cout<<" middle node of the list:"<<endl;
cout<<middle_node(list)->data <<endl;
destroy_list(list);
return 0;
}