单向链表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
- Java基础之集合框架——使用真的的链表LinkedList<>(TryPolyLine)
- Java基础之泛型——使用泛型链表类型(TryGenericLinkedList)
- 单向链表 (Singly linked list) 的C++实现
- leetcode解题之206 # Reverse Linked List Java版 (使用头插法反转链表)
- c# 链表(LinkedList)使用中的问题(.net framework 2.0)
- PHP 双链表(SplDoublyLinkedList)简介和使用实例
- 如何判断一个单向链表是否为回文链表(Palindrome Linked List)
- c# 链表(LinkedList)使用
- leetcode 92. Reverse Linked List II 反转链表 + 头插入反转链表(或者使用栈)
- 链表的使用 (usage of Linked list)
- list.c - A linked list by C --- C语言实现的单向链表
- list.c - A linked list by C --- C语言实现的单向链表
- 链表-Reverse Linked List(反转单向链表)
- 两两反转单向链表 (reverse a singly linked list in pair ) [#22]
- Linked List Cycle II--找出单向链表中环的起点
- 双向链表LinkedList使用
- LinkedList链表的使用
- C++11中std::forward_list单向链表的使用
- 单向链表仿LinkedList