当前位置: 代码迷 >> C语言 >> [求助]进来讨论一下指针赋值问题!
  详细解决方案

[求助]进来讨论一下指针赋值问题!

热度:327   发布时间:2006-07-15 10:58:10.0
[求助]进来讨论一下指针赋值问题!

#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
const int namelength=20;
const int phonelength=20;
struct employee//结构体
{
char name[namelength];
char num[phonelength];
long ID;
struct* next;
};

struct employee* creat(void);//创建链表
struct employee* insert(struct employee*);//插入链表
void show(struct employee*);//显示链表
struct employee* modify(struct employee*);//修改链表
struct employee* DELETE(struct employee*);//删除链表
//函数声明


void main()//主函数
{long id;
char answer;
struct employee* head=NULL;
cout<<"A.creat an initial linked list of names and phone numbers"<<endl;//界面窗口
cout<<"B.insert a new structure into the linked list"<<endl;
cout<<"C.modify an existing structure in the linked list"<<endl;
cout<<"D.delete an existing structure from the list"<<endl;
cout<<"E.exit from the program"<<endl;

do
{

cout<<"ENTER YOUR CHOICE"<<endl;

cin>>answer;

switch(answer)

{


case 'A':
head=creat();
break;
case 'B':
head=insert(head);
break;
case 'C':
head=modify(head);
break;
case 'D':
cout<<"please enter the id of which you want to delete"<<endl;
cin>>id;
head=DELETE(head);
break;
case 'E':
system("cls");

break;
default:
break;

}

show(head);
cout<<"continue?Y/N"<<endl;

}while(answer=='y'||answer=='Y');


}


struct employee* creat(void)
{
int n=0;
struct employee *head,*p1,*p2;
p1=p2=new employee;
head=NULL;
cout<<"enter name number and ID:"<<endl;
cin>>p1->name>>p1->num>>p1->ID;
while(p1->ID)
{n++;
if(n==1)
{
head=p1;
p1->next=NULL;
}
else
p2->next=p1;
p2=p1;

p1=new employee;
cout<<"enter name number and ID:"<<endl;
cin>>p1->name>>p1->num>>p1->ID;

}
p2->next=NULL;
cout<<"the new linked list created successfully!"<<endl;
return(head);
}

struct employee* insert(struct employee* head)
{
struct employee newlist;
cout<<"please initial the new list(name number ID)"<<endl;
cin>>newlist.name>>newlist.num>>newlist.ID;
newlist.next=NULL;

struct employee *p0=&newlist,*p1,*p2;
p1=head;

if(head==NULL)
{head=p0;
p0->next=NULL;
}
else
while(p1->next!=NULL&&p1->ID<p0->ID)
{p2=p1;
p1=p1->next;
}
if(p1->ID<p0->ID)
{p1->next=p0;
p0->next=NULL;
}
else
{p2->next=p0;
p0->next=p1;
}
cout<<"insert successfully!"<<endl;
return(head);
}

void show(struct employee* head)
{
cout<<"date below"<<endl;
cout<<"******************************************************"<<endl;

while(head->next)
{cout<<head->name<<" "<<head->num<<" "<<head->ID<<endl;
head=head->next;
}
}
struct employee* modify(struct employee*head)
{
struct employee* p;
int temp;
long id;
cout<<"employee in which ID you want to modify?"<<endl;
cin>>id;
while(p->next!=NULL&&p->ID!=id)
p=p->next;
if(p->ID=id)
{
cout<<"which one do you want to madify 1.name 2.num?"<<endl;

cin>>temp;
if(temp=1)
cin.getline(p->name,namelength);
else if(temp=2)
cin.getline(p->num,phonelength);

}
else
cout<<"fail to modify!"<<endl;
return(head);
}


struct employee* DELETE(struct employee*head)
{
struct employee *p1,*p2;
p1=p2=head;
long id;
cout<<"which one do you want to delete?"<<endl;
cin>>id;
while(p1->next!=NULL&&p1->ID!=id)
{
p2=p1;

p1=p1->next;
}
if(p1->ID=id)
p2->next=p1->next;
else
cout<<"can't find the list!"<<endl;
return (head);
}
运行一下一个同样错误出现8次。。。可是我找不到。。。指针对指针赋值应该没关系的啊

搜索更多相关的解决方案: 赋值  指针  

----------------解决方案--------------------------------------------------------
C or C++

C = C++ or C != C++

C is C++

C is not C++


----------------解决方案--------------------------------------------------------
c++
----------------解决方案--------------------------------------------------------
提示: 作者被禁止或删除 内容自动屏蔽

2006-07-15 07:43:03
菜鸟1号

等 级:新手上路
帖 子:43
专家分:0
注 册:2006-4-7
  得分:0 

怎么说?


----------------解决方案--------------------------------------------------------
以下是引用菜鸟1号在2006-7-15 10:58:10的发言:

#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
const int namelength=20;
const int phonelength=20;
struct employee//结构体
{
char name[namelength];
char num[phonelength];
long ID;
struct employee *next; //漏了这个
};

struct employee* creat(void);//创建链表
struct employee* insert(struct employee*);//插入链表
void show(struct employee*);//显示链表
struct employee* modify(struct employee*);//修改链表
struct employee* DELETE(struct employee*);//删除链表
//函数声明


void main()//主函数
{long id;
char answer;
struct employee* head=NULL;
cout<<"A.creat an initial linked list of names and phone numbers"<<endl;//界面窗口
cout<<"B.insert a new structure into the linked list"<<endl;
cout<<"C.modify an existing structure in the linked list"<<endl;
cout<<"D.delete an existing structure from the list"<<endl;
cout<<"E.exit from the program"<<endl;

do
{

cout<<"ENTER YOUR CHOICE"<<endl;

cin>>answer;

switch(answer)

{


case 'A':
head=creat();
break;
case 'B':
head=insert(head);
break;
case 'C':
head=modify(head);
break;
case 'D':
cout<<"please enter the id of which you want to delete"<<endl;
cin>>id;
head=DELETE(head);
break;
case 'E':
system("cls");

break;
default:
break;

}

show(head);
cout<<"continue?Y/N"<<endl;

}while(answer=='y'||answer=='Y');


}


struct employee* creat(void)
{
int n=0;
struct employee *head,*p1,*p2;
p1=p2=new employee;
head=NULL;
cout<<"enter name number and ID:"<<endl;
cin>>p1->name>>p1->num>>p1->ID;
while(p1->ID)
{n++;
if(n==1)
{
head=p1;
p1->next=NULL;
}
else
p2->next=p1;
p2=p1;

p1=new employee;
cout<<"enter name number and ID:"<<endl;
cin>>p1->name>>p1->num>>p1->ID;

}
p2->next=NULL;
cout<<"the new linked list created successfully!"<<endl;
return(head);
}

struct employee* insert(struct employee* head)
{
struct employee newlist;
cout<<"please initial the new list(name number ID)"<<endl;
cin>>newlist.name>>newlist.num>>newlist.ID;
newlist.next=NULL;

struct employee *p0=&newlist,*p1,*p2;
p1=head;

if(head==NULL)
{head=p0;
p0->next=NULL;
}
else
while(p1->next!=NULL&&p1->ID<p0->ID)
{p2=p1;
p1=p1->next;
}
if(p1->ID<p0->ID)
{p1->next=p0;
p0->next=NULL;
}
else
{p2->next=p0;
p0->next=p1;
}
cout<<"insert successfully!"<<endl;
return(head);
}

void show(struct employee* head)
{
cout<<"date below"<<endl;
cout<<"******************************************************"<<endl;

while(head->next)
{cout<<head->name<<" "<<head->num<<" "<<head->ID<<endl;
head=head->next;
}
}
struct employee* modify(struct employee*head)
{
struct employee* p;
int temp;
long id;
cout<<"employee in which ID you want to modify?"<<endl;
cin>>id;
while(p->next!=NULL&&p->ID!=id)
p=p->next;
if(p->ID=id)
{
cout<<"which one do you want to madify 1.name 2.num?"<<endl;

cin>>temp;
if(temp=1)
cin.getline(p->name,namelength);
else if(temp=2)
cin.getline(p->num,phonelength);

}
else
cout<<"fail to modify!"<<endl;
return(head);
}


struct employee* DELETE(struct employee*head)
{
struct employee *p1,*p2;
p1=p2=head;
long id;
cout<<"which one do you want to delete?"<<endl;
cin>>id;
while(p1->next!=NULL&&p1->ID!=id)
{
p2=p1;

p1=p1->next;
}
if(p1->ID=id)
p2->next=p1->next;
else
cout<<"can't find the list!"<<endl;
return (head);
}
运行一下一个同样错误出现8次。。。可是我找不到。。。指针对指针赋值应该没关系的啊


----------------解决方案--------------------------------------------------------