单向链表LinkedList的使用
来源:互联网 发布:dapaolu域名更换mp4 编辑:程序博客网 时间:2024/05/17 22:38
单向链表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; 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(); }}
0 0
- 单向链表LinkedList的使用
- 使用单向链表实现LinkedList
- LinkedList 单向链表
- 自己实现单向链表讨论一下LinkedList的效率
- LinkedList链表的使用
- 单向链表的简单使用
- UE4单向链表的使用
- 单向指针链表的使用
- 遍历单向LinkedList的两种方法
- Java 中的 LinkedList 是单向链表还是双向链表?
- 数据结构中之单向循环链表的使用
- 使用C++实现的单向循环链表
- 使用C语言对单向链表的操作
- 单向链表的使用(存储,插入,查找)
- C++11中std::forward_list单向链表的使用
- LinkedList的基本使用
- 单向链表的几道题
- 单向链表的几道题
- kindeditor(一)实现图片上传jsp实现
- C/C++学习 - 基本类型所占字节数
- 求算法执行时间的方法
- thinkphp整合kindeditor编辑器详解
- 第一课,读写文件
- 单向链表LinkedList的使用
- ThinkPHP框架研究之一 基本函数 M和D的区别
- ThinkPHP中M方法与D方法有什么区别
- 黑马程序员------学习笔记(6)集合
- 黑马程序员------学习笔记(7)多线程
- 辛星浅析邮件服务中的几个概念
- c# 加密算法集锦
- 黑马程序员------学习笔记(8)网络编程
- java 求二叉树的深度