链表

jieao111 2008-03-31 02:17:00
以前都是看别人的程序,,自己一些还真发现了不少问题,,下面的怎么回事,,用2008时说
head->next=NULL;有问题。。。。。。

换了v6编译通过,,运行问题

// 数据结构.cpp : 定义控制台应用程序的入口点。
//

#include<iostream>

using namespace std;
struct Node
{
int value;
Node* next;
};
void creatlist(Node *head)//
{
head=new Node;
head->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{

Node* p=new Node;
p->value=i;
p->next=head->next;
head->next=p;
}


}
int print(Node *head)
{
Node *p=head;
while(p)
{
p=p->next;
cout<<p->value<<endl;
}
return 0;
}



int main()
{

Node* head;
creatlist(head);
print(head);
return 0;
}

...全文
84 11 打赏 收藏 转发到动态 举报
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sungodyy 2008-03-31
  • 打赏
  • 举报
回复
#include<iostream>

using namespace std;

struct Node
{
int value;
Node* next;
};
void creatlist(int n)//
{
Node *m_pHead , m_pCurrent,m_pBack;

int i;
cout<<"请输入数字";
for(i = 0; i < N; i++) 
{
m_Hphead = new Node();
if(i == 0)
m_pCurrent = m_Hphead = m_pBack;
else
m_Hphead->next = m_pCurrent;
m_pCurrent->next = NULL;
m_Hphead = m_pCurrent;
}

}
完成创建链表;

houdongfeng 2008-03-31
  • 打赏
  • 举报
回复

struct Node
{
int value;
Node* next;
};
void creatlist(Node **head)//二级指针
{
*head=new Node;
(*head)->value = 0; //*head要加括号
(*head)->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{

Node* p=new Node;
p->value=i;
p->next=*head; //注意都是*head
*head=p; //是*head=p,不是(*head)->next = p,那就循环了
}


}
int print(Node *head)
{
Node *p=head;
while(p)
{

cout<<p->value<<endl; //这两行要交换一下位置
p=p->next;
}
return 0;
}



int main()
{

Node* head;
creatlist(&head);
print(head); //倒序输出
return 0;
}

ttkk_2007 2008-03-31
  • 打赏
  • 举报
回复

void del(Node **head)

{

cout<<"删除一个元素";
Node* q=*head;
Node* p=*head;
int i;

cin>>i;
if(p->value == i){
*head = p->next;
delete p;
}
else{
while(p && p->value != i){
q = p;
p = p->next;
}
if(p && p->value == i){
q->next = p->next;
delete p;
}
}

}

jieao111 2008-03-31
  • 打赏
  • 举报
回复
加了del又有错了~~~~,,郁闷
// 数据结构.cpp : 定义控制台应用程序的入口点。
//

#include<iostream>

using namespace std;
struct Node
{
int value;
Node* next;
};
void creatlist(Node **head)//
{
(*head)=new Node;
(*head)->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{

Node* p=new Node;
p->value=i;
p->next=(*head)->next;
(*head)->next=p;
}


}
int print(Node *head)
{
Node *p=head;
while(p)
{ cout<<p->value<<endl;
p=p->next;

}
return 0;
}
del(Node **head)

{

cout<<"删除一个元素";
Node* q;
Node* p=*head;
int i;
cin>>i;
while(p->value!=i)
{
q=p;
p=p->next;
}
q->next=p->next;
delete p;
return 0;
}


int main()
{

Node *head;
creatlist(&head);
print(head);
del(&head);
print(head);
return 0;
}


jieao111 2008-03-31
  • 打赏
  • 举报
回复
5楼说的饿很对,,不过
p->next=(*head)->next;
(*head)->next=p;
照样工作。。。


总结一下是*不如->的优先把。。。
jieao111 2008-03-31
  • 打赏
  • 举报
回复
// 数据结构.cpp : 定义控制台应用程序的入口点。
//


#include<iostream>

using namespace std;
struct Node
{
int value;
Node* next;
};
void creatlist(Node **head)//
{
*head=new Node;
*head->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{

Node* p=new Node;
p->value=i;
p->next=*head->next;
*head->next=p;
}


}
int print(Node **head)
{
Node *p=*head;
while(p)
{
p=p->next;
cout<<p->value<<endl;
}
return 0;
}



int _tmain(int argc, _TCHAR* argv[])
{

Node *head;
creatlist(&head);
print(&head);
return 0;
}



求一修改
ttkk_2007 2008-03-31
  • 打赏
  • 举报
回复

struct Node
{
int value;
Node* next;
};
void creatlist(Node **head)//二级指针
{
*head=new Node;
(*head)->value = 0; //*head要加括号
(*head)->next=NULL;
int i;
cout<<"请输入数字";
while(cin>>i)
{

Node* p=new Node;
p->value=i;
p->next=*head; //注意都是*head
*head=p; //是*head=p,不是(*head)->next = p,那就循环了
}


}
int print(Node *head)
{
Node *p=head;
while(p)
{

cout<<p->value<<endl; //这两行要交换一下位置
p=p->next;
}
return 0;
}



int main()
{

Node* head;
creatlist(&head);
print(head); //倒序输出
return 0;
}
csdn5211 2008-03-31
  • 打赏
  • 举报
回复
*head->next是什么道理啊,head->next还差不多。
jieao111 2008-03-31
  • 打赏
  • 举报
回复
是啊,,我也发现了,,改了以后是。。 *head->next=NULL;

错误: g:\数据结构.cpp(16) : error C2227: left of '->next' must point to class/struct/union
gl0312 2008-03-31
  • 打赏
  • 举报
回复
同意楼上意见
liyuzhu_1984 2008-03-31
  • 打赏
  • 举报
回复
很明显 你create()函数 没能实现create的功能 要么用引用 要么用void creatlist(Node **head)
相关推荐

63,594

社区成员

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