![]() |
#2
fz199101252012-12-06 17:24
|

#include "stdafx.h"
#include <iostream>
using namespace std;
#define ZERO 0
class Node
{
public:
int num;
Node* next;
Node* head;
void creat()
{
int count=0;
cout<<"please input Node Num";
cin>>count;
if (count <= ZERO)
{
head = NULL;
return ;
}
Node* p1 = new Node;
Node* p2;
head = p1;
for (int i = 0 ; i < count ;++i)
{
cout<<i+1<<":";
cin>>p1->num;
p2 = p1;
p1= new Node;
p2->next = p1;
}
delete p1;
p2->next = NULL;
return ;
}
void show()
{
Node* tmp = head;
while(tmp)
{
cout<<tmp->num<<endl;
tmp = tmp->next;
}
delete tmp;
}
void insert_font()
{
Node* tmp_Node = new Node;
cout<<"input insert of font num:"<<endl;
cin>>tmp_Node->num;
tmp_Node->next = head;
head = tmp_Node;
}
void insert_real()
{
Node* tmp_Node = new Node;
cout<<"input insert of real num:"<<endl;
cin>>tmp_Node->num;
Node*tmp = head;
while(tmp)
{
if (tmp->next == NULL)
{
tmp->next = tmp_Node;
tmp_Node->next = NULL;
//break;
}
tmp = tmp->next;
}
delete tmp;
}
void insert_center()
{
//插入的数据
Node* tmp_Node = new Node;
//插入的位置
int postion;
cout<<"input insert of center num:"<<endl;
//输入插入数据
cin>>tmp_Node->num;
cout<<"insert postion"<<endl;
//输入插入位置
cin>>postion;
//判断异常
if (postion <= ZERO)
{
return ;
}
//创建head头结点副本
Node* tmp = head;
//循环跳转节点
for (int i = 0 ; i < postion-1 ; ++i)
{
tmp = tmp->next;
}
//插入数据,前一个节点
Node* tmpePos = tmp;
Node* tmpeNext = tmp->next;
//将该节点的next指向新节点
tmpePos->next = tmp_Node;
//新节点的next指向原始节点的next
tmp_Node->next = tmpeNext;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Node a;
a.creat();
a.insert_font();
system("cls");
a.show();
a.insert_real();
system("cls");
a.show();
a.insert_center();
system("cls");
a.show();
system("pause");
return 0;
}
大家帮我看看这个链表有什么缺陷?PS:删除还没写,继续更新中#include <iostream>
using namespace std;
#define ZERO 0
class Node
{
public:
int num;
Node* next;
Node* head;
void creat()
{
int count=0;
cout<<"please input Node Num";
cin>>count;
if (count <= ZERO)
{
head = NULL;
return ;
}
Node* p1 = new Node;
Node* p2;
head = p1;
for (int i = 0 ; i < count ;++i)
{
cout<<i+1<<":";
cin>>p1->num;
p2 = p1;
p1= new Node;
p2->next = p1;
}
delete p1;
p2->next = NULL;
return ;
}
void show()
{
Node* tmp = head;
while(tmp)
{
cout<<tmp->num<<endl;
tmp = tmp->next;
}
delete tmp;
}
void insert_font()
{
Node* tmp_Node = new Node;
cout<<"input insert of font num:"<<endl;
cin>>tmp_Node->num;
tmp_Node->next = head;
head = tmp_Node;
}
void insert_real()
{
Node* tmp_Node = new Node;
cout<<"input insert of real num:"<<endl;
cin>>tmp_Node->num;
Node*tmp = head;
while(tmp)
{
if (tmp->next == NULL)
{
tmp->next = tmp_Node;
tmp_Node->next = NULL;
//break;
}
tmp = tmp->next;
}
delete tmp;
}
void insert_center()
{
//插入的数据
Node* tmp_Node = new Node;
//插入的位置
int postion;
cout<<"input insert of center num:"<<endl;
//输入插入数据
cin>>tmp_Node->num;
cout<<"insert postion"<<endl;
//输入插入位置
cin>>postion;
//判断异常
if (postion <= ZERO)
{
return ;
}
//创建head头结点副本
Node* tmp = head;
//循环跳转节点
for (int i = 0 ; i < postion-1 ; ++i)
{
tmp = tmp->next;
}
//插入数据,前一个节点
Node* tmpePos = tmp;
Node* tmpeNext = tmp->next;
//将该节点的next指向新节点
tmpePos->next = tmp_Node;
//新节点的next指向原始节点的next
tmp_Node->next = tmpeNext;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Node a;
a.creat();
a.insert_font();
system("cls");
a.show();
a.insert_real();
system("cls");
a.show();
a.insert_center();
system("cls");
a.show();
system("pause");
return 0;
}
[ 本帖最后由 fz19910125 于 2012-12-6 17:44 编辑 ]