//逆序操作
//算法1:從第二個(gè)節(jié)點(diǎn)開始,記錄它的下一個(gè)節(jié)點(diǎn),然后依次挪到第一個(gè)節(jié)點(diǎn)之前成為新表頭
int inverse_node(struct node pH)
{
struct nodep = pH; //頭結(jié)點(diǎn)
struct node pPrev = NULL; //記錄前一個(gè)節(jié)點(diǎn)
struct nodepBack = NULL; //記錄下一個(gè)節(jié)點(diǎn)地址
struct node *pFirstNode = p->pNext; //記錄第一個(gè)節(jié)點(diǎn)
//節(jié)點(diǎn)只有1個(gè)或者無有效節(jié)點(diǎn)時(shí),返回原來的鏈表,不用進(jìn)行逆序操作
if((NULL == pFirstNode) | (NULL == pFirstNode->pNext))
{
return 0;
}
//節(jié)點(diǎn)數(shù)兩個(gè)或以上時(shí)
else
{
p = p->pNext; //節(jié)點(diǎn)1
pBack = p->pNext; //節(jié)點(diǎn)2
pPrev = p; //節(jié)點(diǎn)1
p = pBack; //節(jié)點(diǎn)2
while(NULL != p->pNext)
{
pBack = p->pNext; //節(jié)點(diǎn)3
pH->pNext = p;
p->pNext = pPrev;
pPrev = p;
p = pBack;
}
}
pH->pNext = p;
p->pNext = pPrev;
pFirstNode->pNext = NULL;
return 0;
}
運(yùn)行結(jié)果:
整體代碼:
#include
#include
//單鏈表的節(jié)點(diǎn)模型。
struct node
{
int data;
struct node *pNext;
};
//創(chuàng)建一個(gè)節(jié)點(diǎn)。
//有效數(shù)據(jù)為data。
//返回一個(gè)指針,該指針指向struct node類型,即創(chuàng)建的節(jié)點(diǎn)。
struct node create_node(int data)
{
//malloc申請一段內(nèi)存來存儲(chǔ)數(shù)據(jù)
struct node p = (struct node *)malloc(sizeof(struct node));
//檢驗(yàn)是否申請內(nèi)存成功
if(NULL == p)
{
printf("malloc error.\n");
return NULL;
}
p->data = data;
p->pNext = NULL;
return p;
}
//從尾部插入一個(gè)新的節(jié)點(diǎn)new,pH為頭節(jié)點(diǎn)
void insert_tail(struct node pH, struct nodenew)
{
struct node *p = pH;
//讓p指向最后一個(gè)節(jié)點(diǎn)
while(NULL != p->pNext)
{
p = p->pNext;
}
p->pNext = new;
}
//從頭部插入一個(gè)新的節(jié)點(diǎn),pH為頭節(jié)點(diǎn),new為新插入節(jié)點(diǎn)
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;
}
}
//遍歷節(jié)點(diǎn)
void ergodic(struct node pH)
{
struct nodep = pH;
while(NULL != p->pNext)
{
p = p->pNext;
printf("node data is %d\n",p->data);
}
}
//刪除存儲(chǔ)數(shù)據(jù)為data的節(jié)點(diǎn)
void delete_node(struct node pH, int data)
{
struct nodep = pH;
struct node *pPrev = NULL;
while(NULL != p->pNext)
{
pPrev = p; //這個(gè)指針用來存儲(chǔ)上一個(gè)節(jié)點(diǎn)的地址
p = p->pNext; //p指向下一個(gè)節(jié)點(diǎn)
//找到要?jiǎng)h除的節(jié)點(diǎn)
if(p->data == data)
{
//這個(gè)節(jié)點(diǎn)是尾節(jié)點(diǎn)
if(NULL == p->pNext)
{
pPrev->pNext = NULL;
}
//不是尾節(jié)點(diǎn)
else
{
pPrev->pNext = p->pNext;
}
free(p); //刪除節(jié)點(diǎn),釋放節(jié)點(diǎn)的存儲(chǔ)空間
}
}
}
//逆序操作
//算法1:從第二個(gè)節(jié)點(diǎn)開始,記錄它的下一個(gè)節(jié)點(diǎn),然后依次挪到第一個(gè)節(jié)點(diǎn)之前成為新表頭
int inverse_node(struct node pH)
{
struct nodep = pH; //頭結(jié)點(diǎn)
struct node pPrev = NULL; //記錄前一個(gè)節(jié)點(diǎn)
struct nodepBack = NULL; //記錄下一個(gè)節(jié)點(diǎn)地址
struct node *pFirstNode = p->pNext; //記錄第一個(gè)節(jié)點(diǎn)
//節(jié)點(diǎn)只有1個(gè)或者無有效節(jié)點(diǎn)時(shí),返回原來的鏈表,不用進(jìn)行逆序操作
if((NULL == pFirstNode) | (NULL == pFirstNode->pNext))
{
return 0;
}
//節(jié)點(diǎn)數(shù)兩個(gè)或以上時(shí)
else
{
p = p->pNext; //節(jié)點(diǎn)1
pBack = p->pNext; //節(jié)點(diǎn)2
pPrev = p; //節(jié)點(diǎn)1
p = pBack; //節(jié)點(diǎn)2
while(NULL != p->pNext)
{
pBack = p->pNext; //節(jié)點(diǎn)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)
{
//頭指針指向頭結(jié)點(diǎn)
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;
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。