c++改错(链表插入)

来源:百度知道 编辑:UC知道 时间:2024/05/13 17:48:50
需要经过调试运行
#include "iostream.h"
class Node
{
friend class LinkedList;
private:
int data;
Node *next;
public:
Node() {};
Node(int i)
{
data=i;
next=NULL;
}
};

class LinkedList
{
private:
Node *head;
Node *tail;
public:
LinkedList();
void print();
void insert (int a);
};

LinkedList::LinkedList()
{
head = new Node;
head->next = NULL;
tail=head;
}

void LinkedList::print()
{
Node *p=head->next;
while (p!=NULL)
{
cout << p->data << " ";
p=p->next;
}
cout << endl;
}

void LinkedList::insert(int a)
{
Node *p,*s;
p = head->next;
s->data = a;
s->next = p->next;
p->next = s;
while(!p) p = p->next;
//Node *s;
//Node

1.将新结点插入链头
void LinkedList::insert(int a)
{
Node *p,*s,*t;
p = head;
t = new Node(a);
t->next = p;
head = t;
}

2.将新结点插入链尾
void LinkedList::insert(int a)
{
Node *p,*s,*t;
p = head;
while(NULL!=p->next){
p = p->next;
}
t = new Node(a);
p->next = t
t->next = NULL;
tail = t;
}

没缩进不能怪提问者,只能怪系统没这功能,是系统吃掉了空格。
void LinkedList::insert(int a)
{
Node *p,*s;
p = head->next; //这一句有问题,执行后p=NULL
s->data = a;
s->next = p->next;
p->next = s;
while(!p) p = p->next;
//Node *s;
//Node *p=head;
//s =new Node(a);
//s->next=p->next;
//if(p->next=NULL)tail=s;
//p->next = s;
}

编译什么报错啊?~

知道没缩进..看得很累人的!

support