标签:creat ima let lib else return com ffffff proc
//逆序操作//节点只有1个或者无有效节点时,返回原来的链表,不用进行逆序操作
if((NULL == pFirstNode) | (NULL == pFirstNode->pNext))
{
return 0;
}
//节点数两个或以上时
else
{
p = p->pNext; //节点1
pBack = p->pNext; //节点2
pPrev = p; //节点1
p = pBack; //节点2
while(NULL != p->pNext)
{
pBack = p->pNext; //节点3
pH->pNext = p;
p->pNext = pPrev;
pPrev = p;
p = pBack;
}
}
pH->pNext = p;
p->pNext = pPrev;
pFirstNode->pNext = NULL;
return 0;
}
运行结果:
整体代码:
#include <stdio.h>
#include <stdlib.h>
//单链表的节点模型。
struct node
{
int data;
struct node *pNext;
};
//创建一个节点。
//有效数据为data。
//返回一个指针,该指针指向struct node类型,即创建的节点。
struct node create_node(int data)
{
//malloc申请一段内存来存储数据
struct node p = (struct node *)malloc(sizeof(struct node));
//检验是否申请内存成功
if(NULL == p)
{
printf("malloc error.\n");
return NULL;
}
p->data = data;
p->pNext = NULL;
return p;
}
//从尾部插入一个新的节点new,pH为头节点
void insert_tail(struct node pH, struct node new)
{
struct node *p = pH;
//让p指向最后一个节点
while(NULL != p->pNext)
{
p = p->pNext;
}
p->pNext = new;
}
//从头部插入一个新的节点,pH为头节点,new为新插入节点
void insert_head(struct node pH, struct node new)
{
struct node *p = pH;
if(NULL == p->pNext)
{
p->pNext = new;
}
else
{
new->pNext = p->pNext;
p->pNext = new;
}
}
//遍历节点
void ergodic(struct node pH)
{
struct node p = pH;
while(NULL != p->pNext)
{
p = p->pNext;
printf("node data is %d\n",p->data);
}
}
//删除存储数据为data的节点
void delete_node(struct node pH, int data)
{
struct node p = pH;
struct node *pPrev = NULL;
while(NULL != p->pNext)
{
pPrev = p; //这个指针用来存储上一个节点的地址
p = p->pNext; //p指向下一个节点
//找到要删除的节点
if(p->data == data)
{
//这个节点是尾节点
if(NULL == p->pNext)
{
pPrev->pNext = NULL;
}
//不是尾节点
else
{
pPrev->pNext = p->pNext;
}
free(p); //删除节点,释放节点的存储空间
}
}
}
//逆序操作
//算法1:从第二个节点开始,记录它的下一个节点,然后依次挪到第一个节点之前成为新表头
int inverse_node(struct node pH)
{
struct node p = pH; //头结点
struct node pPrev = NULL; //记录前一个节点
struct node pBack = NULL; //记录下一个节点地址
struct node *pFirstNode = p->pNext; //记录第一个节点
//节点只有1个或者无有效节点时,返回原来的链表,不用进行逆序操作
if((NULL == pFirstNode) | (NULL == pFirstNode->pNext))
{
return 0;
}
//节点数两个或以上时
else
{
p = p->pNext; //节点1
pBack = p->pNext; //节点2
pPrev = p; //节点1
p = pBack; //节点2
while(NULL != p->pNext)
{
pBack = p->pNext; //节点3
pH->pNext = p;
p->pNext = pPrev;
pPrev = p;
p = pBack;
}
}
pH->pNext = p;
p->pNext = pPrev;
pFirstNode->pNext = NULL;
return 0;
}
int main(void)
{
//头指针指向头结点
struct node pHead = NULL;
pHead = create_node(0);
//struct node pHead = create_node(0);
insert_tail(pHead, create_node(11));
insert_tail(pHead, create_node(12));
insert_tail(pHead, create_node(13));
insert_tail(pHead, create_node(14));
insert_tail(pHead, create_node(15));
insert_tail(pHead, create_node(16));
insert_tail(pHead, create_node(17));
ergodic(pHead);
inverse_node(pHead);
printf("------------逆序后------------\n");
ergodic(pHead);
/
pHead->pNext = create_node(11);
pHead->pNext->pNext = create_node(12);
pHead->pNext->pNext->pNext = create_node(13);
/
/
printf("node1 data : %d\n",pHead->pNext->data);
printf("node2 data : %d\n",pHead->pNext->pNext->data);
printf("node3 data : %d\n",pHead->pNext->pNext->pNext->data);
/
return 0;
}
标签:creat ima let lib else return com ffffff proc
原文地址:http://blog.51cto.com/wuweizhu/2138904