linked list C++实现

Youtube上很好一个讲解视频:
How to Create a Linked List C++ Introduction to Linked Lists

插入第一个元素:

void insertAsFistElement(node *&head, node *&last, int data){
    node *tmp = new node;
    tmp->data = data;
    tmp->next = NULL;
    head = tmp;
    last = tmp;
}

linkedlist
在list末尾插入一个元素:

void insert(node *&head, node *&last, int data){
    if (isEmpty(head))
        insertAsFistElement(head, last, data);
    else{
        node *tmp = new node;
        tmp->data = data;
        tmp->next = NULL;
        last->next = tmp;
        last = tmp;
    }
}

insert
打印linked list:

showlist
从list头移除一个元素:

void showList(node *current){
    if(isEmpty(current))
        cout<<"the list is empty!"<<endl;
    else{
        while(current != NULL){
            cout<<current->data<<"->";
            current = current->next;
        }
        cout<<"NULL"<<endl;
    }
}

remove
remove1

将数组存入一个linkedlist:

node* createListfromArr(int arr[], int n){
    for (int i=0; i<n; i++){
        if (n == 0)
            return NULL;
        node* head = NULL;
        node* last = NULL;
        for (int i=0; i<n; i++){
            insert(head, last, arr[i]);
        }
        return head;
    }
}

createlistfromarr
实现:

#include <iostream>

using namespace std;

struct node{
    int data;
    node* next;
};

bool isEmpty(node *head);
void insertAsFistElement(node *&head, node *&last, int data);
void insert(node *&head, node *&last, int data);
void remove(node *&head, node *&last);
void showList(node *current);

bool isEmpty(node *head){
    if(head == NULL)
        return true;
    else
        return false;
}

void insertAsFistElement(node *&head, node *&last, int data){
    node *tmp = new node;
    tmp->data = data;
    tmp->next = NULL;
    head = tmp;
    last = tmp;

}

void insert(node *&head, node *&last, int data){
    if (isEmpty(head))
        insertAsFistElement(head, last, data);
    else{
        node *tmp = new node;
        tmp->data = data;
        tmp->next = NULL;
        last->next = tmp;
        last = tmp;
    }
}

void remove(node *&head, node *&last){
    if(isEmpty(head))
        cout<< "Empty link list!"<<endl;
    else if(head == last){
        delete head;
        head = NULL;
        last = NULL;
    }
    else{
        node *tmp = head;
        head = head->next;
        delete tmp;
    }

}
void showList(node *current){
    if(isEmpty(current))
        cout<<"the list is empty!"<<endl;
    else{
        while(current != NULL){
            cout<<current->data<<"->";
            current = current->next;
        }
        cout<<"NULL"<<endl;
    }
}

node* createListfromArr(int arr[], int n){
    for (int i=0; i<n; i++){
        if (n == 0)
            return NULL;
        node* head = NULL;
        node* last = NULL;
        for (int i=0; i<n; i++){
            insert(head, last, arr[i]);
        }
        return head;
    }
}

int main() {
    
    node *head = NULL;
    node *last = NULL;
    int data = 5;
    insertAsFistElement(head, last, 4);
    if (isEmpty(head))
        cout << "Empty!"<<endl;
    insert(head, last, data);
    showList(head);
    remove(head, last);
    showList(head);
    int arr[] = {5, 4, 3, 2, 1 ,0};
    node *nhead = createListfromArr(arr, 6);
    showList(nhead);

    return 0;
}

在线调试:https://goo.gl/qcaJmd


Singly linked lists in C++

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值