C++中链表类的实现
来源:互联网 发布:mit python 编辑:程序博客网 时间:2024/06/15 22:49
题目要求:
1、定义一个由int型元素所构成的线性表类LinearList,它有下面的成员函数:
bool insert(int x, int pos); //在位置pos之后插入一个元素x。//pos为0时,在第一个元素之前插入。
//操作成功时返回true,否则返回false。
bool remove(int &x, int pos); //删除位置pos处的元素。
//操作成功时返回true,否则返回false。
int element(int pos) const; //返回位置pos处的元素。
int search(int x) const;
//查找值为x的元素,返回元素的位置(第一个元素的位置为1)。未找到时返回0。
int length() const; //返回元素个数。
第一次自己写的代码啊,虽然中间还是有汪同学的帮忙= =、
#include <iostream>
using namespace std;
struct Node
{
int element;
Node *next;
};
class LinerList{
Node *head;
int len ;
public:
LinerList(){len=0;head = NULL;}
bool insert(int x,int pos);
bool remove(int &x,int pos);
int element(int pos) const;
int search(int x) const;
int length()const;
void show();
};
bool LinerList::insert(int x,int pos){
if (pos<0)
{
cout<<"请输入正确的pos值(需要>0)"<<endl;
return false;
}else if (pos == 0)
{
Node *p = new Node;
p->element = x;
if (head !=NULL)
{
p->next = head;
head = p;
p->next = NULL;
len++;
return true;
}else{
head = p;
p->next=NULL;
len++;
return true;
}
}else if (pos>0&&pos<len)
{
if (head !=NULL)
{
Node *p = new Node;
p->element = x;
Node *p1 = head,*p2=head;
for (int i = 0;i<pos;i++)
{
p2 = p1;
p1 = p1->next;
}
p2->next = p;
p->next = p1;
len++;
return true;
}else{
Node *p = new Node;
p->element = x;
head = p;
p->next=NULL;
len++;
return true;
}
}else {
Node *p = new Node;
p->element = x;
Node *p1 = head;
while (p1->next!=NULL)
{
p1 = p1->next;
}
p1->next = p;
p->next = NULL;
len ++;
return true;
}
}
bool LinerList::remove(int &x,int pos){
if (pos <0||pos>=len)
{
cout<<"请输入正确的pos值!"<<endl;
return false;
}else if (pos ==0)
{
Node *p = head;
x = p->element;
head = p->next;
len--;
return true;
}else if(pos>0&&pos<len-1){
Node *p1 = head,*p2= head;
for (int i = 0;i<pos;i++)
{
p2 = p1;
p1 = p1->next;
}
p2->next = NULL;
x = p1->element;
p1 = p1->next;
p2->next = p1;
len--;
return true;
}else
{
Node *p1 = head,*p2 = head;
for (int i = 0;i<pos;i++)
{
p2 = p1;
p1 = p1->next;
}
p2->next = NULL;
len --;
return true;
}
}
int LinerList::element(int pos) const{
if (pos>=len||pos<0)
{
cout<<"请输入正确的pos值"<<endl;
return -1;
}else{
Node *p1 = head;
for (int i = 0;i<pos;i++)
{
p1 = p1->next;
}
return p1->element;
}
}
int LinerList::search(int x)const{
Node *p = head;
for (int i = 0;i<len;i++)
{
if (p->element ==x)
{
return i+1;
}
p = p->next;
}
return 0;
}
int LinerList::length()const{
return len ;
}
void LinerList::show(){
Node *p = head;
while (p!=NULL)
{
cout<<p->element<<" ";
p = p->next;
}
}
void main(){
LinerList llist ;
llist.insert(1,0);
llist.insert(2,1);
llist.insert(3,1);
llist.insert(4,5);
llist.show();
cout<<llist.length()<<" ";
int x = llist.search(2);
cout<<x;
}
- [C/C++]汉诺塔的实现
- 堆栈的C实现
- C的类实现
- c库函数的实现
- c实现的urlencode
- Base64的 c实现
- C实现的泛型栈
- 哈希表的C实现
- C中泛型的实现
- 哈希表的C实现
- C实现的五子棋
- C实现的俄罗斯方块
- C泛型栈的实现
- C 实现的Music
- c 双链表的实现
- 单链表的C实现
- 栈的C实现
- C/C++的实现
- 反编译华为U8825D“framework-res.apk”出现的错误提示(1)
- JavaScript 大括号和小括号(...)()
- FPGA最新资料大全
- Mysql数据库查询字段值区分大小写问题
- UI2项目有感
- C++中链表类的实现
- CheckBoxList动态绑定 取值_ASPNET(转)
- WebKit内核加载渲染网页的过程
- ATL组件注册表中显示乱码的问题
- matlab遍历文件夹 .
- 反编译华为U8825D“framework-res.apk”出现的错误提示(2)
- 字符串(三)
- knewton适应性学习白皮书(2)
- 如何修改数据库的sid