单向链表LinkedList的使用

本文详细介绍单向链表LinkedList的实现与应用,包括节点增删等核心操作,并通过实例演示如何解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单向链表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();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值