您的位置:首页 > 编程语言 > C语言/C++

单向链表LinkedList的使用

2015-04-02 02:33 597 查看

单向链表LinkedList的使用

为了理解链表的作用,以及对于node class的reuse,将Node和LinkedList分开写成class文件。

LinkedList 用来完成链表的结构框架,其中包括

void addToStart(Node*);

void addToEnd(Node*);

void printList();

bool removeFromStart();

bool removeFromEnd();

void removeNodeFromList(int);

void removeNodeFromList(string);

在链表头部增加节点

在链表尾部增加节点

显示链表的内容

移除链表头部节点

移除链表尾部节点

移除指定内容的节点

cpp文件包括

Node.h

LinkedList.h

LinkedList.cpp

< 第一次写的过程中遇到pointer being freed was not allocated 的错误,原因是将 temp = head, delete head后delete temp。head 与temp 同指向一块内存区域,当将head的内存区域释放后,temp成为空指针。>

Node.h

using namespace std;

class Node{
//申明LinkedList为友元类,在Node class中可以直接调用LinkedList中成员
friend class LinkedList;

//无参构造函数,有参构造函数
Node(){
this->next = NULL;
};
//可以根据需求改变node节点中变量,如多个int 变量
Node(string name, int no){
this->itemName = name;
this->itemNo = no;
this->next = NULL;
}
private:
string itemName;
int itemNo;
Node* next;
};


LinkedList.h

ifndef __ShopList__LinkedList__ #define __ShopList__LinkedList__ #include "Node.h"

class LinkedList:public Node{
public:
LinkedList();
~LinkedList();
int size() const;
void addToStart(Node*);
void addToEnd(Node*);
void printList();
bool removeFromStart();
bool removeFromEnd();
void removeNodeFromList(int);
void removeNodeFromList(string);

private:
Node* head; //指向链表头
Node* curr; //指向链表当前的node,相当于变量i历遍数组
Node* temp; //临时变量链表指针
int mySize; //记录链表中node数量

};

endif /* defined(__ShopList__LinkedList__) */ #include "LinkedList.h"


//

// LinkedList.cpp

// ShopList

#include "LinkedList.h"
LinkedList::LinkedList(){
head = NULL;
curr = NULL;
temp = NULL;
mySize = 0;
//全部初始化为0
}

LinkedList::~LinkedList(){

}

int LinkedList::size() const{
return mySize; //返回链表的结点个数
}

void LinkedList::addToStart(Node *n){
//如果链表头是空值,直接将当前Node 指针指向链表头head
//如果链表头是非空值,将原来链表头右移一位,即n->next = head,再将当前 Node n指向head
if (head != NULL){
n->next = head;
head = n;
}else{
head = n;
}
printList();
mySize++;
}

void LinkedList::addToEnd(Node *n){
//    如果 head != null, go to the end of the list, use the next of last node pointing to new node
//    如果 head == null, head = new node
if (head != NULL) {
curr = head;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = n;
}else{
head = n;
}
printList();
mySize++;
}

void LinkedList::printList(){
curr = head;
if(head == NULL)
cout<<"Shopping List is empty"<<endl;
cout<<"Item NO\tItem Name"<<endl;
while(curr != NULL){
cout<<curr->itemNo<<"\t"<<curr->itemName<<endl;
curr = curr->next;
}
}

bool LinkedList::removeFromStart(){
if(head == NULL)
return false;
if(head->next == NULL){
head = head->next;
}else{
temp = head->next;
delete head;
head = temp;
}
mySize--;
printList();
return true;
}

bool LinkedList::removeFromEnd(){
if(head == NULL)            //when head is not null
return false;
//go to the last node in the list
curr = head;
temp = head;
while (curr->next != NULL){
//para@ temp current node before coming to the next one
temp = curr;
curr = curr->next;
}
if(curr == head)
head = head->next;
else{
temp->next = NULL;
delete curr;
}
mySize--;
printList();
return true;
}

void LinkedList::removeNodeFromList(int n){
temp = head;
curr = head;
if (head == NULL) return;

4000
if (head->itemNo == n) {
head = curr->next;
delete temp;
temp = head;
mySize--;
printList();
return;
}
//    go through the list to find delete data
while(curr != NULL && curr->itemNo != n){
//    para@ temp current node before coming to the next one
temp = curr;
curr = curr->next;
}
/ when curr = null it means delete element is not in the list
if (curr == NULL){
cout<<"Item not found"<<endl;
}else{

temp->next = curr->next;
delete curr;
mySize--;
printList();
}

}
void LinkedList::removeNodeFromList(string s){
temp = head;
curr = head;
if (head == NULL) return;
if (head->itemName == s) {
head = curr->next;
delete temp;
temp = head;
printList();
mySize--;
return;
}
//    go through the list to find delete data
while(curr != NULL && curr->itemName != s){
// para@ temp current node before coming to the next one
temp = curr;
curr = curr->next;
}
//when curr = null it means delete element is not in the list
if (curr == NULL){
cout<<"Item not found"<<endl;
}else{

temp->next = curr->next;
delete curr;
mySize--;
printList();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linkedlist cpp