学到链表这章就写了这个,在调试时发现数组下标越界2个左右的字符是可以的.但数组下标越界错误无疑是致命的,编译器不会去检查数组下标是否越界完全靠自己把握这个一定要注意:
/********************************************************************
created: 2011/12/20
created: 20:12:2011 10:22
filename: C:\Documents and Settings\honglei\桌面\cc\单向链表翻转\单向链表翻转.cpp
file path: C:\Documents and Settings\honglei\桌面\cc\单向链表翻转
file base: 单向链表翻转通过数组形式
file ext: cpp
author:
purpose:
*********************************************************************/
#include <iostream>
using namespace std;
class Ex
{
public:
char x;
Ex *next;
};
Ex *head;
Ex *Creat()
{
Ex *p1;
Ex *p2;
p1=new Ex;
head=p1;
p2=p1;
cout<<"in put numbers:"<<endl;
cin>>p1->x;
if (p1->x=='q')
{
delete p1;p1=NULL;p2=NULL;
return head;
}
while (p1->x!='q')
{
p2=p1;
p1=new Ex;
cout<<"in put numbers:"<<endl;
cin>>p1->x;
if (p1->x=='q')
{
delete p1;p2->next=NULL;
cout<<"exit"<<endl;
break;
}
p2->next=p1;
}
return head;
}
void Show(Ex *head)
{
cout<<endl;
int n=0;
char a[10];//下标越界2-3个字符无报错
cout<<"顺序输出"<<endl;
for (int m=0; head!=NULL; m++)
{
cout<<head->x<<endl;
cout<<endl;
n++;
a[m]=head->x;
head=head->next;
}
cout<<"倒序输出:"<<endl;
for (int j=n-1; j>=0; j--)
{
cout<<a[j]<<endl;
cout<<endl;
}
cout<<"链表节点个数: "<<n<<endl;
}
int main()
{
Creat();
Show(head);
return 0;
}