完整的Linked List程式
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node * next;
};
int main()
{
Node *head= NULL, *tail, *ptr, *temp, *prev, *next;
int selection, value, key, found, index, i;
while (1)
{
system("cls");
cout << "==== Function List ====" << endl;
cout << "[1] Create linked list"<<endl;
cout << "[2] Push the Node after last Node"<<endl;
cout << "[3] Delete the last Node"<<endl;
cout << "[4] Display whole linked list" << endl;
cout << "[5] Search Node"<<endl;
cout << "[6] Insert a Node before given position"<<endl;
cout << "[7] Delete a Node at a given position"<<endl;
cout << "[8] Reverse linked list" << endl;
cout << "==================" << endl << endl;
cout << "Enter function number:";
cin >> selection;
if (selection !=1 & head == NULL)
{
cout << "You haven't create a linked list yet" << endl;
system("pause");
continue;
}
switch (selection)
{
case 1:
cout << "Enter value of new Node:";
cin >> value;
if (head == NULL)
{
head = new Node;
head ->next=NULL;
head ->data=value;
tail = head;
cout << "Create a Linked list and new Node is " << value << endl;
}
else
{
cout << "You already created linked list" << endl;
}
system("pause");
break;
case 2:
cout << "Enter the new Node value:";
cin >> value;
cout << "You enter " << value << " into linked list" << endl;
ptr = new Node;
ptr ->data=value;
ptr ->next=NULL;
tail ->next=ptr;
tail = ptr;
ptr = head;
while (ptr != NULL)
{
cout << ptr ->data << " ";
ptr = ptr ->next;
}
system("pause");
break;
case 4:
ptr = head;
while (ptr != NULL)
{
cout << ptr ->data << " ";
ptr = ptr ->next;
}
system("pause");
break;
case 3:
if (head == tail)
{
ptr = head;
head = NULL;
delete ptr;
cout << "Clear linked list" << endl;
system("pause");
break;
}
ptr = head;
while (ptr ->next != tail || head == tail)
{
ptr = ptr ->next;
}
ptr ->next=NULL;
delete tail;
tail = ptr;
ptr = head;
while (ptr != NULL)
{
cout << ptr ->data << " ";
ptr = ptr ->next;
}
system("pause");
break;
case 5:
cout << "Which value you want to search:";
cin >> key;
found = false;
ptr = head;
while (ptr != NULL)
{
if (ptr ->data == key)
{
found = true;
break;
}
ptr = ptr ->next;
}
if (found)
cout << key << " is found" << endl;
else
cout << key << " is not found" << endl;
ptr = head;
while (ptr != NULL)
{
cout << ptr ->data << " ";
ptr = ptr ->next;
}
system("pause");
break;
case 6:
i = 0;
ptr = head;
while (ptr != NULL)
{
cout << "[" << i++ << "]:" << ptr ->data << " ";
ptr = ptr ->next;
}
cout << "Enter the index of Node you want to insert before:";
cin >> index;
cout << "Enter the value of new Node:";
cin >> value;
if(index==0)
{
ptr = new Node;
ptr->data=value;
ptr->next=head;
head=ptr;
}
else
{
i = 0;
ptr = head;
while (ptr != NULL)
{
if (i+1 == index)
{
temp = new Node;
temp ->data=value;
temp ->next = ptr ->next;
ptr ->next = temp;
break;
}
ptr = ptr ->next;
i++;
}
}
ptr = head;
while (ptr != NULL)
{
cout << ptr ->data << " ";
ptr = ptr ->next;
}
system("pause");
break;
case 7:
i = 0;
ptr = head;
while (ptr != NULL)
{
cout << "[" << i++ << "]:" << ptr ->data << " ";
ptr = ptr ->next;
}
cout << "Enter the index of Node to delete:";
cin >> index;
if (index == 0)
{
ptr = head;
head = head ->next;
delete ptr;
}
else
{
i = 0;
ptr = head;
while (ptr != NULL)
{
if ((i + 1) == index)
{
if (ptr ->next == tail)
{
tail = ptr;
}
temp = ptr ->next;
ptr ->next=ptr ->next ->next;
delete temp;
break;
}
ptr = ptr ->next;
i++;
}
}
ptr = head;
while (ptr != NULL)
{
cout << ptr ->data << " ";
ptr = ptr ->next;
}
system("pause");
break;
case 8:
prev = NULL;
next = NULL;
tail = ptr = head;
while (ptr != NULL)
{
next = ptr ->next;
ptr ->next = prev;
prev = ptr;
ptr = next;
}
head = prev;
ptr = head;
while (ptr != NULL)
{
cout << ptr ->data << " ";
ptr = ptr ->next;
}
system("pause");
break;
default:
break;
}
}
}