创建链表head指向哪里?

小楼东风细雨 2009-05-14 08:49:11
Node *head,*pNew;
pNew=new Node;
cin>>pNew->data;
int m=0;
if(NULL==head)
{
head=pNew;//在创建头指针时是这样写的 那么head的next域到底指向哪里呢?
pCur=pNew;
m++;
.........
}
...全文
233 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
小楼东风细雨 2009-05-14
  • 打赏
  • 举报
回复
本人是新人 分数少 所以就平均分点吧 呵呵 谢谢大家哦
我把我经过大家点拨后调通的源程序贴来 大家看看。。。。。。帮我想想怎么改进
//双向链表学生管理系统
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std;
struct Node
{
int data;
Node *next;
Node *prev;
char name[100];
};
Node *Bghead=NULL;//定义全局head!

void Return();
void Creat();
void OutList();
void Query();
void Slect();
void Input();
void Dele();


int main()
{
Slect();
return 0;
}


void Creat()
{
int n;
cout<<"输入学生个数 : ";
cin>>n;
Node *head;
head=Bghead;

Node *pNew,*pCur;
pCur=head;
if(n<1) {Bghead=head;}
pNew=new Node;
cout<<"输入学号 ";
cout<<"姓名: ";
cin>>pNew->data;
if(cin.fail())
{
cout<<"输入非法数据!";
cout<<"程序退出!\n";
exit(0);
}
cin>>pNew->name;
int m=0;
while(1)
{
if(NULL==head)
{
head=pNew;
pCur=head;
m++;
}
else
{
pNew->prev=pCur;
pCur->next=pNew;
pCur=pNew;
m++;
}

if(m>=n) break;

cout<<"输入学号 ";
cout<<"姓名: ";
pNew=new Node;
cin>>pNew->data>>pNew->name;
if(cin.fail())
{
cout<<"输入非法数据! 程序退出!\n";
exit(0);
}
}
//让head和链表独立

head->prev=pCur;
pCur->next=head;
Bghead=head;
Return();
}

void OutList()
{

while(1){
if(Bghead==NULL)
{
cout<<"没有任何学生信息"<<endl;
Return();
break;
}
Node *pCur=new Node;
pCur=Bghead;
cout<<"学号 :"<<pCur->data<<" 姓名 :"<<pCur->name<<endl;
if(pCur->next==NULL) { Return() ;break; }
pCur=pCur->next;
while(pCur!=Bghead)
{
cout<<"学号 :"<<pCur->data;
cout<<" 姓名 :"<<pCur->name<<endl;
pCur=pCur->next;
}
Return();
break;
}
}

void Query()
{

bool f=true;
int data;
Node *p;
p=Bghead;
cout<<"输入查询的学号"<<endl;

cin>>data;
cout<<"寻找 "<<data<<" 开始!"<<endl;
while(1)
{
if(p->data==data)
{
cout<<"姓名 "<<p->name<<endl;
cout<<"找到 "<<data<<" !"<<endl;
break;
}
if(p->prev==p) { cout<<"您寻找的学生信息不存在!"<<endl;break; }
p=p->next;
p->prev=p->prev->prev;
}
Return();
}

void Slect()
{
cout<<"-------◇--------☆---------欢迎进入学生管理系统!--------★---------■--------"<<endl;
cout<<endl;
cout<<"|---------------------------------请选择操作-----------------------------------|\n"
<<"|----------------------------1->输入学生信息 >------------------------------|\n"
<<"|----------------------------2->添加新的学生 >------------------------------|\n"
<<"|----------------------------3->根据学号查询学生>------------------------------|\n"
<<"|----------------------------4->删除指定学生信息>------------------------------|\n"
<<"|----------------------------5->显示全部学生信息>------------------------------|\n";
int key;
cin>>key;
switch(key)
{
case 1:
Creat();break;
case 2:
Input();break;
case 3:
Query();break;
case 4:
Dele();break;
case 5:
OutList();break;
}

}

void Return()
{
cout<<"1->返回主菜单 2->退出"<<endl;
int a;
cin>>a;
if(a==1) Slect();
else cout<<"退出成功!欢迎下次使用!"<<endl;
}

void Input()
{
Node *head=new Node;
head=Bghead;
Node *pNew=new Node;
Node *pCur=new Node;

while(1)
{
cout<<"输入学生学号_";
cout<<"姓名_";

cin>>pNew->data;
if(cin.fail())
{
cout<<"输入错误!"<<endl;
Slect();
break;
}
else
{
cin>>pNew->name;
cout<<endl;
cout<<"添加成功!O(∩_∩)O"<<endl;

if(head==NULL)
{
head=pNew;
pCur=head;

head->next=NULL;
head->prev=head;
Bghead=head;
Return();
break;
}
pCur=head->prev;//尾指针
pCur->next=pNew;
pNew->prev=pCur;
pCur=pNew;
pCur->next=head;
head->prev=pCur;
Bghead=head;
Return();
break;
}
}

}
void Dele()
{

bool f=true;
int data;
Node *p;
p=Bghead;
cout<<"B h测试="<<p->name<<endl;
cout<<"输入删除的学生学号"<<endl;

cin>>data;
cout<<"寻找 "<<data<<" 开始!"<<endl;
while(1)
{
if(p->data==data)
{
if(Bghead->next==Bghead)//删除到最后只剩下头指针
{
Bghead=NULL;cout<<"Bghead=NULL";

cout<<"删除成功!"<<endl;
break;
}

cout<<"姓名 "<<p->name<<endl;
cout<<"找到 "<<data<<" !"<<endl;
p->prev->next=p->next;
p->next->prev=p->prev;

Bghead=p->next;
cout<<"删除成功!"<<endl;
break;
}


if(p==Bghead->prev)
{
cout<<"您寻找的学生信息不存在!"<<endl;
break;
}
p=p->next;

}
Return();
}

  • 打赏
  • 举报
回复
if(NULL==head)
{
head=pNew;//在创建头指针时是这样写的 那么head的next域到底指向哪里呢? //head只指向了新开的节点,新开节点的next指向NULL啊
pCur=pNew;
m++;
.........
}
liuka 2009-05-14
  • 打赏
  • 举报
回复
大概这样

class list
{
public:
list(){head = new Node;head->next = NULL}
void create()
{
Node *p,*pNew;
p = head->next;
int nV;
while(cin>>nV)
{
pNew = new Node;
pNew->value = nV;
pNew = p;
p = pNew->next;
}
}


private:
Node *head;
}
hiboys 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 enmb 的回复:]
head总指向第一个,如果没有就指向空。
最后一个的next总指向空。
[/Quote]

pnew的构造应该将其next=null,然后head.next=pnew
really3353 2009-05-14
  • 打赏
  • 举报
回复
struct Node
{
int element;
Node *next;
};

Node *phead;
phead->next = NULL;

创建的时候使head的next指针指向空就行了
xiao0915 2009-05-14
  • 打赏
  • 举报
回复
null
头接点不是第一个接点
woshichangjiu 2009-05-14
  • 打赏
  • 举报
回复
头指针指向第一个结点,它的next指向NULL
enmb 2009-05-14
  • 打赏
  • 举报
回复
head总指向第一个,如果没有就指向空。
最后一个的next总指向空。
Paradin 2009-05-14
  • 打赏
  • 举报
回复
NULL
coverallwangp 2009-05-14
  • 打赏
  • 举报
回复
Node *head,*pNew;
pNew=new Node;
cin>>pNew->data;
int m=0;
if(NULL==head)
{
head=pNew;//在创建头指针时是这样写的 那么head的next域到底指向哪里呢?
head->next = NULL;
pCur=pNew;
m++;
.........
}
else
{
pCur -> next = pNew;
pCur = pNew;
.....
}
sagegz 2009-05-14
  • 打赏
  • 举报
回复

head->next = null;

65,211

社区成员

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

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