主题:[讨论]求救啊!~~~~~~~~~~~~~~~~
有//注解的我不会帮助我一下!!!
#include <iostream>
using namespace std;
const MAX = 100;
struct NODE
{
int data;
NODE *next;
};
typedef NODE LIST;
void IniList(LIST *&list)
{
list = new NODE;
list->next = NULL;
}
bool Empty(LIST *&list)
{
return list->next == NULL;
}
int Length(LIST *&list)
{
int result = 0;
NODE *p = list->next;
while(p)
{
p = p->next;
result++;
}
return result;
}
bool Insert(LIST *&list, int k, int x)
{
if(k<0 || k>Length(list)+1) return false;
NODE *p = list;
for(int i=1; i<k; i++)
p=p->next;
NODE *newNode = new NODE;
newNode->data = x;
newNode->next = p->next;
p->next = newNode;
return true;
}
void PrintList(LIST *&list)
{
NODE *p = list->next;
while(p)
{
cout<<p->data<<' ';
p = p->next;
}
cout<<endl;
}
//void ClearList(LIST *&list)
{
}
void main()
{
LIST *L=NULL;
IniList(L);
cout<<Empty(L)<<endl;
for(int i=0; i<5; i++)
Insert(L,1,i*5);
cout<<Length(L)<<endl;
// cout<<Retrieve(L,2)<<endl;
// cout<<Locate(L,5)<<endl;
PrintList(L);
ClearList(L);
cout<<Empty(L)<<endl;
}
#include <iostream>
using namespace std;
const MAX = 100;
struct NODE
{
int data;
NODE *next;
};
typedef NODE LIST;
void IniList(LIST *&list)
{
list = new NODE;
list->next = NULL;
}
bool Empty(LIST *&list)
{
return list->next == NULL;
}
int Length(LIST *&list)
{
int result = 0;
NODE *p = list->next;
while(p)
{
p = p->next;
result++;
}
return result;
}
bool Insert(LIST *&list, int k, int x)
{
if(k<0 || k>Length(list)+1) return false;
NODE *p = list;
for(int i=1; i<k; i++)
p=p->next;
NODE *newNode = new NODE;
newNode->data = x;
newNode->next = p->next;
p->next = newNode;
return true;
}
void PrintList(LIST *&list)
{
NODE *p = list->next;
while(p)
{
cout<<p->data<<' ';
p = p->next;
}
cout<<endl;
}
//void ClearList(LIST *&list)
{
}
void main()
{
LIST *L=NULL;
IniList(L);
cout<<Empty(L)<<endl;
for(int i=0; i<5; i++)
Insert(L,1,i*5);
cout<<Length(L)<<endl;
// cout<<Retrieve(L,2)<<endl;
// cout<<Locate(L,5)<<endl;
PrintList(L);
ClearList(L);
cout<<Empty(L)<<endl;
}