#include <iostream>
#include <list>
using namespace std;
struct Node
{
int data;
Node* next;
};
Node* initList()
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = 0;
node->next = node;
return node;
}
void headInsert(Node* list, int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = list->next;
list->next = node;
list->data++;
}
void tailInsert(Node* list, int data)
{
Node* head = list;
list->data++;
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = head;
while (list->next!=head)
{
list = list->next;
}
list->next = node;
}
void remove(Node* list, int data)
{
Node* head = list;
Node* pre = list;
Node* cur = list->next;
while (cur->next!=head)
{
if (cur->data == data)
{
pre->next = cur->next;
free(cur);
cur = pre->next;
head->data--;
}
else {
pre = cur;
cur = cur->next;
}
}
}
void printList(Node* node)
{
cout << "size:" << node->data << endl;
Node* head = node;
while (node->next!=head)
{
node = node->next;
cout << node->data << endl;
}
cout << "null" << endl;
}
int main()
{
Node* list = initList();
headInsert(list, 1);
headInsert(list, 2);
headInsert(list, 3);
headInsert(list, 4);
headInsert(list, 5);
tailInsert(list, 6);
tailInsert(list, 7);
tailInsert(list, 8);
tailInsert(list, 9);
tailInsert(list, 10);
printList(list);
remove(list, 3);
remove(list, 8);
printList(list);
}
线性表—单循环链表
最新推荐文章于 2024-10-13 22:37:06 发布