双向循环链表删除节点问题

c_blog_zy 2016-01-05 05:11:26
看到过别人提到在结构体重嵌入链表比较方便,我试了一一下,发现删除节点的时候有些问题,请大家指教。


#include<stdio.h>
#include<malloc.h>

//根据结构体成员的地址找到结构体的起始地址
#define GetAddr(type,addr,member) \
((type *)((char *)addr-(unsigned long *)(&((type *)0)->member)))

//链表申明
typedef struct _list_head{
struct _list_head *prev;
struct _list_head *next;
}list_head,*Plist_head;

//结构体申明
typedef struct _MyStruct{
int age;
list_head list;
}mystruct;

//建立表头
Plist_head CreateList()
{
Plist_head head=NULL;
mystruct *p=(mystruct *)malloc(sizeof(mystruct));
p->age=0;
head=&p->list;
head->prev=head->next=head;
return head;
}

//创建节点
Plist_head CreateNode(int data)
{
Plist_head newnode=NULL;
mystruct *p=(mystruct *)malloc(sizeof(mystruct));
p->age=data;
newnode=&p->list;
newnode->next=newnode->prev=newnode;
return newnode;

}

//插入节点
Plist_head InsertNode(Plist_head head,int data)
{
Plist_head newNode=CreateNode(data);
//建立连接
if(newNode)
{
head->prev->next=newNode;
newNode->next=head;
head->prev=newNode;
newNode->next=head;
}
else
return NULL;
return head;

}

//遍历
Plist_head FindNode(Plist_head head,int data)
{
mystruct *fp;
Plist_head Fd=head;

for(;Fd->next!=head;Fd=Fd->next)
{
fp=GetAddr(mystruct,Fd,list);
if(fp->age==data)
break;
}
if(fp->age==data)
{
printf("find done!\n");
return Fd;
}
else
return NULL;
}

//删除节点
Plist_head DeleteNode(Plist_head head,int data)
{
mystruct *ms=NULL;
Plist_head dNode=FindNode(head,data);
Plist_head bNode=head;

if(dNode==head)
bNode=head->next;//当删除头结点时,把头的后继当表头。
if(dNode)
{
dNode->prev->next=dNode->next;
dNode->next->prev=dNode->prev;
ms=GetAddr(mystruct,dNode,list);

free(ms);//这里有问题,不知道怎么释放该节点以及包含该节点的结构体。
//free(dNode);
}
else
return NULL;
return bNode;
}

//计算链表长度
int CalcLength(Plist_head cNode)
{
int length=0;
Plist_head temp=cNode;
for(;temp->next!=cNode;temp=temp->next)
length++;
return (length+1);
}
//主函数
int main()
{
Plist_head p=NULL;
mystruct *ms=NULL;
Plist_head head=CreateList();//建立表头

if(InsertNode(head,20))//插入节点20
printf("Insert 20 successfully!\n");
else
printf("Insert 20 failed!\n");

if(InsertNode(head,21))//插入节点21
printf("Insert 21 successfully!\n");
else
printf("Insert 21 failed!\n");

if(InsertNode(head,22))//插入节点22
printf("Insert 22 successfully!\n");
else
printf("Insert 22 failed!\n");

printf("Node length=%d\n",CalcLength(head));//打印长度

//打印链表
for(p=head;p->next!=head;p=p->next)
{
ms=GetAddr(mystruct,p,list);
printf("%d node!\n",ms->age);
}
printf("%d node!\n",GetAddr(mystruct,p,list)->age);

//删除节点21
if(DeleteNode(head,21))
printf("Delete 21 successfully!\n");
else
printf("Delete 21 failed!\n");
// printf("Node length=%d\n",CalcLength(head));

//打印节点,这里有问题,节点22打出来是错的
for(p=head;p->next!=head;p=p->next)
{
ms=GetAddr(mystruct,p,list);
printf("%d node!\n",ms->age);
}
printf("%d node!\n",GetAddr(mystruct,p,list)->age);
return 0;
}
...全文
921 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
c_blog_zy 2016-01-08
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
看代码中★的地方:
#include<stdio.h>
#include<malloc.h>

//根据结构体成员的地址找到结构体的起始地址
#define GetAddr(type,addr,member) \
((type *)((char *)addr-(char *)(&((type *)0)->member)))

//链表申明
typedef struct _list_head{
struct _list_head *prev;
struct _list_head *next;
}list_head,*Plist_head;

//结构体申明
typedef struct _MyStruct{
int age;
list_head list;
}mystruct;

//建立表头
Plist_head CreateList()
{
    Plist_head head=NULL;
    mystruct *p=(mystruct *)malloc(sizeof(mystruct));
    p->age=0;
    head=&p->list;
    head->prev=head->next=head;
    return head;
}

//创建节点
Plist_head CreateNode(int data)
{
    Plist_head newnode=NULL;
    mystruct *p=(mystruct *)malloc(sizeof(mystruct));
    p->age=data;
    newnode=&p->list;
    newnode->next=newnode->prev=newnode;
    return newnode;

}

//插入节点
Plist_head InsertNode(Plist_head head,int data)
{
    Plist_head newNode=CreateNode(data);
    //建立连接
    if(newNode)
    {
        head->prev->next=newNode;
        newNode->next=head;
        newNode->prev=head->prev;//★
        head->prev=newNode;//★
    }
    else
        return NULL;
    return head;

}

//遍历
Plist_head FindNode(Plist_head head,int data)
{
    mystruct *fp;
    Plist_head Fd=head;

    for(;Fd->next!=head;Fd=Fd->next)
    {
        fp=GetAddr(mystruct,Fd,list);
        if(fp->age==data)
            break;
    }
    if(fp->age==data)
    {
        printf("find done!\n");
        return Fd;
    }
    else
        return NULL;
}

//删除节点
Plist_head DeleteNode(Plist_head head,int data)
{
        mystruct *ms=NULL;
        Plist_head dNode=FindNode(head,data);
        Plist_head bNode=head;

        if(dNode==head)
            bNode=head->next;//当删除头结点时,把头的后继当表头。
        if(dNode)
        {
            dNode->prev->next=dNode->next;
            dNode->next->prev=dNode->prev;
            ms=GetAddr(mystruct,dNode,list);

            free(ms);//这里有问题,不知道怎么释放该节点以及包含该节点的结构体。
            //free(dNode);
        }
        else
            return NULL;
        return bNode;
}

//计算链表长度
int CalcLength(Plist_head cNode)
{
    int length=0;
    Plist_head temp=cNode;
    for(;temp->next!=cNode;temp=temp->next)
        length++;
    return (length+1);
}
//主函数
int main()
{
    Plist_head p=NULL;
    mystruct *ms=NULL;
    Plist_head head=CreateList();//建立表头

    if(InsertNode(head,20))//插入节点20
        printf("Insert 20 successfully!\n");
    else
        printf("Insert 20 failed!\n");

    if(InsertNode(head,21))//插入节点21
        printf("Insert 21 successfully!\n");
    else
        printf("Insert 21 failed!\n");

    if(InsertNode(head,22))//插入节点22
        printf("Insert 22 successfully!\n");
    else
        printf("Insert 22 failed!\n");

    printf("Node length=%d\n",CalcLength(head));//打印长度

    //打印链表
    for(p=head;p->next!=head;p=p->next)
    {
        ms=GetAddr(mystruct,p,list);
        printf("%d node!\n",ms->age);
    }
    printf("%d node!\n",GetAddr(mystruct,p,list)->age);

    //删除节点21
    if(DeleteNode(head,21))
        printf("Delete 21 successfully!\n");
    else
        printf("Delete 21 failed!\n");
//  printf("Node length=%d\n",CalcLength(head));

    //打印节点,这里有问题,节点22打出来是错的
    for(p=head;p->next!=head;p=p->next)
    {
        ms=GetAddr(mystruct,p,list);
        printf("%d node!\n",ms->age);
    }
    printf("%d node!\n",GetAddr(mystruct,p,list)->age);
    return 0;
}
解决了,谢谢
赵4老师 2016-01-07
  • 打赏
  • 举报
回复
看代码中★的地方:
#include<stdio.h>
#include<malloc.h>

//根据结构体成员的地址找到结构体的起始地址
#define GetAddr(type,addr,member) \
((type *)((char *)addr-(char *)(&((type *)0)->member)))

//链表申明
typedef struct _list_head{
struct _list_head *prev;
struct _list_head *next;
}list_head,*Plist_head;

//结构体申明
typedef struct _MyStruct{
int age;
list_head list;
}mystruct;

//建立表头
Plist_head CreateList()
{
    Plist_head head=NULL;
    mystruct *p=(mystruct *)malloc(sizeof(mystruct));
    p->age=0;
    head=&p->list;
    head->prev=head->next=head;
    return head;
}

//创建节点
Plist_head CreateNode(int data)
{
    Plist_head newnode=NULL;
    mystruct *p=(mystruct *)malloc(sizeof(mystruct));
    p->age=data;
    newnode=&p->list;
    newnode->next=newnode->prev=newnode;
    return newnode;

}

//插入节点
Plist_head InsertNode(Plist_head head,int data)
{
    Plist_head newNode=CreateNode(data);
    //建立连接
    if(newNode)
    {
        head->prev->next=newNode;
        newNode->next=head;
        newNode->prev=head->prev;//★
        head->prev=newNode;//★
    }
    else
        return NULL;
    return head;

}

//遍历
Plist_head FindNode(Plist_head head,int data)
{
    mystruct *fp;
    Plist_head Fd=head;

    for(;Fd->next!=head;Fd=Fd->next)
    {
        fp=GetAddr(mystruct,Fd,list);
        if(fp->age==data)
            break;
    }
    if(fp->age==data)
    {
        printf("find done!\n");
        return Fd;
    }
    else
        return NULL;
}

//删除节点
Plist_head DeleteNode(Plist_head head,int data)
{
        mystruct *ms=NULL;
        Plist_head dNode=FindNode(head,data);
        Plist_head bNode=head;

        if(dNode==head)
            bNode=head->next;//当删除头结点时,把头的后继当表头。
        if(dNode)
        {
            dNode->prev->next=dNode->next;
            dNode->next->prev=dNode->prev;
            ms=GetAddr(mystruct,dNode,list);

            free(ms);//这里有问题,不知道怎么释放该节点以及包含该节点的结构体。
            //free(dNode);
        }
        else
            return NULL;
        return bNode;
}

//计算链表长度
int CalcLength(Plist_head cNode)
{
    int length=0;
    Plist_head temp=cNode;
    for(;temp->next!=cNode;temp=temp->next)
        length++;
    return (length+1);
}
//主函数
int main()
{
    Plist_head p=NULL;
    mystruct *ms=NULL;
    Plist_head head=CreateList();//建立表头

    if(InsertNode(head,20))//插入节点20
        printf("Insert 20 successfully!\n");
    else
        printf("Insert 20 failed!\n");

    if(InsertNode(head,21))//插入节点21
        printf("Insert 21 successfully!\n");
    else
        printf("Insert 21 failed!\n");

    if(InsertNode(head,22))//插入节点22
        printf("Insert 22 successfully!\n");
    else
        printf("Insert 22 failed!\n");

    printf("Node length=%d\n",CalcLength(head));//打印长度

    //打印链表
    for(p=head;p->next!=head;p=p->next)
    {
        ms=GetAddr(mystruct,p,list);
        printf("%d node!\n",ms->age);
    }
    printf("%d node!\n",GetAddr(mystruct,p,list)->age);

    //删除节点21
    if(DeleteNode(head,21))
        printf("Delete 21 successfully!\n");
    else
        printf("Delete 21 failed!\n");
//  printf("Node length=%d\n",CalcLength(head));

    //打印节点,这里有问题,节点22打出来是错的
    for(p=head;p->next!=head;p=p->next)
    {
        ms=GetAddr(mystruct,p,list);
        printf("%d node!\n",ms->age);
    }
    printf("%d node!\n",GetAddr(mystruct,p,list)->age);
    return 0;
}
赵4老师 2016-01-06
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。 仅供参考:
//不带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <locale.h>
struct NODE {
    int          data;
    struct NODE *next;
} *head,*p,*q,*s,*p1,*p2,*q1,**ta;
int i,k,n,t,m,v,N=10;
int main() {
    setlocale(LC_ALL,"chs");
    srand(time(NULL));

    head=NULL;

    printf("创建%d个节点的单链表:",N);//创建N个节点的单链表
    p=head;
    for (i=0;i<N;i++) {
        q=(struct NODE *)malloc(sizeof(struct NODE));
        if (NULL==q) exit(1);
        q->data=rand()%100;//填写0..99的随机值
        q->next=NULL;
        if (NULL==p) {
            head=q;
            p=head;
        } else {
            p->next=q;
            p=q;
        }
    }

    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    k=3;
    v=5;
    printf("将值为%d的结点插入到单链表的第%d个结点前:",v,k);//将值为v的结点插入到单链表的第k个结点前
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==1) {
            q=(struct NODE *)malloc(sizeof(struct NODE));
            if (NULL==q) exit(1);
            q->data=5;
            q->next=head;
            head=q;
            break;
        } else {
            if (k-1==n) {
                q=(struct NODE *)malloc(sizeof(struct NODE));
                if (NULL==q) exit(1);
                q->data=5;
                q->next=p->next;
                p->next=q;
                break;
            }
        }
        p=p->next;
    }

    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    k=5;
    printf("删除第%d个节点:",k);//删除第k个节点
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==1) {
            q=head;
            head=head->next;
            free(q);
            break;
        } else {
            if (k-1==n) {
                q=p->next;
                if (q) {
                    p->next=q->next;
                    free(q);
                }
                break;
            }
        }
        p=p->next;
    }

    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    printf("从小到大排序:");//从小到大排序
    for (p=head,p1=NULL;p!=NULL;p1=p,p=p->next) {
        for (q=p->next,q1=p;q!=NULL;q1=q,q=q->next) {
            if (p->data > q->data) {

                //交换data
//              printf("swap %02d %02d\n",p->data,q->data);
//              t=p->data;p->data=q->data;q->data=t;

                //或者

                //交换next
//              printf("swap %02d %02d\n",p->data,q->data);
                if (p==head) {//p是头
                    if (p->next==q) {//pq挨着
                        head=q;
                        p->next=q->next;
                        q->next=p;
                        q=p;
                        p=head;
                    } else {//pq不挨着
                        head=q;
                        p2=p->next;
                        p->next=q->next;
                        q->next=p2;
                        q1->next=p;
                        q=p;
                        p=head;
                    }
                } else {//p不是头
                    if (p->next==q) {//pq挨着
                        p1->next=q;
                        p->next=q->next;
                        q->next=p;
                        q=p;
                        p=p1->next;
                    } else {//pq不挨着
                        p1->next=q;
                        p2=p->next;
                        p->next=q->next;
                        q->next=p2;
                        q1->next=p;
                        q=p;
                        p=p1->next;
                    }
                }

                //输出整个单链表
//              s=head;
//              while (1) {
//                  if (NULL==s) {
//                      printf("\n");
//                      break;
//                  }
//                  printf("%02d->",s->data);
//                  s=s->next;
//              }
//              getchar();
            }
        }
    }

    //输出整个单链表并计算链表长度n
    n=0;
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        n++;
        s=s->next;
    }

    printf("将整个链表逆序:");//将整个链表逆序
    if (n>=2) {
        p=head;
        q=p->next;
        p->next=NULL;
        while (1) {
            q1=q->next;
            q->next=p;
            p=q;
            q=q1;
            if (NULL==q) break;
        }
        head=p;
    }

    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    m=4;
    n=6;
    printf("将单链表中前%d个结点和后%d个结点进行互换:",m,n);//将单链表中前m个结点和后n个结点进行互换,m+n为链表总长
    k=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        k++;
        if (m==k) {
            q=p;
        }
        s=p;
        p=p->next;
    }
    q1=head;
    head=q->next;
    s->next=q1;
    q->next=NULL;

    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //释放所有节点
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        q=p->next;
        free(p);
        p=q;
    }

    return 0;
}
//创建10个节点的单链表:08->74->07->23->03->99->31->56->88->16->
//将值为5的结点插入到单链表的第3个结点前:08->74->05->07->23->03->99->31->56->88->16->
//删除第5个节点:08->74->05->07->03->99->31->56->88->16->
//从小到大排序:03->05->07->08->16->31->56->74->88->99->
//将整个链表逆序:99->88->74->56->31->16->08->07->05->03->
//将单链表中前4个结点和后6个结点进行互换:31->16->08->07->05->03->99->88->74->56->
//
c_blog_zy 2016-01-06
  • 打赏
  • 举报
回复
木人
c_blog_zy 2016-01-06
  • 打赏
  • 举报
回复
能不能举着类似我问题的例子

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧