c++中关于双向链表的问题

雨于鱼 2018-04-29 09:03:47
#ifndef _DoubleList_h_
#define _DoubleList_h_

#include <stdio.h>
#include <stdlib.h>
typedef struct DNode//节点的定义
{
int x;
struct DNode *prior,*next;
}DNode;

typedef struct Header//头节点的定义
{
int length;
DNode *next;
}Header;

Header Init_List()//初始化一个双向链表
{
Header header;
header.length = 0;
header.next = NULL;
return header;
}
Header ListInsert(Header &header,int i,DNode &e)//在位置i插入指定元素e
{
if(i>header.length)
{
printf("please input suit num");
}
DNode *current=(DNode*)&header;
int j = 0;
if (header.next == NULL)
{
header.next=&e;
e.next =NULL;
e.prior=NULL;
header.length ++;
return header;
}
else if (i == header.length)
{
e.next = NULL;
e.prior = current;
current->next = &e;
header.length ++;
return header;
}
else
{
for (j=0;j<i;i++)
{
current=current->next;
}
e= *(current->next->prior);
e.next = current->next ;
*(current->next)= e;
e.prior = current;
header.length++;
return header;
}
}
#endif

int main()
{
DNode n1,n2,n3,n4;
n1.x = 1;
n2.x = 2;
n3.x = 3;
Header N;
N=Init_List();

N=ListInsert(N,0,n1);
printf("%d\n",N.next->x);

N=ListInsert(N,1,n2);
printf("%d",N.next->next->x);

N=ListInsert(N,1,n3);
printf("%d",N.next->next->next->next );
}
在打印第二个数时出现Program received signal SIGSEGV, Segmentation fault求教

...全文
960 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
JackyRao 2018-07-06
  • 打赏
  • 举报
回复
By the way, Why not use class in c plus plus?
JackyRao 2018-07-06
  • 打赏
  • 举报
回复
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.cc
* Copyright (C) 2018 JackyRauo <jackyrauo@jackyrauo-LIFEBOOK-AH555>
*
* foobar-cpp is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* foobar-cpp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef _DoubleList_h_
#define _DoubleList_h_

#include <stdio.h>
#include <stdlib.h>
typedef struct DNode//节点的定义
{
int x;
struct DNode *prior,*next;
}DNode;

typedef struct Header//头节点的定义
{
int length;
DNode *next;
}Header;

Header *Init_List()//初始化一个双向链表
{
Header *pheader = new Header();
pheader->length = 0;
pheader->next = NULL;
return pheader;
}
Header *ListInsert(Header *pheader,int i,int ele)//在位置i插入指定元素e
{
if(i>pheader->length)
{
printf("please input suit num");

}
else
{
DNode*pNewNode = new DNode();
pNewNode->next = NULL;
pNewNode->prior = NULL;
pNewNode->x = ele;

if (pheader->next == NULL)
{
pheader->next=pNewNode;
pheader->length ++;
return pheader;
}
else
{
DNode *current=pheader->next;

for (int j=0;j<i-1;i++)
{
current=current->next;
}
pNewNode->next =current->next;
pNewNode->prior = current;
current->next = pNewNode;
pheader->length++;
}
}
}

void Destroy(Header *pHeader)
{
if (pHeader == NULL)
return ;
while(pHeader->next!=NULL)
{
DNode *pTemp = pHeader->next;
pHeader->next = pTemp->next;
delete pTemp;
}
delete pHeader;
}
#endif

int main()
{

Header* pHeader;
pHeader=Init_List();

pHeader=ListInsert(pHeader,0,1);
printf("%d\n",pHeader->next->x);

pHeader=ListInsert(pHeader,1,2);

printf("%d\n",pHeader->next->next->x);

pHeader=ListInsert(pHeader,1,3);
printf("%d\n",pHeader->next->next->x );
Destroy(pHeader);
}

赵4老师 2018-04-30
  • 打赏
  • 举报
回复
仅供参考:
//不带表头结点的双向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <locale.h>
struct NODE {
    int          data;
    struct NODE *next;
    struct NODE *prev;
} *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;
        q->prev=NULL;
        if (NULL==p) {
            head=q;
            p=head;
        } else {
            p->next=q;
            q->prev=p;
            p=q;
        }
    }

    //输出整个双向链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d<->",s->data);
        if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}
        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=v;
            q->next=head;
            q->prev=NULL;
            head->prev=q;
            head=q;
            break;
        } else {
            if (k-1==n) {
                q=(struct NODE *)malloc(sizeof(struct NODE));
                if (NULL==q) exit(1);
                q->data=v;
                q->next=p->next;
                q->prev=p;
                p->next->prev=q;
                p->next=q;
                break;
            }
        }
        p=p->next;
    }

    //输出整个双向链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d<->",s->data);
        if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}
        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;
            head->prev=NULL;
            free(q);
            break;
        } else {
            if (k-1==n) {
                q=p->next;
                if (q) {
                    p->next=q->next;
                    if (q->next) q->next->prev=p;
                    free(q);
                }
                break;
            }
        }
        p=p->next;
    }

    //输出整个双向链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d<->",s->data);
        if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}
        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;

                //或者

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

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

    //输出整个双向链表并计算链表长度n
    n=0;
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d<->",s->data);
        if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}
        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->prev=q;
            p=q;
            q=q1;
            if (NULL==q) break;
        }
        head=p;
        head->prev=NULL;
    }

    //输出整个双向链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d<->",s->data);
        if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}
        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;
    head->prev=NULL;
    s->next=q1;
    q1->prev=s;
    q->next=NULL;

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

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

    return 0;
}
//创建10个节点的双向链表:93<->03<->60<->05<->57<->71<->02<->41<->26<->85<->
//将值为5的结点插入到双向链表的第3个结点前:93<->03<->05<->60<->05<->57<->71<->02<->41<->26<->85<->
//删除第5个节点:93<->03<->05<->60<->57<->71<->02<->41<->26<->85<->
//从小到大排序:02<->03<->05<->26<->41<->57<->60<->71<->85<->93<->
//将整个链表逆序:93<->85<->71<->60<->57<->41<->26<->05<->03<->02<->
//将双向链表中前4个结点和后6个结点进行互换:57<->41<->26<->05<->03<->02<->93<->85<->71<->60<->
//
真相重于对错 2018-04-29
  • 打赏
  • 举报
回复
引用 2 楼 qq_41003024 的回复:
还有就是指针函数能不能返回的是函数内部的指针变量,求教
可以 例如 int* get() { int* a=new int; *a=100; return a; } void main() { int *b = get(); printf("%d",*b); delete b; } 请区分什么变量在堆上,什么变量在栈上,什么是堆,什么是栈?如果不知道的话,这种问题可以问百度
Isnis-fallen 2018-04-29
  • 打赏
  • 举报
回复
如果返回函数内部的指针变量,系统回收后,指针指向的内容就无意义了
雨于鱼 2018-04-29
  • 打赏
  • 举报
回复
还有就是指针函数能不能返回的是函数内部的指针变量,求教
雨于鱼 2018-04-29
  • 打赏
  • 举报
回复
这个问题烦大概2,3天了。求各位大佬给解决一下
雨于鱼 2018-04-29
  • 打赏
  • 举报
回复
还有能不能将自己用struct定义的两个类型互相转换
雨于鱼 2018-04-29
  • 打赏
  • 举报
回复
我想问问各位大佬,我printf("%d\n",N.next->x);可以吗?刚出错就是这一句

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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