- 论坛徽章:
- 0
|
急,急,在线求助有关链表删除。
很遗憾!有的事情还得靠自己呀.欢迎正在学习链表的朋友进来一看.特别是有链表困惑的朋友.
以下是一个从链表中删除某节点的程序.我以在linux下测试过.代码如下:
typedef struct outnode
{
char saddr[16];
double length;
struct outnode * next;
}linkoutlist;
/* 输出out链表 */
void printfoutlist(linkoutlist *head)
{
linkoutlist *p;
int n;
n = 0;
p = head;
if(head != NULL)
while( p != NULL)
{
n++;
printf("************printoutlist*************\n" ;
printf("source ip=%s length=%.3f.\n",p->;saddr,p->;length);
p = p->;next;
}
printf("*************************************\n" ;
printf("outlist have %d node.\n\n",n);
}
/* deleteoutlist: delete i node from the outlist */
linkoutlist * deletenodeoutlist(linkoutlist *head, int i)
{
linkoutlist *p,*q,*s;
linkoutlist *newhead;
int j;
int k;
int n;
k = 1;
p = head; /*从开始节点比较*/
if( head != NULL)
{
j = i-1;
printf("locate %d node from outlist.\n",j);
while( (p != NULL)&&(k < j))
{
p = p->;next;
k ++;
}
printf("j=%d,k=%d.\n",j,k);
if( j == 0) // 删除第1节点
{
printf("删除outlist第1节点.\n" ;
if(p->;next == NULL) //当第1节点后为空
{
printf("j=%d,outlist only 1 node.\n",j);
s = head;
free(s);
newhead = NULL;
}
else
{
printf("j=%d,outlist after 1 node is not NULL.\n",j);
s = head;
newhead = s->;next;
free(s);
q = newhead;
printfoutlist(q);
}
}
else //删除第2,3,...n节点
{
printf("删除outlist第2,3..n节点.\n" ;
deleteoutafter(p);
s = head;
printfoutlist(s);
newhead = head;
} |
|