单链表自我实践(可能有不恰当之处)
#include<stdio.h>
struct Node
{
int data;
Node *Next;
};
//该测试例题的索引号从 1开始
//下列函数均未对 head是否为 NULL进行判断处理,当传入的 head为 NULL会报错
void AddLast(Node **head, const int n) //注意 head是指针的指针
{
Node *newSt = new Node;
newSt->data = n;
newSt->Next = NULL;
if (*head!=NULL)
{
Node *Temp = *head;
while (Temp->Next!=NULL)
{
Temp = Temp->Next;
}
Temp->Next = newSt;
}
else
{
*head = newSt;
}
}
void AddFirst(Node **head, const int n) //注意 head是指针的指针
{
Node *newSt = new Node;
newSt->data = n;
newSt->Next = *head;
*head = newSt;
}
int GetLength(Node *head)
{
Node *temp = head;
int Length = 0;
while (temp)
{
temp = temp->Next;
Length++;
}
return Length;
}
int DataAt(Node *head, int pos)
{
Node *temp = head;
if (pos<=0||pos>GetLength(temp))
{
printf("输入的位置 pos不正确 !");
return -1; // -1代表输入错误,也许 pos位置的值也为 -1,所以不太恰当
}
while (pos>1)
{
temp = temp->Next;
pos--;
}
return temp->data;
}
bool Insert(Node **head, int pos, const int data)
{
if (pos<1||pos>GetLength(*head)+1)
{
printf("插入位置不正确 !");
return false;
}
Node *newNode = new Node;
newNode->data = data;
if (pos==1)
{
newNode->Next = *head;
*head = newNode;
}
else if(pos==GetLength(*head)+1)
{
Node *temp = *head;
while (temp->Next!=NULL)
{
temp = temp->Next;
}
newNode->Next = NULL;
temp->Next= newNode;
}
else
{
Node *temp = *head;
while (pos>2) //得到 pos的前一位置
{
temp = temp->Next;
pos--;
}
Node *tempNode = temp->Next;
temp->Next = newNode;
newNode->Next = tempNode;
}
return true;
}
bool Search(Node *head, const int x, int &pos) //查找值为 x的节点,位置保存在 pos中
{
Node *temp = head;
pos = 1;
while (temp->Next!=NULL)
{
if (temp->data==x)
{
return true;
}
pos++;
temp = temp->Next;
}
if (temp->data==x) //判断最后一位是否是 x
{
return true;
}
pos = -1;
return false;
}
void DeleteNode(Node **head, int pos) //删除位于 pos处的节点
{
if (pos<1||pos>GetLength(*head))
{
printf("要删除的节点不存在 !");
return;
}
Node *temp = *head;
if (pos==1) //有删除第一个节点的情况,所以 head必须用指针的指针;若不删除第一个 //节点则用指针就可以
{
temp = temp->Next;
delete *head;
*head = temp;
}
else
{
pos--;
while (pos>1) //找到要删除的节点的前一个节点
{
temp = temp->Next;
pos--;
}
if (temp->Next->Next==NULL)
{
delete temp->Next;
temp->Next = NULL;
}
else
{
Node *p = temp->Next->Next;
delete temp->Next;
temp->Next = p;
}
}
}
void DeleteData(Node **head, const int x) //删除值为 x的节点
{
int pos;
while (Search(*head,x,pos))
{
DeleteNode(head,pos);
}
}
void DeleteAll(Node **head) //清空链表
{
Node *temp = *head;
while (temp)
{
temp = temp->Next;
delete *head;
*head = temp;
}
}
void Reverse(Node **head) //反转链表
{
Node *temp=*head;
Node *p;
*head = NULL;
while (temp!=NULL)
{
p = temp;
temp = temp->Next;
p->Next = *head;
*head = p;
}
}
Node *GetNodeLast(Node *head, int n) //得到倒数第 n个节点的指针
{
if (n<1||n>GetLength(head))
{
printf("参数 n超出范围 !");
return head; //如果超出范围,则返回第一个节点。
}
Node *fast = head;
Node *slow = head;
while (n-->1)
{
fast = fast->Next;
}
while (fast->Next!=NULL)
{
fast = fast->Next;
slow = slow->Next;
}
return slow;
}
Node *GetMidNode(Node *head) //得到链表的中间位置
{
Node *fast = head;
Node *slow = head;
while (fast->Next!=NULL && fast->Next->Next!=NULL)
{
fast = fast->Next->Next;
slow = slow->Next;
}
return slow;
}
bool IsLoop(Node *head) //判断是否有环
{
Node *fast = head;
Node *slow = head;
while (fast->Next!=NULL && fast->Next->Next!=NULL)
{
fast = fast->Next->Next;
slow = slow->Next;
if (fast==slow)
{
return true;
}
}
return false;
}
void Bubble_Sort(Node **head) //从小到大排序
{
Node *temp = *head;
while (temp->Next!=NULL)
{
Node *temp2 = temp->Next;
while (temp2!=NULL)
{
if (temp2->data<temp->data)
{
int n = temp2->data;
temp2->data = temp->data;
temp->data = n;
}
temp2 = temp2->Next;
}
temp = temp->Next;
}
}
Node *Join(Node *head1, Node *head2) //合并两个已排序的链表
{
Node *pNode, *pHead;
pNode = pHead = NULL;
while(head1!=NULL&&head2!=NULL)
{
if(head1->data <= head2->data)
{
if (pNode==NULL)
{
pHead = pNode = head1;
}
else
{
pNode->Next = head1;
pNode = pNode->Next;
}
head1 = head1->Next;
}
else
{
if (pNode==NULL)
{
pHead = pNode = head2;
}
else
{
pNode->Next = head2;
pNode = pNode->Next;
}
head2 = head2->Next;
}
}
if (head1!=NULL)
{
pNode->Next = head1;
}
if (head2!= NULL)
{
pNode->Next = head2;
}
return pHead;
}
void Display(Node *head)
{
while (head)
{
printf("%2d ", head->data);
head = head->Next;
}
printf("/n");
}
void main()
{
Node *p=NULL;
AddLast(&p,1);
AddLast(&p,9);
AddLast(&p,7);
AddLast(&p,-1);
Insert(&p,4,6);
Node *p2=NULL;
AddLast(&p2,2);
AddLast(&p2,5);
AddLast(&p2,18);
AddLast(&p2,0);
Display(p);
Display(p2);
Reverse(&p);
Reverse(&p2);
Display(p);
Display(p2);
Bubble_Sort(&p);
Bubble_Sort(&p2);
Display(p);
Display(p2);
Node *tp = Join(p,p2);
Display(tp);
}
#include<stdio.h>
struct Node
{
int data;
Node *Next;
};
//该测试例题的索引号从 1开始
//下列函数均未对 head是否为 NULL进行判断处理,当传入的 head为 NULL会报错
void AddLast(Node **head, const int n) //注意 head是指针的指针
{
Node *newSt = new Node;
newSt->data = n;
newSt->Next = NULL;
if (*head!=NULL)
{
Node *Temp = *head;
while (Temp->Next!=NULL)
{
Temp = Temp->Next;
}
Temp->Next = newSt;
}
else
{
*head = newSt;
}
}
void AddFirst(Node **head, const int n) //注意 head是指针的指针
{
Node *newSt = new Node;
newSt->data = n;
newSt->Next = *head;
*head = newSt;
}
int GetLength(Node *head)
{
Node *temp = head;
int Length = 0;
while (temp)
{
temp = temp->Next;
Length++;
}
return Length;
}
int DataAt(Node *head, int pos)
{
Node *temp = head;
if (pos<=0||pos>GetLength(temp))
{
printf("输入的位置 pos不正确 !");
return -1; // -1代表输入错误,也许 pos位置的值也为 -1,所以不太恰当
}
while (pos>1)
{
temp = temp->Next;
pos--;
}
return temp->data;
}
bool Insert(Node **head, int pos, const int data)
{
if (pos<1||pos>GetLength(*head)+1)
{
printf("插入位置不正确 !");
return false;
}
Node *newNode = new Node;
newNode->data = data;
if (pos==1)
{
newNode->Next = *head;
*head = newNode;
}
else if(pos==GetLength(*head)+1)
{
Node *temp = *head;
while (temp->Next!=NULL)
{
temp = temp->Next;
}
newNode->Next = NULL;
temp->Next= newNode;
}
else
{
Node *temp = *head;
while (pos>2) //得到 pos的前一位置
{
temp = temp->Next;
pos--;
}
Node *tempNode = temp->Next;
temp->Next = newNode;
newNode->Next = tempNode;
}
return true;
}
bool Search(Node *head, const int x, int &pos) //查找值为 x的节点,位置保存在 pos中
{
Node *temp = head;
pos = 1;
while (temp->Next!=NULL)
{
if (temp->data==x)
{
return true;
}
pos++;
temp = temp->Next;
}
if (temp->data==x) //判断最后一位是否是 x
{
return true;
}
pos = -1;
return false;
}
void DeleteNode(Node **head, int pos) //删除位于 pos处的节点
{
if (pos<1||pos>GetLength(*head))
{
printf("要删除的节点不存在 !");
return;
}
Node *temp = *head;
if (pos==1) //有删除第一个节点的情况,所以 head必须用指针的指针;若不删除第一个 //节点则用指针就可以
{
temp = temp->Next;
delete *head;
*head = temp;
}
else
{
pos--;
while (pos>1) //找到要删除的节点的前一个节点
{
temp = temp->Next;
pos--;
}
if (temp->Next->Next==NULL)
{
delete temp->Next;
temp->Next = NULL;
}
else
{
Node *p = temp->Next->Next;
delete temp->Next;
temp->Next = p;
}
}
}
void DeleteData(Node **head, const int x) //删除值为 x的节点
{
int pos;
while (Search(*head,x,pos))
{
DeleteNode(head,pos);
}
}
void DeleteAll(Node **head) //清空链表
{
Node *temp = *head;
while (temp)
{
temp = temp->Next;
delete *head;
*head = temp;
}
}
void Reverse(Node **head) //反转链表
{
Node *temp=*head;
Node *p;
*head = NULL;
while (temp!=NULL)
{
p = temp;
temp = temp->Next;
p->Next = *head;
*head = p;
}
}
Node *GetNodeLast(Node *head, int n) //得到倒数第 n个节点的指针
{
if (n<1||n>GetLength(head))
{
printf("参数 n超出范围 !");
return head; //如果超出范围,则返回第一个节点。
}
Node *fast = head;
Node *slow = head;
while (n-->1)
{
fast = fast->Next;
}
while (fast->Next!=NULL)
{
fast = fast->Next;
slow = slow->Next;
}
return slow;
}
Node *GetMidNode(Node *head) //得到链表的中间位置
{
Node *fast = head;
Node *slow = head;
while (fast->Next!=NULL && fast->Next->Next!=NULL)
{
fast = fast->Next->Next;
slow = slow->Next;
}
return slow;
}
bool IsLoop(Node *head) //判断是否有环
{
Node *fast = head;
Node *slow = head;
while (fast->Next!=NULL && fast->Next->Next!=NULL)
{
fast = fast->Next->Next;
slow = slow->Next;
if (fast==slow)
{
return true;
}
}
return false;
}
void Bubble_Sort(Node **head) //从小到大排序
{
Node *temp = *head;
while (temp->Next!=NULL)
{
Node *temp2 = temp->Next;
while (temp2!=NULL)
{
if (temp2->data<temp->data)
{
int n = temp2->data;
temp2->data = temp->data;
temp->data = n;
}
temp2 = temp2->Next;
}
temp = temp->Next;
}
}
Node *Join(Node *head1, Node *head2) //合并两个已排序的链表
{
Node *pNode, *pHead;
pNode = pHead = NULL;
while(head1!=NULL&&head2!=NULL)
{
if(head1->data <= head2->data)
{
if (pNode==NULL)
{
pHead = pNode = head1;
}
else
{
pNode->Next = head1;
pNode = pNode->Next;
}
head1 = head1->Next;
}
else
{
if (pNode==NULL)
{
pHead = pNode = head2;
}
else
{
pNode->Next = head2;
pNode = pNode->Next;
}
head2 = head2->Next;
}
}
if (head1!=NULL)
{
pNode->Next = head1;
}
if (head2!= NULL)
{
pNode->Next = head2;
}
return pHead;
}
void Display(Node *head)
{
while (head)
{
printf("%2d ", head->data);
head = head->Next;
}
printf("/n");
}
void main()
{
Node *p=NULL;
AddLast(&p,1);
AddLast(&p,9);
AddLast(&p,7);
AddLast(&p,-1);
Insert(&p,4,6);
Node *p2=NULL;
AddLast(&p2,2);
AddLast(&p2,5);
AddLast(&p2,18);
AddLast(&p2,0);
Display(p);
Display(p2);
Reverse(&p);
Reverse(&p2);
Display(p);
Display(p2);
Bubble_Sort(&p);
Bubble_Sort(&p2);
Display(p);
Display(p2);
Node *tp = Join(p,p2);
Display(tp);
}