满意答案
218239安乐窝
2015.11.05
采纳率:46% 等级:5
已帮助:406人
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586#include #include #include typedef struct LinkNode { int data; struct LinkNode *pNext;}LinkNode,*PNode; //insert with positonbool insert( LinkNode *pHead, int data, int iPos){ LinkNode *p=NULL, *pTmp=NULL ; LinkNode* pNewNode = (LinkNode *)malloc(sizeof(LinkNode)); pNewNode->data = data; pNewNode->pNext = NULL; p = pHead; while ((iPos-1) && p->pNext){ p = p->pNext; iPos--; } if (p->pNext == NULL){ p->pNext = pNewNode; pNewNode->pNext = NULL; }else{ pTmp = p->pNext; p->pNext = pNewNode; pNewNode->pNext = pTmp; } return true;} //delete with positionbool deletefun(LinkNode *pHead, int iPos){ LinkNode *p = pHead, *pTemp=NULL; if (p->pNext ==NULL) return true; while ((iPos-1) && p->pNext->pNext) { p = p->pNext; iPos--; } pTemp = p->pNext; if (p->pNext->pNext != NULL){ p->pNext = p->pNext->pNext; free(pTemp); }else{ free(p->pNext); p->pNext = NULL; } return true;} void PrintLink(const LinkNode *pHead){ LinkNode *p = pHead->pNext; while (p) { printf("%d\t",p->data); p = p->pNext; } printf("\n");} int main(){ LinkNode obHead; obHead.pNext = NULL; insert(&obHead,10,1); insert(&obHead,100,1); insert(&obHead,1000,1); insert(&obHead,10000,1); insert(&obHead,100000,1); insert(&obHead,333,2); PrintLink(&obHead); deletefun(&obHead,2); PrintLink(&obHead); return 0;}追问: 运行有错误啊
追答: 我是在c++环境下编译的,又修改了一下,你再看看,如果有错误,发一下错误给我看看,我本地正常
00分享举报