本文介绍一个简单的关于数据结构的算法题目."对未知长度的单链表进行逆序操作"
代码功能:实现对一个含有头节点长度未知的单向链表逆序,
原始链表为:头->1->2->3->4->5,
经过逆序后:头->5->4->3->2->1.
逆序方法一:pLinkList reverse(pLinkList &List)
遍历整个链表,边遍历边运用头插法的方式将链表元素插入到新的链表中,完成逆序整个链表.
注意:需要给相关节点分配内存后才能对节点进行->next进行赋值等操作.
逆序方法二:pLinkList reverse_2(pLinkList &List)
使用额外的三个指针,p1,p2,p3,可以不需要新建链表,在原来的链表中边遍历边交换相邻的两个节点.
p1,p2用于交换两个相邻节点的指向顺序,p3用于保持原有的链表后续节点不断链.
代码结果示例:
please enter the length of this list: 10
please enter the data of this list:
->1
->2
->3
->4
->5
->6
->7
->8
->9
->0
head->0->9->8->7->6->5->4->3->2->1->NULL
head->1->2->3->4->5->6->7->8->9->0->NULL
head->0->9->8->7->6->5->4->3->2->1->NULL
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
typedef int element;
typedef struct LNode{
element data;
LNode *next;
}LinkList,*pLinkList;
/*功能:头插法初始化链表,即后插入的数据更靠近链表头部,如输入顺序1,2,3,4,5,链表顺序:5,4,3,2,1
*参数 n:表示链表的长度
*/
bool CreateList(pLinkList &List,int n)
{
pLinkList head,p;
element data;
int i = 0;
head = (pLinkList)malloc(sizeof(LinkList));
if(!head)
return ERROR;
List = head;
head->next = NULL;
for(i=0;i<n;i++)
{
p = (pLinkList)malloc(sizeof(LinkList));
if(!p)
return ERROR;
printf("->");
scanf("%d",&data);
p->data = data;
p->next = head->next;
head->next = p;
}
}
/*功能:头插法的方式插入待进入链表的数据
*/
bool InsertNode(pLinkList &List,element data)
{
pLinkList head,TempNode;
//head = List;
TempNode = (pLinkList)malloc(sizeof(LinkList));
if(!TempNode)
return ERROR;
TempNode->data = data;
TempNode->next = List->next;
List->next = TempNode;
return OK;
}
/*名称:逆序单链表方法一
**功能:遍历整个链表,运用头插法的方式逆序整个链表
*/
pLinkList reverse(pLinkList &List)
{
pLinkList TempList;
pLinkList NewList;
NewList = (pLinkList)malloc(sizeof(LinkList));//分配内存后才能对NewList执行InsertNode
if(!NewList){
return ERROR;
}
TempList = List->next;
while(TempList){
InsertNode(NewList,TempList->data);
TempList = TempList->next;
}
return NewList;
}
/*名称:逆序单链表方法二
**功能:利用三个外部指针逆序整个链表
*/
pLinkList reverse_2(pLinkList &List)
{
pLinkList p1,p2,p3;
pLinkList TempList;
pLinkList head;
head = (pLinkList)malloc(sizeof(LinkList));
if(!head){
return ERROR;
}
TempList = List->next;
p1 = TempList;
if(!p1){
return ERROR;
}
p2 = TempList->next;
if(!p2){
head->next = p1;
p1->next = NULL;
return head;
}
p3 = TempList->next->next;
p1->next = NULL;//务必设置此尾节点next 为空.
while(p3){
p2->next = p1;
p1 = p2;
p2 = p3;
p3 = p3->next;//遍历检索所用,保持不会断链
}
p2->next = p1;
head->next = p2;
return head;
}
/*功能:遍历打印输出整个链表
*/
bool OutputList(pLinkList &List)
{
pLinkList TempList;
TempList = List->next;
if(!TempList){
printf("there is no Node of this List.");
return ERROR;
}
printf("\r\nhead");
while(TempList->next)
{
printf("->%d",TempList->data);
TempList = TempList->next;
}
printf("->%d->NULL\n",TempList->data);
return OK;
}
int main()
{
int num = 0;
pLinkList List,NewList,New2List;
printf("please enter the length of this list: ");
scanf("%d",&num);
printf("please enter the data of this list:\r\n");
CreateList(List,num);
OutputList(List);
int data;
NewList = reverse(List);
if(!NewList){
printf("reverse meet an ERROR.\r\n");
return ERROR;
}
OutputList(NewList);
New2List = reverse_2(NewList);
if(!New2List){
printf("reverse_2 meet an ERROR.\r\n");
return ERROR;
}
OutputList(New2List);
return OK;
}