c++学的 太差了!(在线)

noscar 2004-04-06 04:36:34
我 在表 尾插入 ,不对!

#include "stdio.h"
#include "conio.h"
class Node
{
public : Node(int e);
public: int getE();
private: int _e;
public: Node* getN();
private :Node *next;
};

Node * Node::getN()
{
return next;
}

Node::Node(int e)
{
_e=e;
next=NULL;
}

int Node::getE()
{
return _e;
}

class Head
{
public: void Add(Node *node,int p);
public: void print();
private: Node *end;
private: Node *head;
};

void Head::print()
{
while(head)
{
printf("%3d",head->getE());
head=head->getN();
}
}

void Head::Add(Node *node,int p)
{
Node *c1;
Node *c2;
if(p==1)
{
end=node;
head=end;
}
else
{
c1=end;
end=node;
c2=c1->getN();
c2=end;
}
}

void main()
{
Head *head=new Head();
clrscr();
head->Add(new Node(1),1);
head->Add(new Node(2),2);
head->Add(new Node(3),3);
head->print();
getchar();

}
...全文
24 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
noscar 2004-04-07
  • 打赏
  • 举报
回复
谢谢,兄弟们了,我是 学了,java,我已经 找到 错了!
。。。
c1=end;
end=node;
c2=c1->getN();
c2=end;
。。。。

要 用 一个friend 函数来 抄作 end 的 next !
jeff__lueny 2004-04-06
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<malloc.h>

#define ERROR 0
#define OK 1
#define NULL 0

typedef int status;

typedef struct LNode
{
char data;
struct LNode *next;
}LNode,* LinkList;

int i;
LinkList L;

void Display(LinkList L1)
{
while(L1!=NULL)
{
printf("%c\n",L1->data);
L1=L1->next;
}
}


void CreateList_L(int n)
{
char ch;
LinkList head;
LNode *temp1, *temp2;
head = NULL;

ch = getchar();

for (i=1; i<=n; i++)
{
temp1 = (LNode *)malloc(sizeof(LNode));
temp1 -> data = ch;
if (head == NULL)
head = temp1;
else
temp2->next = temp1;
temp2 = temp1;
ch = getchar();
ch = getchar();
}
if (temp2 != NULL)
temp2->next=NULL;
L = head;
}

void main()
{
int n=6;
CreateList_L(n);
printf("show!\n");
Display(L);
}
jp1984 2004-04-06
  • 打赏
  • 举报
回复
to superman421(superman) ;
大师~~。。STL用这么好???

我绝对比楼主学的差。。我才刚开始学连表呢。 。你都开始找错了。 。
//*love myj*
古布 2004-04-06
  • 打赏
  • 举报
回复
class Node
{
public :
Node(int e);
int getE();
int _e;
Node* getN();
private :
Node *next;
};
superman421 2004-04-06
  • 打赏
  • 举报
回复
不是不帮你改,想楼上说的,你肯定是看了JAVA来的。

给你一个我写的连表,自己琢磨一下!


template<class Type> class List;

template<class Type> class ListNode{
friend class List<Type>;
private:
Type data;
ListNode<Type> * link;
public:
ListNode();
ListNode(const Type& item);
ListNode<Type> * NextNode(){ return link;}
void InsertAfter(ListNode<Type> * p);
ListNode<Type> * GetNode(const Type& item,ListNode<Type> * next=NULL);
ListNode<Type> * RemoveAfter();
};

template<class Type> class List{
private:
ListNode<Type> * first,*last;
public:
List(const Type& value){last=first=new ListNode<Type>(value);}
List(){last=first=new ListNode<Type>;}
~List();
void MakeEmpty();
int Length()const;
ListNode<Type>* Find(const Type& value);
ListNode<Type>* Find(int i);
int Insert(Type value,int i);
Type* Remove(int i);
Type* Get(int i);
};

template<class Type> ListNode<Type>::ListNode():link(NULL){}

template<class Type> ListNode<Type>::ListNode(const Type& item):data(item),link(NULL){}

template<class Type> void ListNode<Type>::InsertAfter(ListNode<Type>* p){
p->link=link;
link=p;
}

template<class Type> ListNode<Type>* ListNode<Type>::GetNode(const Type& item,ListNode<Type> * next){
ListNode<Type>* newNode=new ListNode<Type>(item);
newNode->link=next;
return newNode;
}

template<class Type> ListNode<Type>* ListNode<Type>::RemoveAfter(){
ListNode<Type> * p=link;
if(!p) return NULL;
link=p->link;
return p;
}

template<class Type> List<Type>::~List(){
MakeEmpty();
delete first;
}

template<class Type> void List<Type>::MakeEmpty(){
ListNode<Type>* p=first->link;
while(p){
first->link=p->link;
delete p;
p=first->link;
}
last=first;
}

template<class Type> int List<Type>::Length() const{
ListNode<Type>* p=first->link;
for(int i=0;p;i++) p=p->link;
return i;
}

template<class Type> ListNode<Type>* List<Type>::Find(const Type& value){
ListNode<Type>* p=first->link;
while(p&&p->data!=value) p=p->link;
return p;
}

template<class Type> ListNode<Type>* List<Type>::Find(int i){
if(i<-1) return NULL;
if(i==-1) return first;
ListNode<Type> * p=first->link;
for(int j=0;p&&j<i;j++) p=p->link;
return p;
}

template<class Type> int List<Type>::Insert(Type value,int i){
ListNode<Type>* p=Find(i-1);
if(!p) return 0;
p->link=p->GetNode(value,p->link);
if(p==last) last=p->link;
return 1;
}

template<class Type> Type* List<Type>::Remove(int i){
ListNode<Type> * p=Find(i-1),*q;
if(!p||!p->link) return NULL;
q=p->link;
p->link=q->link;
Type* value=new Type(q->data);
delete q;
return value;
}

template<class Type> Type* List<Type>::Get(int i){
ListNode<Type> * p=Find(i);
if(!p) return NULL;
return &p->data;
}
owl2008 2004-04-06
  • 打赏
  • 举报
回复
看你写的样子、你是先学java的吧?
coldrainn 2004-04-06
  • 打赏
  • 举报
回复
我们都是菜鸟

努力!
qazxsw1982103 2004-04-06
  • 打赏
  • 举报
回复
有我差吗?!!
bm1408 2004-04-06
  • 打赏
  • 举报
回复
我比你学的还差!

64,648

社区成员

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

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