c++链表操作
来源:互联网 发布:移动云计算 编辑:程序博客网 时间:2024/06/16 01:58
#include <iostream>
#include <iomanip>
using
namespace
std;
template
<
class
T>
class
node
//节点
{
public
:
T data;
node *next;
};
template
<
class
T>
class
list
{
public
:
list();
void
Create();
//创建链表
bool
Empty()
const
;
//判断链表是否为空
void
InsertLast(
const
T e);
//从尾部插入一个元素
void
InsertFirst(
const
T e);
//从头部插入一个元素
void
DeleteFirst();
//从头删除一个元素
void
DeleteLast();
//从尾删除元素
void
Display()
const
;
//显示链表
node<T>* GetNode(
int
i);
//返回第i个节点
void
Reverse();
//链表逆置
bool
Find(
const
T e);
//在链表中查找某个值
~list();
//销毁链表
private
:
node<T> *head;
//头节点
};
template
<
class
T>
list<T>::list()
{
head=
new
node<T>;
head->next=NULL;
}
template
<
class
T>
void
list<T>::Create()
{
node<T> *p,*q;
p=head;
q=
new
node<T>;
cout<<
"请输入值(按'ctrl+z'停止): "
;
while
(cin>>q->data)
{
p->next=q;
p=q;
q=
new
node<T>;
}
p->next=NULL;
}
template
<
class
T>
bool
list<T>::Empty()
const
{
return
(head->next==NULL);
}
template
<
class
T>
void
list<T>::InsertFirst(
const
T e)
{
node<T> *p=
new
node<T>;
p->data=e;
p->next=head->next;
head->next=p;
}
template
<
class
T>
void
list<T>::InsertLast(
const
T e)
{
node<T> *p,*q;
p=head;
q=
new
node<T>;
q->data=e;
while
(p->next)
{
p=p->next;
}
p->next=q;
q->next=NULL;
}
template
<
class
T>
void
list<T>::DeleteFirst()
{
head->next=head->next->next;
}
template
<
class
T>
void
list<T>::DeleteLast()
{
node<T> *p;
p=head;
while
(p->next->next)
p=p->next;
p->next=NULL;
}
template
<
class
T>
void
list<T>::Display()
const
//显示链表
{
node<T> *p;
p=head->next;
while
(p)
{
cout<<p->data<<
" "
;
p=p->next;
}
}
template
<
class
T>
node<T>* list<T>::GetNode(
int
i)
//返回第i个节点
{
int
k=0;
node<T> *p;
p=head;
while
(p && k<i)
{
p=p->next;
++k;
}
return
p;
}
template
<
class
T>
void
list<T>:: Reverse()
//链表逆置
{
node<T> *p,*q;
p=head->next;
int
count=0;
while
(p)
//求链表的长度
{
p=p->next;
count++;
}
int
i,j;
i=1;
j=count;
while
(i<=count/2 && j>=count/2)
{
p=GetNode(i);
q=GetNode(j);
T temp=p->data;
p->data=q->data;
q->data=temp;
++i;
--j;
}
}
template
<
class
T>
bool
list<T>::Find(
const
T e)
//在链表中查找某个值
{
bool
flag=
false
;
node<T> *p;
p=head->next;
while
(p)
{
if
(p->data==e)
{
flag=
true
;
break
;
}
p=p->next;
}
return
flag;
}
template
<
class
T>
list<T>::~list()
{
node<T> *p;
while
(head)
{
p=head->next;
delete
head;
head=p;
}
}
int
main()
{
list<
int
> ilist;
ilist.Create();
//创建链表
ilist.Display();
//输出链表
cout<<endl;
ilist.InsertFirst(123);
//从头插入一个值
ilist.InsertLast(456);
//从尾插入一个值
ilist.Display();
cout<<endl;
if
(ilist.Find(123))
cout<<
"123 在链表中"
<<endl;
else
cout<<
"123 不在链表中"
<<endl;
ilist.DeleteFirst();
//从头删除一个值
ilist.DeleteLast();
//从尾删除一个值
ilist.Display();
cout<<endl;
ilist.Reverse();
//逆序
ilist.Display();
cout<<endl;
system
(
"pause"
);
return
0;
}
0 0
- C链表操作
- 双向链表-C/C++-多项式操作
- c/c++链表操作
- C语言 链表操作
- C语言 链表操作
- [C++]双向链表操作
- c语言链表操作
- c语言链表操作
- C语言 -- 链表操作
- C实现链表操作
- 链表操作(C++)
- c语言链表操作
- c语言链表操作
- 链表操作C语言版
- 【c】链表的操作!!!
- 链表的操作(C)
- C语言 链表的一些操作
- 链表的基本操作(C++)
- 【Ambari】Agent总体概览
- 3. 实用的脚本
- Android开发面试经——1.常见人事面试问题
- libuv网络库的TCP服务端与客户端
- A. Vitaliy and Pie(Codeforces Round #297 (Div. 2) 水题)
- c++链表操作
- ASCII码
- Distinct Substrings后缀数组
- BeautifulSoup 官方文档
- 彻底学通string.Format以及IFormattable,IFormatProvider,ICustomFormatter(1)
- 如何打开Mysql服务
- 一步一步学习ASP.NET 5 (七)- 快速把ASP.NET 5应用以Docker方式部署到Mac上
- [面试时]如何讲清楚objective-c内存管理
- 5.6 TCP prequeue