#include <iostream>
using namespace std;
typedef struct LinkList
{
int Data;
struct LinkList *pNext;
}Node, *pLinkList;
Node* NodeCreat()
{
int nums;
cout<<"Please input the counts of the LinkList: ";
cin>>nums;
if(nums < 1)
{
return NULL;
}
Node *head = new Node;
Node *curr = NULL;
int mdate;
cout<<"Please input the data of this node: ";
cin>>mdate;
head->Data = mdate;
curr = head;
while(nums - 1)
{
Node *newNode = new Node;
cout<<"Please input the data of this node: ";
cin>>mdate;
newNode->Data = mdate;
curr->pNext = newNode;
curr = curr->pNext;
nums--;
}
curr->pNext = NULL;
return head;
}
int GetListLenth(Node *head)
{
int num = 0;
Node *p = head;
while(p)
{
num++;
p = p->pNext;
}
return num;
}
void TraverseList(Node* head)
{
int counts = 0;
Node* p = head;
while(p)
{
counts++;
cout<<counts<<" = "<<p->Data<<endl;
p = p->pNext;
}
}
Node* InsertNode(Node* head, int index)
{
Node *p = head;
Node *temp = new Node;
cout<<"Input the wanted data:";
cin>>temp->Data;
cout<<endl;
if(index <= 1)
{
temp->pNext = head;
temp =head;
index = 0;
return head;
}
while(index - 1)
{
p = p->pNext;
index--;
}
temp->pNext = p->pNext;
p->pNext = temp;
return head;
}
Node* DeleteNode(Node *head, int index)
{
cout<<"delete the index "<<index<<endl;
Node *p = head;
Node *temp = NULL;
if(index <= 1)
{
p = p->pNext;
index = 0;
head = p;
delete p;
p = NULL;
return head;
}
while(index - 1)
{
p = p->pNext;
index--;
}
temp = p->pNext;
p->pNext = temp->pNext;
delete temp;
temp = NULL;
return head;
}
Node *ReverseList(Node *head)
{
Node *p1 = head;
Node *p2 = p1->pNext;
Node *p3;
if(!head && !(head->pNext))
return head;
while(p2)
{
p3 = p2->pNext;
p2->pNext = p1;
p1 = p2;
p2 = p3;
}
head ->pNext = NULL;
head = p1;
return head;
}
void MergeLists(Node *LA, Node* LB, pLinkList &LC)
{
if(!LA)
LC = LB;
if(!LB)
LC = LA;
if(LA->Data < LB->Data)
{
LC = LA;
LA = LA->pNext;
}
else
{
LC = LB;
LB = LB->pNext;
}
Node *temp = NULL;
temp = LC;
while (LA && LB)
{
if(LA->Data < LB->Data)
{
temp->pNext = LA;
LA = LA->pNext;
}
else
{
temp->pNext = LB;
LB = LB->pNext;
}
temp = temp->pNext;
}
if(!LA)
temp->pNext = LB;
if(!LB)
temp->pNext = LA;
}
Node* ListSort(Node* head)
{
int lenth;
int temp;
Node *p;
p = head;
if(!head || !head->pNext)
return head;
lenth = GetListLenth(head);
for(int i = 0; i < lenth - 1; i++)
{
p = head;
for (int j = 0; j < lenth - i -1; j++)
{
if(p->Data > p->pNext->Data)
{
temp = p->Data;
p->Data = p->pNext->Data;
p->pNext->Data = temp;
}
p = p->pNext;
}
}
return head;
}
void main()
{
int index;
Node* head = NodeCreat();
TraverseList(head);
head = ListSort(head);
TraverseList(head);
Node* head2 = NodeCreat();
TraverseList(head2);
head2 = ListSort(head2);
TraverseList(head2);
Node* head3;
MergeLists(head, head2, head3);
TraverseList(head3);
head = InsertNode(head, 3);
TraverseList(head);
head = DeleteNode(head, 3);
TraverseList(head);
head = ReverseList(head);
TraverseList(head);
cin>>index;
cout<<endl;
}
using namespace std;
typedef struct LinkList
{
int Data;
struct LinkList *pNext;
}Node, *pLinkList;
Node* NodeCreat()
{
int nums;
cout<<"Please input the counts of the LinkList: ";
cin>>nums;
if(nums < 1)
{
return NULL;
}
Node *head = new Node;
Node *curr = NULL;
int mdate;
cout<<"Please input the data of this node: ";
cin>>mdate;
head->Data = mdate;
curr = head;
while(nums - 1)
{
Node *newNode = new Node;
cout<<"Please input the data of this node: ";
cin>>mdate;
newNode->Data = mdate;
curr->pNext = newNode;
curr = curr->pNext;
nums--;
}
curr->pNext = NULL;
return head;
}
int GetListLenth(Node *head)
{
int num = 0;
Node *p = head;
while(p)
{
num++;
p = p->pNext;
}
return num;
}
void TraverseList(Node* head)
{
int counts = 0;
Node* p = head;
while(p)
{
counts++;
cout<<counts<<" = "<<p->Data<<endl;
p = p->pNext;
}
}
Node* InsertNode(Node* head, int index)
{
Node *p = head;
Node *temp = new Node;
cout<<"Input the wanted data:";
cin>>temp->Data;
cout<<endl;
if(index <= 1)
{
temp->pNext = head;
temp =head;
index = 0;
return head;
}
while(index - 1)
{
p = p->pNext;
index--;
}
temp->pNext = p->pNext;
p->pNext = temp;
return head;
}
Node* DeleteNode(Node *head, int index)
{
cout<<"delete the index "<<index<<endl;
Node *p = head;
Node *temp = NULL;
if(index <= 1)
{
p = p->pNext;
index = 0;
head = p;
delete p;
p = NULL;
return head;
}
while(index - 1)
{
p = p->pNext;
index--;
}
temp = p->pNext;
p->pNext = temp->pNext;
delete temp;
temp = NULL;
return head;
}
Node *ReverseList(Node *head)
{
Node *p1 = head;
Node *p2 = p1->pNext;
Node *p3;
if(!head && !(head->pNext))
return head;
while(p2)
{
p3 = p2->pNext;
p2->pNext = p1;
p1 = p2;
p2 = p3;
}
head ->pNext = NULL;
head = p1;
return head;
}
void MergeLists(Node *LA, Node* LB, pLinkList &LC)
{
if(!LA)
LC = LB;
if(!LB)
LC = LA;
if(LA->Data < LB->Data)
{
LC = LA;
LA = LA->pNext;
}
else
{
LC = LB;
LB = LB->pNext;
}
Node *temp = NULL;
temp = LC;
while (LA && LB)
{
if(LA->Data < LB->Data)
{
temp->pNext = LA;
LA = LA->pNext;
}
else
{
temp->pNext = LB;
LB = LB->pNext;
}
temp = temp->pNext;
}
if(!LA)
temp->pNext = LB;
if(!LB)
temp->pNext = LA;
}
Node* ListSort(Node* head)
{
int lenth;
int temp;
Node *p;
p = head;
if(!head || !head->pNext)
return head;
lenth = GetListLenth(head);
for(int i = 0; i < lenth - 1; i++)
{
p = head;
for (int j = 0; j < lenth - i -1; j++)
{
if(p->Data > p->pNext->Data)
{
temp = p->Data;
p->Data = p->pNext->Data;
p->pNext->Data = temp;
}
p = p->pNext;
}
}
return head;
}
void main()
{
int index;
Node* head = NodeCreat();
TraverseList(head);
head = ListSort(head);
TraverseList(head);
Node* head2 = NodeCreat();
TraverseList(head2);
head2 = ListSort(head2);
TraverseList(head2);
Node* head3;
MergeLists(head, head2, head3);
TraverseList(head3);
head = InsertNode(head, 3);
TraverseList(head);
head = DeleteNode(head, 3);
TraverseList(head);
head = ReverseList(head);
TraverseList(head);
cin>>index;
cout<<endl;
}
浙公网安备 33010602011771号