主题:不知程序问题出在哪里了??
不知程序问题出在哪里了??
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node
{
char data;
node *next;
}*LinkList;
void Traverse(LinkList L)
{
LinkList p;
p=L->next ;
cout<<"链表中数据为:"<<endl;
while(p!=NULL)
{ cout<<p->data <<" ";
p= p->next;
}
cout<<endl;
}
void CreateFromHead(LinkList &L)
{
L= new node;
L->next= NULL;
char c;
cout<<"请输入结点元素:"<<endl;
cin>>c;
while( c!='#')
{
node *s=(LinkList)malloc(sizeof(node));
s->data= c;
s->next= L->next;
L->next= s;
cout<<"请输入结点元素:"<<endl;
cin>>c;
}
cout<<"创建成功!"<<endl;
}
void CreateFromTail(LinkList &L)
{
L=(LinkList)malloc(sizeof(node));
L->next= NULL;
node* r=L;
char c;
cout<<"请输入结点元素:"<<endl;
cin>>c;
while( c!='#')
{
node* s=(LinkList)malloc(sizeof(node));
s->data=c;
s->next=r->next;
r->next=s;
r=s;
cout<<"请输入结点元素:"<<endl;
cin>>c;
}
r->next=NULL;
cout<<"创建成功!"<<endl;
}
//
int IsEmpty(LinkList L)
{
if( L->next!=NULL)return 1;
else return 0;
}
LinkList Getelem(LinkList L,int i)
{
node* p=L->next;
int j=0;
while ( p&& ++j<i)
p= p->next;
return p;
}
LinkList Locate(LinkList L,char key)
{
node* p= L->next;
int i=1;
while(p!=NULL)
if(p!=key )
{
p=p->next;
i++;
}
else break;
if(p!=NULL)cout<<"这是第"<<i<<"个元素"<<endl;
return p;
}
int ListLength(LinkList L)
{
node* p= L->next;
int j=0;
while(p!=NULL)
{
p= p->next ;
j++;
return j;
}
int main()
{
LinkList L;
CreateFromHead(L);//从头开始--头插法
//CreateFromTail(L);//从尾开始--尾插法
Traverse(L);//验证创建效果
//查找第i个元素=============================================
int pos;
cout<<"需要读取第几个元素?"<<endl;
cin>>pos;
node* p=Getelem(L,pos);
if(!p)cout<<"没有这个元素!"<<endl;
else cout<<"这个元素是:"<<p->data<<endl;
//按值查找==================================================
char a;
cout<<"需要查找哪个元素?"<<endl;
cin>>a;
node* pk=Locate(L,a);
if(!pk)cout<<"没有这个元素!"<<endl;
//计算长度==================================================
cout<<"当前链表的长度为:"<<ListLength(L)<<endl;
return 0;
}
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node
{
char data;
node *next;
}*LinkList;
void Traverse(LinkList L)
{
LinkList p;
p=L->next ;
cout<<"链表中数据为:"<<endl;
while(p!=NULL)
{ cout<<p->data <<" ";
p= p->next;
}
cout<<endl;
}
void CreateFromHead(LinkList &L)
{
L= new node;
L->next= NULL;
char c;
cout<<"请输入结点元素:"<<endl;
cin>>c;
while( c!='#')
{
node *s=(LinkList)malloc(sizeof(node));
s->data= c;
s->next= L->next;
L->next= s;
cout<<"请输入结点元素:"<<endl;
cin>>c;
}
cout<<"创建成功!"<<endl;
}
void CreateFromTail(LinkList &L)
{
L=(LinkList)malloc(sizeof(node));
L->next= NULL;
node* r=L;
char c;
cout<<"请输入结点元素:"<<endl;
cin>>c;
while( c!='#')
{
node* s=(LinkList)malloc(sizeof(node));
s->data=c;
s->next=r->next;
r->next=s;
r=s;
cout<<"请输入结点元素:"<<endl;
cin>>c;
}
r->next=NULL;
cout<<"创建成功!"<<endl;
}
//
int IsEmpty(LinkList L)
{
if( L->next!=NULL)return 1;
else return 0;
}
LinkList Getelem(LinkList L,int i)
{
node* p=L->next;
int j=0;
while ( p&& ++j<i)
p= p->next;
return p;
}
LinkList Locate(LinkList L,char key)
{
node* p= L->next;
int i=1;
while(p!=NULL)
if(p!=key )
{
p=p->next;
i++;
}
else break;
if(p!=NULL)cout<<"这是第"<<i<<"个元素"<<endl;
return p;
}
int ListLength(LinkList L)
{
node* p= L->next;
int j=0;
while(p!=NULL)
{
p= p->next ;
j++;
return j;
}
int main()
{
LinkList L;
CreateFromHead(L);//从头开始--头插法
//CreateFromTail(L);//从尾开始--尾插法
Traverse(L);//验证创建效果
//查找第i个元素=============================================
int pos;
cout<<"需要读取第几个元素?"<<endl;
cin>>pos;
node* p=Getelem(L,pos);
if(!p)cout<<"没有这个元素!"<<endl;
else cout<<"这个元素是:"<<p->data<<endl;
//按值查找==================================================
char a;
cout<<"需要查找哪个元素?"<<endl;
cin>>a;
node* pk=Locate(L,a);
if(!pk)cout<<"没有这个元素!"<<endl;
//计算长度==================================================
cout<<"当前链表的长度为:"<<ListLength(L)<<endl;
return 0;
}