C++中链表类的实现

题目要求:

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;
}

面向对象程序设计课程作业 1. 请创建一个数据为T的链表模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该模板的正确性: 1) 用List模板定义一个List的模板对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List的模板对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List的模板对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List的模板对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值