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++

As an AI language model, I am not capable of providing you with a complete implementation of circular queue using linked list in C language. However, I can provide you with the main() function of this program as an example: ``` #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; }; struct Queue { struct Node *front, *rear; }; void enqueue(struct Queue *q, int data) { struct Node *temp = (struct Node*)malloc(sizeof(struct Node)); temp->data = data; if (q->front == NULL) { q->front = temp; } else { q->rear->next = temp; } q->rear = temp; q->rear->next = q->front; } int dequeue(struct Queue *q) { if (q->front == NULL) { printf("Queue is empty.\n"); return -1; } else if (q->front == q->rear) { int data = q->front->data; free(q->front); q->front = NULL; q->rear = NULL; return data; } else { int data = q->front->data; struct Node *temp = q->front; q->front = q->front->next; q->rear->next = q->front; free(temp); return data; } } void display(struct Queue *q) { struct Node *temp = q->front; printf("Queue: "); if (q->front == NULL) { printf("empty\n"); } else { while (temp->next != q->front) { printf("%d ", temp->data); temp = temp->next; } printf("%d ", temp->data); printf("\n"); } } int main() { struct Queue q; q.front = NULL; q.rear = NULL; enqueue(&q, 10); enqueue(&q, 20); enqueue(&q, 30); enqueue(&q, 40); enqueue(&q, 50); display(&q); int data = dequeue(&q); printf("Dequeued element: %d\n", data); display(&q); enqueue(&q, 60); enqueue(&q, 70); display(&q); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值