求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!! 50
求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!求一个C++的对线性表进行插入,删除等基本...
求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!
求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!
求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!
要用类进行编写!!! 敢不敢不要粘别的网站上的 高手都跑哪去了? 展开
求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!
求一个C++的对线性表进行插入,删除等基本操作的程序,急!!!!
要用类进行编写!!! 敢不敢不要粘别的网站上的 高手都跑哪去了? 展开
7个回答
展开全部
//////////////////////我自己写的 用结构体写的 有用类写的 但是没带~~
#include<iostream>
#include<fstream>
using std::cout;
using std::cin;
using std::endl;
struct Course{
char name[20];
int Chour;
int Cterm;
};
struct List{
Course *cos;
int size;
int MaxSize;
};
//重载操作符
bool operator < (Course &cl,Course &cr)
{return cl.Chour<cr.Chour;}
bool operator > (Course &cl,Course &cr)
{return cl.Chour>cr.Chour;}
bool operator == (Course &cl,Course &cr)
{
if(strcmp(cl.name,cr.name)==0)
return true;
else return false;
}
//初始化
void Init_List(List &L)
{
L.MaxSize=10;
L.cos=new Course[L.MaxSize];
if(L.cos==NULL){
cout<<"线性表动态分配的存储空间已经用完,程序退出!"<<endl;
exit(1);
}
L.size=0;
}
//返回长度
int Get_Length(List &L)
{
return L.size;
}
//是否为空
bool Empty(List &L)
{
return L.size==0;
}
//清除所有元素
void Clear_List(List &L)
{
if(L.cos != NULL){
delete []L.cos;
L.cos=NULL;
}
L.size=0;
L.MaxSize=0;
}
//查找
int Find(List &L,Course item)
{
if(Empty(L)){
cout<<"线性表为空"<<endl;
return -2;
}
for(int i=0;i<L.size;++i)
if(item==L.cos[i])
return i;
}
//排序
void Sequence(List &L)
{
Course temp;
for(int i=0;i<L.size-2;++i)
for(int j=i;j<L.size-1;++j)
if(L.cos[i]>L.cos[j]){
temp=L.cos[i];
L.cos[i]=L.cos[j];
L.cos[j]=temp;
}
}
//插入
bool Insert_List(List &L,Course item,int pos)
{
if(L.size == L.MaxSize){
L.cos=static_cast<Course*>(realloc(L.cos,sizeof(Course)*2*L.MaxSize));
if(L.cos==NULL){
cout<<"线性表的动态空间存储空间已经用完"<<endl;
return false;
}
L.MaxSize*=2;
}
int position;
if(pos==1)
position=0;
if(pos==-1)
position=L.size-1;
if(pos==0){
for(position=0;position<L.size;++position)
if(L.cos[position]==item)
break;
}
for(int i=L.size;i>position;--i)
L.cos[i]=L.cos[i-1];
L.cos[position]=item;
++L.size;
return true;
}
//删除
bool Delete_List(List &L,int pos)
{
if(Empty(L)){
cout<<"线性表为空"<<endl;
return false;
}
for(int i=pos;i<L.size-1;++i)
L.cos[i]=L.cos[i+1];
--L.size;
if(static_cast<float>(L.size/L.MaxSize)<0.4 && L.MaxSize>10){
L.cos=static_cast<Course*>(realloc(L.cos,sizeof(Course)*L.MaxSize/2));
L.MaxSize/=2;
}
return true;
}
//修改
void Revise(List &L,Course item,int pos)
{
L.cos[pos]=item;
}
//遍历所有元素输出
void print(List &L)
{
if(Empty(L)){
cout<<"线性表为空"<<endl;
return ;
}
for(int i=0;i<L.size;++i)
cout<<L.cos[i].name<<"\t"<<L.cos[i].Chour<<"\t"<<L.cos[i].Cterm<<endl;
}
int main()
{
List list;
Init_List(list);
Course course;
std::ifstream in("a.txt");
int j=0;
int seek=0;
while(!in.eof()){
in>>course.name>>course.Chour>>course.Cterm;
list.cos[j++]=course;
++list.size;
}
while(1)
{
cout<<" 请输入要使用的功能: "<<endl
<<"1. 排序 2. 插入 "<<endl
<<"3. 清空 4. 删除 "<<endl
<<"5. 查找 6. 修改 "<<endl
<<"7. 输出 "<<endl;
int i;
cin>>i;
switch(i)
{
case 1:
Sequence(list);
break;
case 2:
cout<<"请输入:课程名称 课时 开课学期"<<endl;
cin>>course.name>>course.Chour>>course.Cterm;
Insert_List(list,course,0);
break;
case 3:
Clear_List(list);
break;
case 4:
cout<<"请输入要查找的课程名"<<endl;
cin>>course.name;
Delete_List(list,Find(list,course));
break;
case 5:
cout<<"请输入要查找的课程名"<<endl;
cin>>course.name;
seek=Find(list,course);
cout<<list.cos[i].name
<<"\t"<<list.cos[i].Chour
<<"\t"<<list.cos[i].Cterm<<endl;
break;
case 6:
Course c1,c2;
cout<<"请输入要查找的课程名";cin>>c1.name;
cout<<"请输入修改后的信息";cin>>c2.name>>c2.Chour>>c2.Cterm;
Revise(list,c2,Find(list,c1));
case 7:
print(list);
break;
default:
cout<<"错误编号"<<endl;
}
}
return 0;
}
//////再c++中没必要去写链表 线性表啥的 数据结构中讲得只不过是让我们知道它的写法 算法 我们c++中的模板库提供了非常强大的功能
所以用结构体也没什么 不过我喜欢用类 因为那样可以锻炼自己的编程能力
#include<iostream>
#include<fstream>
using std::cout;
using std::cin;
using std::endl;
struct Course{
char name[20];
int Chour;
int Cterm;
};
struct List{
Course *cos;
int size;
int MaxSize;
};
//重载操作符
bool operator < (Course &cl,Course &cr)
{return cl.Chour<cr.Chour;}
bool operator > (Course &cl,Course &cr)
{return cl.Chour>cr.Chour;}
bool operator == (Course &cl,Course &cr)
{
if(strcmp(cl.name,cr.name)==0)
return true;
else return false;
}
//初始化
void Init_List(List &L)
{
L.MaxSize=10;
L.cos=new Course[L.MaxSize];
if(L.cos==NULL){
cout<<"线性表动态分配的存储空间已经用完,程序退出!"<<endl;
exit(1);
}
L.size=0;
}
//返回长度
int Get_Length(List &L)
{
return L.size;
}
//是否为空
bool Empty(List &L)
{
return L.size==0;
}
//清除所有元素
void Clear_List(List &L)
{
if(L.cos != NULL){
delete []L.cos;
L.cos=NULL;
}
L.size=0;
L.MaxSize=0;
}
//查找
int Find(List &L,Course item)
{
if(Empty(L)){
cout<<"线性表为空"<<endl;
return -2;
}
for(int i=0;i<L.size;++i)
if(item==L.cos[i])
return i;
}
//排序
void Sequence(List &L)
{
Course temp;
for(int i=0;i<L.size-2;++i)
for(int j=i;j<L.size-1;++j)
if(L.cos[i]>L.cos[j]){
temp=L.cos[i];
L.cos[i]=L.cos[j];
L.cos[j]=temp;
}
}
//插入
bool Insert_List(List &L,Course item,int pos)
{
if(L.size == L.MaxSize){
L.cos=static_cast<Course*>(realloc(L.cos,sizeof(Course)*2*L.MaxSize));
if(L.cos==NULL){
cout<<"线性表的动态空间存储空间已经用完"<<endl;
return false;
}
L.MaxSize*=2;
}
int position;
if(pos==1)
position=0;
if(pos==-1)
position=L.size-1;
if(pos==0){
for(position=0;position<L.size;++position)
if(L.cos[position]==item)
break;
}
for(int i=L.size;i>position;--i)
L.cos[i]=L.cos[i-1];
L.cos[position]=item;
++L.size;
return true;
}
//删除
bool Delete_List(List &L,int pos)
{
if(Empty(L)){
cout<<"线性表为空"<<endl;
return false;
}
for(int i=pos;i<L.size-1;++i)
L.cos[i]=L.cos[i+1];
--L.size;
if(static_cast<float>(L.size/L.MaxSize)<0.4 && L.MaxSize>10){
L.cos=static_cast<Course*>(realloc(L.cos,sizeof(Course)*L.MaxSize/2));
L.MaxSize/=2;
}
return true;
}
//修改
void Revise(List &L,Course item,int pos)
{
L.cos[pos]=item;
}
//遍历所有元素输出
void print(List &L)
{
if(Empty(L)){
cout<<"线性表为空"<<endl;
return ;
}
for(int i=0;i<L.size;++i)
cout<<L.cos[i].name<<"\t"<<L.cos[i].Chour<<"\t"<<L.cos[i].Cterm<<endl;
}
int main()
{
List list;
Init_List(list);
Course course;
std::ifstream in("a.txt");
int j=0;
int seek=0;
while(!in.eof()){
in>>course.name>>course.Chour>>course.Cterm;
list.cos[j++]=course;
++list.size;
}
while(1)
{
cout<<" 请输入要使用的功能: "<<endl
<<"1. 排序 2. 插入 "<<endl
<<"3. 清空 4. 删除 "<<endl
<<"5. 查找 6. 修改 "<<endl
<<"7. 输出 "<<endl;
int i;
cin>>i;
switch(i)
{
case 1:
Sequence(list);
break;
case 2:
cout<<"请输入:课程名称 课时 开课学期"<<endl;
cin>>course.name>>course.Chour>>course.Cterm;
Insert_List(list,course,0);
break;
case 3:
Clear_List(list);
break;
case 4:
cout<<"请输入要查找的课程名"<<endl;
cin>>course.name;
Delete_List(list,Find(list,course));
break;
case 5:
cout<<"请输入要查找的课程名"<<endl;
cin>>course.name;
seek=Find(list,course);
cout<<list.cos[i].name
<<"\t"<<list.cos[i].Chour
<<"\t"<<list.cos[i].Cterm<<endl;
break;
case 6:
Course c1,c2;
cout<<"请输入要查找的课程名";cin>>c1.name;
cout<<"请输入修改后的信息";cin>>c2.name>>c2.Chour>>c2.Cterm;
Revise(list,c2,Find(list,c1));
case 7:
print(list);
break;
default:
cout<<"错误编号"<<endl;
}
}
return 0;
}
//////再c++中没必要去写链表 线性表啥的 数据结构中讲得只不过是让我们知道它的写法 算法 我们c++中的模板库提供了非常强大的功能
所以用结构体也没什么 不过我喜欢用类 因为那样可以锻炼自己的编程能力
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
展开全部
根据elem[loc-1]=el可知,loc表示的是以1为开头的位置,如当loc=3时,表示的是在第3个位置处插入数据.即elem[2]处.
如1
2
4
5在第3个位置即(loc=3)插入6,即1
2
6
4
5,
如1
2
4
5在第3个位置即(loc=3)插入6,即1
2
6
4
5,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看看这个如何:
#include
"stdafx.h"
#include
<iostream>
using
namespace
std;
template<class
T>
struct
Node
{
T
data;
Node
<T>
*next;
};
template
<class
T>
class
LinkList
{
public:
LinkList(T
a[],
int
n);
LinkList();
~LinkList(
);
bool
Add(const
T
&);
void
Delete(const
T
&);
void
Print();
private:
Node<T>
*first;
};
template
<class
T>
LinkList<T>::LinkList()
{
first
=
new
Node<T>;
first->next
=
NULL;
}
template
<class
T>
LinkList<T>::LinkList(T
a[],
int
n)
{
first
=
new
Node<T>;
first->next
=
NULL;
for
(int
i
=
0;
i
<
n;
i++)
{
Node<T>
*n;
n
=
new
Node<T>;
n->data
=
a[i];
n->next
=
first->next;
first->next
=
n;
}
}
template
<class
T>
LinkList<T>::
~LinkList()
{
Node
<T>
*p,*q;
p
=
first;
while
(p)
{
q
=
p;
p
=
p->next;
delete
q;
}
}
template
<class
T>
bool
LinkList<T>::Add(const
T
&data)
{
Node<T>
*c=
first->next;
while
(c)
{
if
(c->data
==
data)
return
false;
c
=
c->next;
}
Node<T>
*n;
n
=
new
Node<T>;
n->data
=
data;
n->next
=
first->next;
first->next
=
n;
return
true;
}
template
<class
T>
void
LinkList<T>::Delete(const
T
&data)
{
Node
<T>
*p,
*q,
*t;
p
=
first->next;
while
(p)
{
if
(p->data
==
data)
{
if
(first->next
==
p)
first->next
=
p->next;
else
t->next
=
p->next;
q
=
p;
p
=
p->next;
delete
q;
continue;
}
t
=
p;
p
=
p->next;
}
}
template
<class
T>
void
LinkList<T>::Print()
{
Node<T>
*c
=
first->next;
while
(c)
{
cout
<<
c->data
<<
endl;
c
=
c->next;
}
cout
<<
endl;
}
int
main
()
{
int
r[]
=
{1,
3,
6,
7,
12,
15};
int
m[10]
=
{2,
4,
6,
8,10,
12,
14,
16,
18,
20};
LinkList<int>
A(r,
6);
LinkList<int>
B(m,
10);
A.Print();
B.Print();
A.Delete(15);
B.Delete(6);
B.Delete(12);
A.Print();
B.Print();
A.Add(88);
B.Add(66);
B.Add(2);
A.Print();
B.Print();
return
0;
}
#include
"stdafx.h"
#include
<iostream>
using
namespace
std;
template<class
T>
struct
Node
{
T
data;
Node
<T>
*next;
};
template
<class
T>
class
LinkList
{
public:
LinkList(T
a[],
int
n);
LinkList();
~LinkList(
);
bool
Add(const
T
&);
void
Delete(const
T
&);
void
Print();
private:
Node<T>
*first;
};
template
<class
T>
LinkList<T>::LinkList()
{
first
=
new
Node<T>;
first->next
=
NULL;
}
template
<class
T>
LinkList<T>::LinkList(T
a[],
int
n)
{
first
=
new
Node<T>;
first->next
=
NULL;
for
(int
i
=
0;
i
<
n;
i++)
{
Node<T>
*n;
n
=
new
Node<T>;
n->data
=
a[i];
n->next
=
first->next;
first->next
=
n;
}
}
template
<class
T>
LinkList<T>::
~LinkList()
{
Node
<T>
*p,*q;
p
=
first;
while
(p)
{
q
=
p;
p
=
p->next;
delete
q;
}
}
template
<class
T>
bool
LinkList<T>::Add(const
T
&data)
{
Node<T>
*c=
first->next;
while
(c)
{
if
(c->data
==
data)
return
false;
c
=
c->next;
}
Node<T>
*n;
n
=
new
Node<T>;
n->data
=
data;
n->next
=
first->next;
first->next
=
n;
return
true;
}
template
<class
T>
void
LinkList<T>::Delete(const
T
&data)
{
Node
<T>
*p,
*q,
*t;
p
=
first->next;
while
(p)
{
if
(p->data
==
data)
{
if
(first->next
==
p)
first->next
=
p->next;
else
t->next
=
p->next;
q
=
p;
p
=
p->next;
delete
q;
continue;
}
t
=
p;
p
=
p->next;
}
}
template
<class
T>
void
LinkList<T>::Print()
{
Node<T>
*c
=
first->next;
while
(c)
{
cout
<<
c->data
<<
endl;
c
=
c->next;
}
cout
<<
endl;
}
int
main
()
{
int
r[]
=
{1,
3,
6,
7,
12,
15};
int
m[10]
=
{2,
4,
6,
8,10,
12,
14,
16,
18,
20};
LinkList<int>
A(r,
6);
LinkList<int>
B(m,
10);
A.Print();
B.Print();
A.Delete(15);
B.Delete(6);
B.Delete(12);
A.Print();
B.Print();
A.Add(88);
B.Add(66);
B.Add(2);
A.Print();
B.Print();
return
0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//********List.h**************
#ifndef _LIST_H
#define _LIST_H
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef float ElemType;
class List{
public:
void InitList();
void DestroyList();
//void ClearList();
//bool ListEmpty();
Status ListLength();
void GetElem(int i,ElemType &e);
Status PriorElem(ElemType cur_e,ElemType & pre_e);
Status NextElem(ElemType cur_e,ElemType & next_e);
void ListInsert(int i,ElemType e);
void ListDelete(int i,ElemType & e);
private:
struct SqList{
ElemType * elem;
int length;
int listsize;
};
SqList L;
};
#endif //***********List.h
//*****List.cpp*********
#include "List.h"
#include <iostream>
using namespace std;
void List::InitList()
{
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L.elem) exit(0);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
}
void List::DestroyList()
{
free(L.elem);
}
Status List::ListLength()
{
return L.length;
}
void List::GetElem(int i,float & e)
{
e = L.elem[i-1];
}
Status List::PriorElem(float cur_e,float & pre_e)
{
int i;
for(i=0;i<L.length;i++)
if(L.elem[i] == cur_e) break;
if(i != 0 && i!= L.length)
{
pre_e = L.elem[i-1];
return 1;
}
else return 0;
}
Status List::NextElem(float cur_e,float & next_e)
{
int i;
for(i=0;i<L.length;i++)
if(L.elem[i] == cur_e) break;
if(i >= L.length - 1) return 0;
else
{
next_e = L.elem[i+1];
return 1;
}
}
void List::ListInsert(int i,float e)
{
ElemType *p,*q;
if(i < 1 || i > L.length + 1) cout << "ERROR" << endl;
if(L.length >= L.listsize)
{
L.elem = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT)*sizeof(ElemType));
if(!L.elem) exit(0);
L.listsize += LISTINCREMENT;
}
p = L.elem + i - 1;
for(q=L.elem + L.length - 1;q >= p;q--) *(q + 1) = *q;
*p = e;
L.length ++;
}
void List::ListDelete(int i,float & e)
{
if(i < 1 || i > L.length) cout << "ERROR" << endl;
ElemType *p,*q;
p = L.elem + i - 1;
e = *p;
for(q=p+1;q<=L.elem + L.length - 1;q++) *(q - 1) = *q;
L.length --;
}
//********List.cpp*********
#ifndef _LIST_H
#define _LIST_H
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef float ElemType;
class List{
public:
void InitList();
void DestroyList();
//void ClearList();
//bool ListEmpty();
Status ListLength();
void GetElem(int i,ElemType &e);
Status PriorElem(ElemType cur_e,ElemType & pre_e);
Status NextElem(ElemType cur_e,ElemType & next_e);
void ListInsert(int i,ElemType e);
void ListDelete(int i,ElemType & e);
private:
struct SqList{
ElemType * elem;
int length;
int listsize;
};
SqList L;
};
#endif //***********List.h
//*****List.cpp*********
#include "List.h"
#include <iostream>
using namespace std;
void List::InitList()
{
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L.elem) exit(0);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
}
void List::DestroyList()
{
free(L.elem);
}
Status List::ListLength()
{
return L.length;
}
void List::GetElem(int i,float & e)
{
e = L.elem[i-1];
}
Status List::PriorElem(float cur_e,float & pre_e)
{
int i;
for(i=0;i<L.length;i++)
if(L.elem[i] == cur_e) break;
if(i != 0 && i!= L.length)
{
pre_e = L.elem[i-1];
return 1;
}
else return 0;
}
Status List::NextElem(float cur_e,float & next_e)
{
int i;
for(i=0;i<L.length;i++)
if(L.elem[i] == cur_e) break;
if(i >= L.length - 1) return 0;
else
{
next_e = L.elem[i+1];
return 1;
}
}
void List::ListInsert(int i,float e)
{
ElemType *p,*q;
if(i < 1 || i > L.length + 1) cout << "ERROR" << endl;
if(L.length >= L.listsize)
{
L.elem = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT)*sizeof(ElemType));
if(!L.elem) exit(0);
L.listsize += LISTINCREMENT;
}
p = L.elem + i - 1;
for(q=L.elem + L.length - 1;q >= p;q--) *(q + 1) = *q;
*p = e;
L.length ++;
}
void List::ListDelete(int i,float & e)
{
if(i < 1 || i > L.length) cout << "ERROR" << endl;
ElemType *p,*q;
p = L.elem + i - 1;
e = *p;
for(q=p+1;q<=L.elem + L.length - 1;q++) *(q - 1) = *q;
L.length --;
}
//********List.cpp*********
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看看这个如何:
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T>
struct Node
{
T data;
Node <T> *next;
};
template <class T>
class LinkList
{
public:
LinkList(T a[], int n);
LinkList();
~LinkList( );
bool Add(const T &);
void Delete(const T &);
void Print();
private:
Node<T> *first;
};
template <class T>
LinkList<T>::LinkList()
{
first = new Node<T>;
first->next = NULL;
}
template <class T>
LinkList<T>::LinkList(T a[], int n)
{
first = new Node<T>;
first->next = NULL;
for (int i = 0; i < n; i++)
{
Node<T> *n;
n = new Node<T>;
n->data = a[i];
n->next = first->next;
first->next = n;
}
}
template <class T>
LinkList<T>:: ~LinkList()
{
Node <T> *p,*q;
p = first;
while (p)
{
q = p;
p = p->next;
delete q;
}
}
template <class T>
bool LinkList<T>::Add(const T &data)
{
Node<T> *c= first->next;
while (c)
{
if (c->data == data)
return false;
c = c->next;
}
Node<T> *n;
n = new Node<T>;
n->data = data;
n->next = first->next;
first->next = n;
return true;
}
template <class T>
void LinkList<T>::Delete(const T &data)
{
Node <T> *p, *q, *t;
p = first->next;
while (p)
{
if (p->data == data)
{
if (first->next == p)
first->next = p->next;
else
t->next = p->next;
q = p;
p = p->next;
delete q;
continue;
}
t = p;
p = p->next;
}
}
template <class T>
void LinkList<T>::Print()
{
Node<T> *c = first->next;
while (c)
{
cout << c->data << endl;
c = c->next;
}
cout << endl;
}
int main ()
{
int r[] = {1, 3, 6, 7, 12, 15};
int m[10] = {2, 4, 6, 8,10, 12, 14, 16, 18, 20};
LinkList<int> A(r, 6);
LinkList<int> B(m, 10);
A.Print();
B.Print();
A.Delete(15);
B.Delete(6);
B.Delete(12);
A.Print();
B.Print();
A.Add(88);
B.Add(66);
B.Add(2);
A.Print();
B.Print();
return 0;
}
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T>
struct Node
{
T data;
Node <T> *next;
};
template <class T>
class LinkList
{
public:
LinkList(T a[], int n);
LinkList();
~LinkList( );
bool Add(const T &);
void Delete(const T &);
void Print();
private:
Node<T> *first;
};
template <class T>
LinkList<T>::LinkList()
{
first = new Node<T>;
first->next = NULL;
}
template <class T>
LinkList<T>::LinkList(T a[], int n)
{
first = new Node<T>;
first->next = NULL;
for (int i = 0; i < n; i++)
{
Node<T> *n;
n = new Node<T>;
n->data = a[i];
n->next = first->next;
first->next = n;
}
}
template <class T>
LinkList<T>:: ~LinkList()
{
Node <T> *p,*q;
p = first;
while (p)
{
q = p;
p = p->next;
delete q;
}
}
template <class T>
bool LinkList<T>::Add(const T &data)
{
Node<T> *c= first->next;
while (c)
{
if (c->data == data)
return false;
c = c->next;
}
Node<T> *n;
n = new Node<T>;
n->data = data;
n->next = first->next;
first->next = n;
return true;
}
template <class T>
void LinkList<T>::Delete(const T &data)
{
Node <T> *p, *q, *t;
p = first->next;
while (p)
{
if (p->data == data)
{
if (first->next == p)
first->next = p->next;
else
t->next = p->next;
q = p;
p = p->next;
delete q;
continue;
}
t = p;
p = p->next;
}
}
template <class T>
void LinkList<T>::Print()
{
Node<T> *c = first->next;
while (c)
{
cout << c->data << endl;
c = c->next;
}
cout << endl;
}
int main ()
{
int r[] = {1, 3, 6, 7, 12, 15};
int m[10] = {2, 4, 6, 8,10, 12, 14, 16, 18, 20};
LinkList<int> A(r, 6);
LinkList<int> B(m, 10);
A.Print();
B.Print();
A.Delete(15);
B.Delete(6);
B.Delete(12);
A.Print();
B.Print();
A.Add(88);
B.Add(66);
B.Add(2);
A.Print();
B.Print();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询