标签: linked-list

为什么我不能在 C++ 中打印 NULL 地址

假设我有一个链表类,如下所示。

struct Node {
    int data;
    Node* rptr;
};

class LinkedList
{
public:
    Node* curr;
    Node* prev;
    Node* first;
    Node node;

    LinkedList()
    {
        curr = NULL;
        prev = NULL;
        first = NULL;
        node.data = NAN;
        node.rptr = NULL;
    }

    void push(int);
    void pop();
};

void LinkedList::push(int data) 
{
    curr = new Node;
    curr->data = data;
    curr->rptr = NULL;

    if (first == NULL)
    {
        prev = first = curr;
    }
    else
    {
        prev->rptr = curr;
        curr->data = data;
        prev = curr; …
Run Code Online (Sandbox Code Playgroud)

c++ pointers linked-list

-1
推荐指数
1
解决办法
59
查看次数

Deque中remove()和pop()的区别

这段代码:

    Deque<String> list = new LinkedList<>();
    list.push("first");
    list.push("second");
    list.push("third");
    
    System.out.println(list.remove());
Run Code Online (Sandbox Code Playgroud)

相当于:

    Deque<String> list = new LinkedList<>();
    list.push("first");
    list.push("second");
    list.push("third");
    
    System.out.println(list.pop());
Run Code Online (Sandbox Code Playgroud)

pop() 和remove() 都删除第一个元素(head)。那么,采用两种不同方法的原因是什么?

java queue collections linked-list deque

-1
推荐指数
1
解决办法
1244
查看次数

如何访问链表的空指针(节点)?

此方法应该在链接列表的末尾附加一个节点。该方法循环直到到达末尾,即空指针。但是当我尝试将空指针更改为一个值时,它崩溃了。我应该如何解决这个问题?(节点指针有一个整数数据和另一个当前节点指向的节点变量)。

void appendItem(LinkedList* list, int value)
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp = list->head;

    while(temp != NULL)
    {
        temp = temp->next;
    }

    temp->data = value;
    temp->next = NULL;
}
Run Code Online (Sandbox Code Playgroud)

c linked-list append singly-linked-list function-definition

-1
推荐指数
1
解决办法
59
查看次数

将第 k 个元素打印到链表中的最后一个元素的算法

我正在阅读 Cracking the Coding Interview 并做练习题,但我一直坚持这个问题:

“实现一种算法来查找单向链表的第 k 个到最后一个元素。”

我的递归函数没有返回任何东西,我不知道为什么。在递归函数中,我采用了 3 个参数。K将是我们想要找出的最后一个元素的位置。Node* temp将是头节点, int i 将保留最后一个节点的元素计数。

#include<bits/stdc++.h>
using namespace std;

class Node
{
    int data;
    Node* next;
    public:
    Node(){
        data=0;
        next=NULL;
    }

    friend class LinkedList;
};

class LinkedList
{
    public:
    Node* head;
    public:
    LinkedList(){
        head=NULL;
    }

    void append(int data){
        Node* temp= new Node();
        temp->data=data;
        temp->next=NULL;
    
        if (head==NULL){
            head=temp;
            
        }
        else{
        Node* ptr=head;
        while(ptr->next!=NULL){
            ptr=ptr->next;
        }
        ptr->next=temp;
        }

    }

    void display(){
        Node* ptr=head;
        if(head==NULL){
            cout<<"List is empty"<<endl; …
Run Code Online (Sandbox Code Playgroud)

c++ linked-list singly-linked-list

-1
推荐指数
1
解决办法
96
查看次数

使用指向指针的指针访问结构成员

我有一个如下所示的程序,当我尝试使用指向指针的指针访问结构成员时,它表示表达式必须具有指向类类型的指针。请告诉我如何使用指向指针的指针访问结构对象的数据元素

#include "stdafx.h"
#include<iostream>
struct Node{
    int data;
    Node* next;
};

void addnode(Node** node, int val)
{
    if (node == NULL){
        *node = new Node();
        *node->data = val;

    }
}



int _tmain(int argc, _TCHAR* argv[])
{
    Node* node;
    addnode(&node, 10);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ struct linked-list singly-linked-list function-definition

-1
推荐指数
1
解决办法
1048
查看次数

为什么我的 C 程序没有给出控制台输出?

我是编码初学者,最近开始学习 C。

我最近学习概念后自己编写了这段代码。我需要一些可以优化我的代码的建议。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

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

//function to add node at the front
void push(node** head_ref, int new_data){
    //allocate node
    node* new_node = (node*)malloc(sizeof(node));

    //put in new data
    new_node->data = new_data;

    //make new node as head 
    new_node->next = (*head_ref);

    //move the head to new node
    (*head_ref) = new_node;
}

//function to add node after a certain node
void insertAfter(node* prev_node,int new_data){

    //check if previous node …
Run Code Online (Sandbox Code Playgroud)

c linked-list

-1
推荐指数
1
解决办法
59
查看次数

双链表的类

我必须编写一个类来处理没有空头或尾节点的双向链表.我已经修改了一些我认为应该用于双链接列表的代码,但它抛出了以下异常.如何防止发生异常以及删除空头或尾节点需要进行哪些更改?

Exception in thread "main" java.lang.NullPointerException
    at P4DLL.removeLast(P4DLL.java:105)
    at P4DLL.main(P4DLL.java:197)

码:

class ListNode
{
    Object element;
    ListNode next;
    ListNode prev;

    public ListNode( Object anElement )
    {
        element = anElement;
        next = null;
        prev = null;
    }

    public ListNode( Object anElement,  ListNode nextPtr, ListNode prevPtr)
    {
        element = anElement;
        next = nextPtr;
        prev = prevPtr;
    }
} // end class ListNode

public class P4DLL
{

    private ListNode head;
    private ListNode tail;
    private int size;

    public P4DLL()
    {
        head = null;
        tail …
Run Code Online (Sandbox Code Playgroud)

java linked-list

-2
推荐指数
1
解决办法
777
查看次数

在c中使用运算符' - >'的奇怪副作用

我在C中编写的代码中使用运算符' - >'时得到了这种奇怪的副作用.我使用的指针 - > on,被改为有一些垃圾.

进一步来说:

我有以下结构:

typedef void* ListElement ;

typedef  struct List_t* List ;

typedef struct Node_t* Node;

Struct Node_t {
  ListElement data ;
  Node next;
}

Struct List_t {
  Node* head;
  Node* current
}
Run Code Online (Sandbox Code Playgroud)

当我使用以下内容时ListGetFirst(),我得到了有线行为:

ListElement ListGetFirst(List list)
{
  if( list == NULL || list->head==NULL)
  {
    return NULL;
  }
  list->current=list->head;
  Node* head =list->head; // here is the problem
  ListElement data = (*head)->data;
  return data;
}
Run Code Online (Sandbox Code Playgroud)

当我使用调试器时,我发现指针列表 - >头部在标记的前述行上发生了变化.

我真的不知道为什么,而且我不知道' - >'会有副作用

提前致谢

c pointers linked-list side-effects

-2
推荐指数
1
解决办法
275
查看次数

java-存储字符串值 - 这是最有效的链表,数组列表或哈希映射

在java应用程序中,我有一个要求,用户将定义一个字符串值,然后继续将其他字符串值附加到原始值...

用户可以定义多个不同的命名字符串.

从hashmap,数组列表和链表中,我应该根据以下标准使用哪一个:

(1)大多数内存有效(2)每个字符串值最大可能空间

此外,所有3个选项(hashmap/array list/linked list)中单个字符串值的最大可能大小是多少?

java linked-list arraylist hashmap

-2
推荐指数
1
解决办法
798
查看次数

如何实现双向链表?

我们学会了如何在课堂上实现单链表.我们的教授提到我们做了双重链表,但显然很容易,他真的没有详细解释如何做到这一点.我非常擅长处理单链表,但是有人可以告诉我如何制作双重链表吗?

c++ linked-list doubly-linked-list

-2
推荐指数
1
解决办法
1349
查看次数