c++程序调试问题

gg_664MT 2012-04-12 10:02:25
//建立线性链表,记录和管理某产品参数,管理包括按序号插入、删除、查询

#include<iostream.h>
#include<stdio.h>
#include <stdlib.h>
#include"实习.h"
#define type struct product
#define mum sizeof(type)
//定义链表
typedef struct product
{ int num; // 序号
int data; // 参数
struct product *next;// 指针
struct product *pf,*pb;
}product,*list;

//创建链表
void creatlist(list &head){
int n; //数据个数
cout<<"输入初始数据个数:"<<endl;
cin>>n;
head=(list)malloc(sizeof(product)); //建立带头结点的链表
head->next=NULL;
struct product *pf;
for(int i=1;i<=n;i++)
{ pf=(list)malloc(sizeof(product)); //生成新结点
cout<<"请输入第"<<i<<"个产品的序号及参数"<<endl; //输入元素值
cin>>pf->num>>pf->data;
pf->next=head->next;
head->next=pf;
}//for
}// creatlist

//按序插入某产品信息
void insert(list&head,int num1,int data1){
struct product *pb=head;
while(pb&&pb->num<num1){pb=pb->next;}
struct product *pf;
pf=(list)malloc(mum); //生成新节点
pf->data=data1;
pf->next=pb->next;
pb->next=pf;

}
//按序号查询产品信息(参数)
void search(list&head,int num1,int* e){
struct product *pb=head;
if(num1<0)cout<<"序号不合法!"<<endl;
while (pb->num<num1){pb=pb->next;} //查找位置
*e=pb->data;
}

//删除某一序号的有关记录
void delet(list& head,int num1){
type * pb=head->next;
while(pb&&pb->num!=num1)
{
pb=pb->next;
} //查找位置

struct product* temp = head->next;

while(temp->next!=pb)
{
temp = temp->next;
}

if(pb->next==NULL)pb=NULL; //若只有一个节点
else {
struct product* pf=pb->next; //若有多个节点
temp->next=pf;
free(pb);
}//else
}//delete

//输出所有的结点
void print(type* head){

head = head->next;
while(head!=NULL){
cout<<"序号:"<<head->num<<"参数:"<<head->data<<endl;
head=head->next;
}//while
}//print


//////////////////////////验证////////////////////////////////////////////////////////////
void main(){


//创建链表,输出链表//////////////
type*head; int num,data;
creatlist(head);
print(head);

//////////删除//////////////////
cout<<"请输入要删除的产品序号:"<<endl;
cin>>num;
delet(head,num);
print(head);

/////////查询//////////////////
cout<<""<<endl;
cin>>num;
int p;
int* u=&p;
search(head,num,u);
cout<<"序号为"<<num<<"的产品参数为"<<p<<endl;


////////////插入////////////////

cout<<"请输入要添加的序号及参数:"<<endl;
cin>>num>>data;
insert(head,num,data);
print(head);

}
...全文
128 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyou5840 2012-04-16
  • 打赏
  • 举报
回复
在if(pb->next==NULL)
pb=NULL; 后加free(pb);
temp->next = NULL;并用括号括起来
如:if(pb->next==NULL)
{pb=NULL;
free(pb);
temp->next = NULL;
}//若只有一个节点
evencoming 2012-04-16
  • 打赏
  • 举报
回复
ok

#include<iostream>
#include<stdio.h>
#include <stdlib.h>
//#include"实习.h"
using namespace std;
#define type struct product
#define mum sizeof(type)
//定义链表
typedef struct product
{
int num; // 序号
int data; // 参数
struct product *next;// 指针
struct product *pf,*pb;
} product,*list;
//创建链表
void creatlist(list &head)
{
int n; //数据个数
cout<<"输入初始数据个数:"<<endl;
cin>>n;
head=(list)malloc(sizeof(product)); //建立带头结点的链表
head->next=NULL;
struct product *pf;
for(int i=1; i<=n; i++)
{
pf=(list)malloc(sizeof(product)); //生成新结点
cout<<"请输入第"<<i<<"个产品的序号及参数"<<endl; //输入元素值
cin>>pf->num>>pf->data;
pf->next=head->next;
head->next=pf;
}//for
}// creatlist
//按序插入某产品信息
void insert(list&head,int num1,int data1)
{
//changed
struct product *pb=head->next,*q=head;
while(pb&&pb->num<num1)
{
q=pb;
pb=pb->next;
}
struct product *pf;
pf=(list)malloc(mum); //生成新节点
pf->num=num1;
pf->data=data1;
pf->next=q->next;
q->next=pf;
}
//按序号查询产品信息(参数)
void search(list&head,int num1,int* e)
{
struct product *pb=head;
if(num1<0)cout<<"序号不合法!"<<endl;
while (pb&&pb->num<num1)
{
pb=pb->next; //查找位置
}
if(pb&&pb->num==num1)
*e=pb->data;
else *e=-1;//没找到
}
//删除某一序号的有关记录
void delet(list& head,int num1)
{
type * pb=head->next;
while(pb&&pb->num!=num1)
{
pb=pb->next;
} //查找位置
//没找到,返回
if(!pb)
return ;
struct product* temp = head;
while(temp->next!=pb)
{
temp = temp->next;
}
temp->next=pb->next;
free(pb);
}//delete
//输出所有的结点
void print(type* head)
{
head = head->next;
while(head!=NULL)
{
cout<<"序号:"<<head->num<<"参数:"<<head->data<<endl;
head=head->next;
}//while
}//print

//////////////////////////验证////////////////////////////////////////////////////////////
int main()
{

//创建链表,输出链表//////////////
type*head;
int num,data;
creatlist(head);
print(head);
//////////删除//////////////////
cout<<"请输入要删除的产品序号:"<<endl;
cin>>num;
delet(head,num);
print(head);
/////////查询//////////////////
cout<<""<<endl;
cin>>num;
int p;
int* u=&p;
search(head,num,u);
cout<<"序号为"<<num<<"的产品参数为"<<p<<endl;

////////////插入////////////////
cout<<"请输入要添加的序号及参数:"<<endl;
cin>>num>>data;
insert(head,num,data);
print(head);
system("pause");
return 0;
}

wuyou5840 2012-04-16
  • 打赏
  • 举报
回复
while(pb&&pb->num<num1)中的第一个pb改为pb->next.
Insert的pf->data=data1;后面加上pf->num = num1;
lkxd2011 2012-04-12
  • 打赏
  • 举报
回复
你想问什么?
pengfoo 2012-04-12
  • 打赏
  • 举报
回复

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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