单向链表 尾部不能插入,请说明原因
// printer_test.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
typedef struct tag_gpio_elem
{
unsigned int nNum;
unsigned int nAddr;
unsigned int nPins;
struct tag_gpio_elem *pNext;
}GPIO_ELEM, *PGPIO_ELEM;
GPIO_ELEM *gl_pGPIOList = 0;
GPIO_ELEM *GPIOElem_New(unsigned int nNum, unsigned int nAddr, unsigned int nPinCount)
{
PGPIO_ELEM pNew;
pNew = (PGPIO_ELEM)malloc(sizeof(GPIO_ELEM));
if (!pNew)
return 0;
pNew->nNum = nNum;
pNew->nAddr = nAddr;
pNew->nPins = nPinCount;
pNew->pNext = 0;
return pNew;
}
void GPIOElem_Insert(GPIO_ELEM *pNew)
{
GPIO_ELEM *pPre, *pNow;
if (!pNew)
return;
if (!gl_pGPIOList)
{
gl_pGPIOList = pNew;
return;
}
pPre = gl_pGPIOList;
pNow = pPre->pNext;
if (!pNow) {
pNow = pNew;
return;
}
while (pNow)
{
pNow = pPre->pNext;
}
pPre = pNew;
}
int main()
{
for (int i = 0; i < 4; i++) {
GPIO_ELEM *GPIO;
GPIO = GPIOElem_New(i + 4, i + 5, i + 6);
GPIOElem_Insert(GPIO);
}
GPIO_ELEM *GPIO,*gpio_next;
GPIO = gl_pGPIOList;
gpio_next = GPIO->pNext;
while (gpio_next) {
printf("%d %d %d \n", gpio_next->nAddr, gpio_next->nNum, gpio_next->nPins);
gpio_next = gpio_next->pNext;
}
getchar();
return 0;
}