您的位置:首页 > 编程语言 > C语言/C++

链表的建立和一些基本功能的实现

2014-12-10 13:01 316 查看
#include<iostream>
using namespace std;
typedef int T;

class List  {
struct Node{
Node*next;
T data;
Node( const T t = T() ) : data(t), next(NULL) {}
};
int len ;
Node *head;
public:
List() : len(0), head(NULL) {}
void push_front(T t)
{
Node *p = new Node(t);
Node *it = head;
if(it == NULL)
{
head = p;
len++;
}else {
cout<<"push fornt"<<endl;
p->next = it;
head = p;
len++;
}
}
void push_back(T t)
{
Node *p = new Node(t);
Node *tem = NULL, *it = head;
if(it == NULL)
{
head = p;
len++;
}else {
while(it)
{
tem = it;
it = it->next;
}
tem->next = p;
len++;
}
}
void insert(T t, int pos)
{
if(pos <= 0 || pos > len)
{
return;
}
else if(pos == 1)
{
push_front(t);
}
else
{
Node *p = new Node(t);
Node *it = head;
for (int i = 1; i < pos-1; i++)
{
it = it->next;
}
p->next = it->next;
it->next = p;
len++;
}
}
void erase(int pos)
{
Node *it = head;
if(pos <= 0 || pos > len)
{
return;
}
else if (pos == 1)
{
it = it->next;
head = it;
len--;
}
else
{
Node *tem = NULL;
for (int i = 1; i < pos-1; i++)
{
it = it->next;
}
tem = it->next;
if (pos == len)
{
delete tem;
}
else
it->next = tem->next;
len--;
}
}
void set (T t, int pos)
{
Node *p = new Node(t);
Node *it = head, *tem = NULL;
if (pos <= 0 || pos >len)
{
return;
}
else if (pos == 1)
{
it = it->next;
p->next = it;
head = p;
}
else
{
for (int i = 1; i < pos-1; i++)
{
it = it->next;
}
tem = it->next;
it->next = p;
p->next = tem->next;
}
}
int find(T t)
{
int count = 0;
Node *it = head;
for (int i = 1; i <= len; i++)
{
if (it->data == t)
{
count++;
}
it = it->next;
}
return count;
}
int size()
{
return len;
}
void travel()
{
Node *it = head;
cout << "========This List is ========== " << endl;
while (it)
{
cout << it->data <<" ";
it = it->next;
}
cout<<endl;
/*
cout << it->data << " ";
for (int i = 1; i < size(); i++)
{
it = it->next;
cout << it->data << " ";
}
*/
}
void clear()
{
Node *it = head;
for (int i = 1; i <= len; i++)
{
delete it;
it = it->next;
}
}
};
void function (List &list)
{
int t;
cout << "put data to List" <<endl;
while (cin >> t  && t !=  0)
{
list.push_front(t);
}
cout << "The List size is " << list.size() << endl;
list.travel();
//	cin.ignore(1024,'/n');
}
int main()
{
List list;

function(list);
int w, t, pos,count;
cout<<"consloe:"<<endl;
//cin.ignore(1024,'/n');
while (cout<<"please enter a commond:"&& cin >> w && w !=0 )
{
switch (w)
{
case 1:
cout << "Insert a Node at List front "<<endl;
cout << "The List size is " << list.size() << endl;
cout << "Please input a data of  t : ";
cin >> t ;
list.push_front(t);
break;
case 2:
cout << "Insert a Node at List After" << endl;
cout << "The List size is " << list.size() <<endl;
cout << "please input a data of  t : ";
cin >> t;
list.push_back(t);
break;
case 3:
cout << "Insert a Node at anyplace" << endl;
cout << "The List size is " << list.size() <<endl;
cout << "Please input two data of t and pos: ";
cin >> t >> pos;
list.insert(t, pos);
break;
case 4:
cout << "Change the Node at anyplace"<< endl;
cout << "The List size is " << list.size() <<endl;
cout << "Please input two data of t and pos: ";
cin >> t >> pos;
list.set( t, pos);
break;
case 5:
cout << "delete a Node at anyplace " << endl;
cout << "The List size is " << list.size() <<endl;
cout << "Please input a data of pos: ";
cin >> pos;
list.erase( pos);
break;
case 6:
cout << "Find the List have how much Node are equal the t " << endl;
cout << "Please input a data of t " ;
cin >> t;
count = list. find(t);
cout << "count = " << count << endl;
break;
}
list.travel();
//cin.ignore(1024,'/n');
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ struct