下面的代码,没有编译错误,但为什么输出乱码呢?大侠们帮我看一下,谢谢!
#include <iostream>
#include<cstring>
using namespace std;
class Node
{
public:
Node *next;
char Name[10];
char No[10];
class LinkedList;
public:
Node( char name[], char no[]):next(NULL)
{strcpy(Name,name);
strcpy(No,no);
}
};
class LinkedList
{
public :
Node *front,*rear;
int size ;
public:
LinkedList(){Node node("Í·½áµã","000");front=&node;rear=&node;size=1;}
void Add_rear(char name[],char no[])
{
Node node(name,no);
rear->next=&node;
rear=&node;
size++;
}
void Add_front(char name[],char no[])
{ Node node(name,no);
node.next=front->next;
front=&node;
size++;
}
void Add_at(int n,char name[],char no[])
{ Node *m_next;
m_next=front;
for(int i=0;i<n-1;i++)
m_next=m_next->next;
Node node(name,no);
node.next=m_next;
m_next=&node;
size++;
}
void delete_front()
{ Node *p;
p=front;
if(size==0)
{ cout<<"Á´±í¿Õ£¡"<<endl;
exit(1);
}
front=front->next;
size--;
delete p;
}
void delete_at(int n)
{
if(size==0)
{cout<<"Á´±í¿Õ !"<<endl;
exit(1);
}
Node *m_next;
m_next=front;
for(int i=0;i<n-1;i++)
m_next=m_next->next;
Node *p;
p=m_next;
m_next=m_next->next;
size--;
delete p;
}
void find(int index)
{ Node *m_front;
m_front=front;
if((index<0)||(index>size))
{cout<<"²»´æÔÚ´ËÊý¾Ý!"<<endl;
exit(1);
}
for(int i=1;i<index-1;i++)
m_front=m_front->next;
cout<<"ÐÕÃûÊÇ:"<<m_front->Name<<endl<<"µç»°ÊÇ:"<<m_front->No<<endl;
}
void find (char item[])
{ Node *m_next ;
m_next=front;
for(int i=0;i<=size;i++)
{
if(strcmp(m_next->Name,item)==0)
{ cout<<"ÄãÒªÕҵĵ绰ÊÇ:"<<m_next->No<<endl;
m_next=m_next->next;
}
}
}
};
void main()
{ int ch,exit=0;
char name[10],no[10];
LinkedList list ;
while (!exit)
{
cout<<"ÇëÑ¡Ôñ:1 Ìí¼Ó 2 ɾ³ý 3 ²éÕÒ 4 ÏÔʾËùÓеļǼ"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"ÇëÑ¡ÔñÌí¼Ó·½Ê½: 1 ±íÍ·Ìí¼Ó 2±íβÌí¼Ó Ìض¨Î»ÖÃÌí¼Ó"<<endl;
int ch1;
cin>>ch1;
cout<<"ÇëÊäÈëÒªÌí¼ÓÈ˵ÄÐÕÃû:"<<endl;
cin>>name;
cout<<"ÇëÊäÈëÒªÌí¼ÓÈ˵ĵ绰£º"<<endl;
cin>>no;
switch(ch1)
{
case 1:
list.Add_front(name,no);
break;
case 2:
list.Add_rear(name,no);
break;
case 3:int m;
cout<<"ÇëÊäÈëÒªÌí¼ÓµÄλÖÃ:"<<endl;
cin>>m;
list.Add_at(m,name,no);
break;
}
break;
case 2:int ch2;
cout<<"ÇëÑ¡Ôñɾ³ýµÄ·½Ê½£º1 ±íͷɾ³ý 2 Ìض¨Î»ÖÃɾ³ý"<<endl;
cin>>ch2;
switch(ch2)
{
case 1: list.delete_front();
break;
case 2: int m;
cout<<"ÇëÊäÈëҪɾ³ýÊý¾ÝµÄλÖÃ"<<endl;
cin>>m;
list.delete_at(m);
break;
}
break;
case 3:int ch3;
cout<<"ÇëÑ¡ÔñÒª²éÕҵķ½Ê½: 1 ÐÕÃû²éÕÒ 2 Ìض¨Î»ÖòéÕÒ "<<endl;
cin>>ch3;
switch(ch3)
{ case 1: cout<<"ÇëÊäÈëÐÕÃû"<<endl;
cin>>name;
list.find(name);
break;
case 2: int m;
cout<<"ÇëÊäÈëÒª²éÕÒµÄÊý¾ÝµÄλÖÃ"<<endl;
cin>>m;
list.find(m);
break;
}
break;
case 4:Node *m_next;
m_next=list.front;
cout<<"ËùÓеļǼ:"<<endl;
for(int i=0;i<list.size;i++)
{cout<<m_next->Name<<endl<<m_next->No<<endl;
m_next=m_next->next;
}
break;
}
cout<<"Í˳öÇëÊäÈë:1£¬¼ÌÐøÇëÊäÈë:0"<<endl;
cin>>exit;
}
}