链表创建途中遇到的问题

#include <iostream>
#include<new>
using namespace std;
struct node{
int data;
node *next;
};
class list{
public:
node *head;
list(){head=null};
list(int x){
node *p,*q;
p=q=head;
head=new node;

head->next=null;
head->data=0;
for(int i=1;i<x;i++)
{
p=new node;
p->data=i;
p->next=null;
q->next=p;
q=p;
}q->next=null;

};
void print()
{
node *r=head;
if(head==null)
cout<<"have no number"<<endl;
else while(r!=null)
{cout<<r->data;
r=r->next;}
}
}
int main()
{
int n;
cout<<"input number>>"<<endl;
cin>>n;
list l1;
list l2(n);
cout<<"list1"<<endl;
l1.print();
cout<<"list2"<<endl;
l2.print();

return 0;
}
论该程序的list 2为什么无法输出全部链表值?
错误总结一:
请看如下改进:
#include <iostream>
#include<new>
using namespace std;
struct node{
int data;
node *next;
};
class list{
public:
node *head;
list(){head=NULL;};
list(int x){
node *p,*q;

head=new node;
p=q=head;//the imporant position
head->next=NULL;
head->data=0;
for(int i=1;i<x;i++)
{
p=new node;
p->data=i;
p->next=NULL;
q->next=p;
q=p;
}q->next=NULL;

};
void print()
{
node *r=head;
if(head==NULL)
cout<<"have no number"<<endl;
else while(r!=NULL)
{cout<<r->data;
r=r->next;}
}
};
int main()
{
int n;
cout<<"input number>>"<<endl;
cin>>n;
list l1;
list l2(n);
cout<<"list1"<<endl;
l1.print();
cout<<"list2"<<endl;
l2.print();

return 0;
}
之所以第一段代码的list2无法输出全部链表的原因是p,q若在head创建存储空间(new)之前赋值,三个都指向一的个空间并不是head=new node的那个空间
、、、、、、、、、、、、、、、
node *p,*q;
cout<<head<<endl;//1
head=new node;
cout<<head<<endl;//2
。。。。。。。。。。。。。。可以通过部分添加如上两行反映1,2地址是不同的。
而导致的后边创建链表空间用p,q,根本就和head没有关系。即head就只有表头一个空间。

时间: 2024-07-30 17:48:37

链表创建途中遇到的问题的相关文章

链表创建和链表遍历算法的演示_C语言

今天搞了一个多小时,头是疼的,应该是没休息好吧,学习了数据结构这一节,感觉收益良多,下面贴上代码和心得: 1 /*24_链表创建和链表遍历算法的演示*/ 2 # include <stdio.h> 3 # include <malloc.h> 4 # include <stdlib.h> 5 6 typedef struct Node 7 { 8 int data;//数据域 9 struct Node * pNext;//指针域 10 }NODE, *PNODE;//

链表创建打印删除

链表创建打印删除:http://wenku.baidu.com/view/d2343df67c1cfad6195fa7d8.html http://bbs.ednchina.com/BLOG_ARTICLE_2143077.HTM http://zhidao.baidu.com/link?url=vdWWhzPcyNykH1NSG-EdhHnPAsmTS6VDSmm-hMZU7GZPi7-w5s-WRpANOCat9y7C5xQHYLjwjPQWeZj9yb_91q http://www.2ct

链表(创建,插入,删除和打印输出(转载)

链表(创建,插入,删除和打印输出 /*----------------------------------------------------------------------------- 文件功能:实现了动态建立一个学生信息的链表包括链表的创建.插入.删除.和打印输出学生信息包括姓名和分数本链表是带有头结点的,头结点的内容为空内容-----------------------------------------------------------------------------*//*

链表 创建 插入 删除 查找 合并

最近学习了一下单链表的操作,将代码保存如下,供以后查看. 链表创建: 1.先建立一个不含数据的头指针*head,头指针的链接域为NULL. 2.声明一个用于暂存新申请空间的指针*pc,一个用于保存创建的链表的指针*r,令*r指向*head. 3.在循环中,为指针*pc申请空间,并给数据域赋值,head->next = pc, pc->next = NULL, head = pc. #define _CRT_SECURE_NO_DEPRECATE /*取消scanf,printf不安全之类的错误

链表创建及增加节点

1 #include <stdio.h> 2 #include <string.h> 3 #include "zmalloc.h" 4 //定义节点 5 typedef struct listNode { 6 struct listNode *prev; 7 struct listNode *next; 8 void *value; 9 } listNode; 10 11 //定义链表 12 typedef struct list { 13 listNode *

链表学习一:单链表创建-头插入与尾插入

链表的创建过程是一个动态的生成过程,创建链表有两种思路,一种是从表头插入,另一种是从表尾插入. 表头插入思路:从一个空表开始,重复读入数据,生成新结点,将读入数据存放在新结点的数据域中,然后将新结点插入到当前链表的表头上,直到读入结束标志为止. 表尾插入思路:从一个空表开始,重复读入数据,生成新结点,将读入数据存放在新结点的数据域中,然后将新结点插入到当前链表的表尾上,直到读入结束标志为止. 两种方法C++实现如下: 1 #include<iostream> 2 using namespace

C语言链表创建

#include "malloc.h"#include "stdio.h"#define LEN sizeof(struct student) typedef struct student{ int num; int age; float score; struct student *next;}stu;int n;// 创建动态链表函数stu *creat(void){ //定义结构体类型的指针 stu *head,*p1,*p2; n=0; p1=p2=(stu

[PHP] 数据结构-链表创建-插入-删除-查找的PHP实现

链表获取元素1.声明结点p指向链表第一个结点,j初始化1开始2.j<i,p指向下一结点,因为此时p是指向的p的next,因此不需要等于3.如果到末尾了,p还为null,就是没有查找到 插入元素1.插入元素和查找类似,找到位置后2.生成新的结点s, s->next=p->next p->next=s; 删除元素1.删除元素,找到位置后2.绕过一下,q=p->next p->next=q->next; <?php class Node{ public $data

单向非循环链表:链表创建、节点插入、链表打印、节点长度计算、链表清空、链表销毁

/* 单向非循环链表:    初始化    前插入 后插入    打印    链表长度    清空    销毁*/#include <stdio.h>#include <stdlib.h> #define itemType int typedef struct node{    itemType data;    struct node *pNext;}Node; /* 创建Head节点: 节点的pNext为NULL */int initList(Node **ppN){    N