手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

VC++6.0程序设计系列:[2]链表-链接库

时间:2024-10-13 03:48:39

1、新建工程,注意新建的是 “静态的库皤材装肢工程” 如截图所示,这个地方一定注意。建好工程之后向工程中添加 头文件 MyList.h,源文件MyList.cpp, 如图所示。其中头文围泠惶底件中的代码如下:#ifndef __MYLIST_H__#define __MYLIST_H__struct Node { int data ; Node * next ; Node():data(0),next(0){} Node(int _d):data(_d),next(0){} Node(Node&node):data(node.data),next(node.next){} operator=(Node& node){ data = node.data ; next = node.next ; }};class MyList{private : Node * head ; int size ;public : MyList() ; MyList(int ds[] ,int n) ; ~MyList() ; void Insert(int index,Node& node) ; void Add(Node& node) ; void Remove(int index) ; void RemoveAll() ; bool Empty() const; int Size() const; Node Get(int index) const;};#endif源文件的代码如下所示:#include "MyList.h"MyList::MyList(){ this->head = new Node(0) ; this->size = 0 ;}MyList::MyList(int ds[] ,int n){ this->head = new Node(0) ; this->size = 0 ; Node * p = 0; Node * q = head; int i = 0 ; for(;i<n;i++){ p = new Node(ds[i]) ; q->next = p ; q = p ; } this->size = n ;}MyList::~MyList(){ Node * p = this->head ; Node * q ; while(p){ q = p ; p = p->next ; delete q ; } this->size = 0 ;}void MyList::Insert(int index,Node& node) { if(this->Size()<index || index < 0 ) throw "can not insert!" ; Node * p = head ; int i = 0 ; while(p && i< index){ p = p->next ; i++ ; } Node * q = new Node(node) ; q->next = p->next ; p->next = q ; this->size ++ ;}void MyList::Add(Node& node) { Insert(this->Size(),node) ;}void MyList::Remove(int index) { if(this->Size()<=index || index < 0) throw "can not remove!"; Node * p = head ; int i = 0 ; while(p && i < index){ p = p->next ; i++ ; } Node * q = p->next ; p->next = q->next; delete q ; this->size -- ;}void MyList::RemoveAll() { Node * p = head->next ; Node * q = 0; while(p){ q = p ; p = p->next ; delete q ; } this->size = 0 ;}bool MyList::Empty() const{ return this->size ;}int MyList::Size() const{ return this->size ;}Node MyList::Get(int index) const{ if(this->Size()<=index || index < 0) throw "can not get!"; Node * p = head ; int i = 0 ; while(p && i < index){ p = p->next ; i++ ; } return *(p->next) ;}

VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库

2、下面就是编译生成 库文件 My_lib.lib,这里注意不要点击执行,因为是没法执行的,很好理解,生成的是链接文件,不是直接可执行的文件。

VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库

3、以上步骤就生成了我们需要的文件 My_lib.lib ,但是怎么使用My_l坡纠课柩ib.lib这个文件呢?下面我们就介绍一下使用方法。新建控制台工程,作为测试工程。如图所示,然后讲刚才的头文件Mylist.h 导入,导入方法如下图,然后新建测试源文件 Main.cpp ,写入如下代码。此时还不能执行程序,因为我们的My_lib.lib还没有导入,导入方法很简单,我们先拷贝一份My_lib.lib到 lib 目录下面,注意 lib目录是我们自己建立的。然后按照图中所示 配置 additional library 在 link标签页做相应的设置。Main.cpp的代码如下#include <iostream>#include "MyList.h"using namespace std ;void Print(MyList & list){ for(int i =0 ; i < list.Size() ;i++) { cout<<list.Get(i).data<<" "; } cout<<endl;}int main(){ int ds [] = {1,2,3,4,5,6,7,8,9,10} ; MyList list(ds ,10) ; Print(list) ; Node p(11) ; list.Add(p) ; Print(list) ; p.data = 12 ; list.Insert(0,p) ; Print(list) ; cout<<list.Size()<<endl; return 0 ;}

VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库

4、最后我们编译执行代码,看看是否能完成链表的功能。

VC++6.0程序设计系列:[2]链表-链接库
VC++6.0程序设计系列:[2]链表-链接库
© 手抄报圈