依然链表---------

赵Andy 2010-09-16 08:12:48

#include<iostream.h>
struct node
{
node *next;
int d;
};
typedef struct node Sqlist;
class chainlist
{
private:
node *head;
node *now;
public:
chainlist()
{
head=NULL;
now=NULL;
};
void init();
void Delete(int locate,int number);
void out();
void add(node *a);
};

void chainlist::init()
{
now=head;
};
void chainlist::add(node *a)
{
if(head==NULL)
head=a;
else
{
now->next=a;
a->next=NULL;
now=now->next;
}
};
void chainlist::out()
{
node *b;
b=head;
while(b!=NULL)
{
cout<<b->d<<endl;
b=b->next;
}
};
void chainlist::Delete(int locate,int number)
{
node *h,*temp;
temp=head;
for(int i=0;i!=locate;++i)
temp=temp->next;
for(int ii=0;ii!=number;++ii)
{
h=temp;
temp=temp->next;
h->next=temp->next;
temp=NULL;
}
};

#include<iostream.h>
#include"chainlist.h"
void main()
{
Sqlist *a=new Sqlist[10];
chainlist ch;
ch.init();
for(int num=0;num!=10;++num) //这里往下可能有问题
{
a->d=2;
a->next=NULL;
ch.add(a);

a=a->next;
}
ch.out();
delete [] a;
}
...全文
212 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

#define MAX_LINK_NUM 20000

typedef struct Node
{
string name;
struct Node *next;
}node;

//初始化一个链表,link_num为初始节点个数,返回头节点
node *init_link(int link_num)
{
node *head = NULL,*cur = NULL,*pre = NULL;
for(int i=0; i<link_num; i++)
{
cout<<"input the "<<i+1<<"th data:";
cur = new node;
cur->next = NULL;
if(i == 0)
{
head = cur;
}
if(pre != NULL)
{
pre->next = cur;
}
pre = cur;
cin>>cur->name;
}
return head;
}

//打印链表
void print_link(const node *head)
{
while(head)
{
cout<<head->name<<endl;
head = head->next;
}
}

//删除链表
void delete_link(node *head)
{
node *next = NULL;
while(head)
{
next = head->next;
delete head;
head = next;
}
}

//按key值删除某个指定的节点,并返回头节点给形参head
bool delete_node(node *&head,string key)
{
node *pre = NULL,*cur = NULL;
if(key.length() == 0)
{
return false;
}
if(head->name == key) //删除的是头节点,特殊处理
{
cur = head->next;
delete head;
head = cur;
return true;
}
pre = head;
cur = head->next;
while(cur)
{
if(cur->name == key) //查找命中
{
pre->next = cur->next;
delete cur;
return true;
}
pre = cur;
cur = cur->next;
}
return false;
}

//插入节点
bool insert_node(node *head,string data)
{
node *new_node = NULL;
while(head->next)
{
head = head->next;
}
new_node = new node;
if(new_node)
{
new_node->name = data;
new_node->next = NULL;
head->next = new_node;
return true;
}
return false;
}

int main()
{
int link_num = 0;
bool bRet = true;
string str;
node *head = NULL,*tail = NULL;
cout<<"Input link_num:";
cin>>link_num;
head = init_link(link_num);
print_link(head);
delete_link(head);
return 0;
}


以前写的,你参考下,就能对出问题了。
dxms8 2010-09-17
  • 打赏
  • 举报
回复
for(int num=0;num!=10;++num)
{
Sqlist *a=new Sqlist; //new出来的,用完需要delete
a->d=num;
a->next=NULL;
ch.add(a);
}
ch.out();
ch.destroy(); //添加一个函数用来delete链表中的Node指针
whg01 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 whg01 的回复:]

C/C++ code


for(int num=0;num!=10;++num)
{
Sqlist *a=new Sqlist;
a->d=num;
a->next=NULL;
ch.add(a);
delete [] a;
}
ch.out();
……
[/Quote]
晕了,不需要那个delete [] a;
dingshaofengbinbin 2010-09-16
  • 打赏
  • 举报
回复
顶了!!!!!!
whg01 2010-09-16
  • 打赏
  • 举报
回复


for(int num=0;num!=10;++num)
{
Sqlist *a=new Sqlist;
a->d=num;
a->next=NULL;
ch.add(a);
delete [] a;
}
ch.out();

改成这样,你之前的写法ch.add是把同一个a不停的往末尾加,从而导致a->next==a,形成了一个环。
赵Andy 2010-09-16
  • 打赏
  • 举报
回复
我改成这样了,但还是有问题,是在out里,无限输出最后一个数。

Sqlist *a=new Sqlist;
for(int num=0;num!=10;++num)
{
a->d=num;
a->next=NULL;
ch.add(a);
}
ch.out();
delete [] a;
dxms8 2010-09-16
  • 打赏
  • 举报
回复
void chainlist::add(node *a)没什么问题。
for(int num=0;num!=10;++num)
{
a->d=num;
a->next=NULL;
ch.add(a);
delete [] a;
}
你都delete[] a了,还循环。。。爆了吧。。
赵Andy 2010-09-16
  • 打赏
  • 举报
回复

void chainlist::add(node *a)
{
if(head==NULL)
{
head=a;
now=head;
}
else
{
now->next=a;
// a->next=NULL;
now=now->next;
}
};
for(int num=0;num!=10;++num)
{
a->d=num;
a->next=NULL;
ch.add(a);
delete [] a;
}

感觉这两个地方有点问题,请大侠指点。
赵Andy 2010-09-16
  • 打赏
  • 举报
回复

for(int num=0;num!=10;++num) //这里往下可能有问题
{
a->d=2;
a->next=NULL; //a->next是NULL
ch.add(a);

a=a->next; //a是NULL,当第二次循环时,a->d=2; 会出错。
}


怎么改,试一个不行。
dxms8 2010-09-16
  • 打赏
  • 举报
回复
void chainlist::add(node *a)
{
if(head==NULL)
{
head=a;
now = head;
}
else
{
now->next=a;
a->next=NULL;
now=now->next;
}
};
whg01 2010-09-16
  • 打赏
  • 举报
回复

for(int num=0;num!=10;++num) //这里往下可能有问题
{
a->d=2;
a->next=NULL; //a->next是NULL
ch.add(a);

a=a->next; //a是NULL,当第二次循环时,a->d=2; 会出错。
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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