#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
//typedef struct Node* LinkList;
Node* insert(Node* ptr, Node* &specNode, int data)
{
Node *tmp = new Node;
tmp->data = data;
tmp->next = NULL;
if(specNode == NULL) // 初始插入
{
ptr = tmp;
specNode = ptr;
}
else
{
if(specNode->next == NULL) // 尾部插入
{
specNode->next = tmp;
specNode = specNode->next;
}
else // 中間插入
{
tmp->next = specNode->next;
specNode->next = tmp;
}
}
return ptr;
}
Node* deleteNode(Node* ptr, Node* specNode)
{
Node *curr = ptr;
if(curr == specNode) // 頭節點刪除
{
ptr = specNode->next;
specNode->next = NULL;
delete specNode;
}
else // 非頭節點刪除
{
while(curr)
{
if(curr->next == specNode)
{
curr->next = specNode->next;
specNode->next = NULL;
delete specNode;
}
curr = curr->next;
}
}
return ptr;
}
Node* inverse(Node* ptr)
{
Node *first, *tmp ;
first = NULL; // 新head
tmp = ptr; // 跟著舊head走
while(ptr != NULL)
{
ptr = ptr->next;
tmp->next = first;
first = tmp;
tmp = ptr;
}
return first;
}
void display(Node* ptr)
{
while(ptr != NULL)
{
cout << ptr->data <<endl;
ptr = ptr->next;
}
}
int main()
{
Node *first = NULL, *curr = NULL, *test = NULL;
for(int i=0; i<10; i++)
{
first = insert(first, curr, i);
if(i==5)
{
test = curr;
}
}
insert(first, test, 20);
deleteNode(first, test->next);
first = inverse(first);
display(first);
return 0;
}
0 意見