1.創(chuàng)建鏈表結(jié)構(gòu)
typedef struct NODE{
int value;
struct NODE *pNext;
}Node;
2.新建節(jié)點(diǎn)
Node *createNode(int value)
{
Node *p = malloc(sizeof(Node));
p->value = value;
return p;
}
3.打印函數(shù)
void printNodeInfo(Node *head)
{
Node *pCur = head;
while (pCur) {
NSLog(@"當(dāng)前節(jié)點(diǎn)值:%d", pCur->value);
pCur = pCur->pNext;
}
}
4.main函數(shù)實(shí)現(xiàn)
//鏈表逆序
- (void)LBNX
{
Node *pHead = createNode(10);
Node *p1 = createNode(20);
pHead->pNext = p1;
Node *p2 = createNode(30);
p1->pNext = p2;
Node *p3 = createNode(40);
p2->pNext = p3;
Node *p4 = createNode(50);
p3->pNext = p4;
Node *p5 = createNode(60);
p4->pNext = p5;
p5->pNext = NULL;
printNodeInfo(pHead);
Node *cur = NULL;
Node *cur1 = NULL;
Node *cur2 = NULL;
cur = pHead;
cur1 = cur->pNext;
cur2 = cur1->pNext;
cur->pNext = NULL;
while (cur && cur1) {
cur1->pNext = cur;
cur = cur1;
if (cur2 == NULL)
break;
cur1 = cur2;
cur2 = cur2->pNext;
}
pHead = cur;
printNodeInfo(pHead);
}
5.打印結(jié)果
2019-11-14 20:31:44.959604+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:10
2019-11-14 20:31:44.959726+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:20
2019-11-14 20:31:44.959837+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:30
2019-11-14 20:31:44.959943+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:40
2019-11-14 20:31:44.960042+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:50
2019-11-14 20:31:44.960143+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:60
2019-11-14 20:32:29.903751+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:60
2019-11-14 20:32:29.903971+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:50
2019-11-14 20:32:29.904118+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:40
2019-11-14 20:32:29.904260+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:30
2019-11-14 20:32:29.904392+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:20
2019-11-14 20:32:29.904517+0800 逆波蘭數(shù)[1463:62294] 當(dāng)前節(jié)點(diǎn)值:10