//逆序操作
//算法1:从第二个节点开始,记录它的下一个节点,然后依次挪到第一个节点之前成为新表头
int inverse_node(struct node pH)
{
struct nodep = pH; //头结点
struct node pPrev = NULL; //记录前一个节点
struct nodepBack = 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;
}
运行结果:
整体代码:
#include
#include
//单链表的节点模型。
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 nodenew)
{
struct node *p = pH;
//让p指向最后一个节点
while(NULL != p->pNext)
{
p = p->pNext;
}
p->pNext = new;
}
//从头部插入一个新的节点,pH为头节点,new为新插入节点
void insert_head(struct node pH, struct nodenew)
{
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 nodep = 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 nodep = 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 nodep = pH; //头结点
struct node pPrev = NULL; //记录前一个节点
struct nodepBack = 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 nodepHead = 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;
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。