linked-list

Simple linked list in C++

江枫思渺然 提交于 2019-12-18 10:12:00
问题 I am about to create a linked that can insert and display until now: struct Node { int x; Node *next; }; This is my initialisation function which only will be called for the first Node : void initNode(struct Node *head, int n){ head->x = n; head->next = NULL; } To add the Node , and I think the reason why my linked list isn't working correct is in this function: void addNode(struct Node *head, int n){ struct Node *NewNode = new Node; NewNode-> x = n; NewNode -> next = head; head = NewNode; }

Dereferencing the void pointer in C++

痴心易碎 提交于 2019-12-18 09:26:36
问题 I'm trying to implement a generic linked list. The struct for the node is as follows - typedef struct node{ void *data; node *next; }; Now, when I try to assign an address to the data, suppose for example for an int, like - int n1=6; node *temp; temp = (node*)malloc(sizeof(node)); temp->data=&n1; How can I get the value of n1 from the node? If I say - cout<<(*(temp->data)); I get - `void*' is not a pointer-to-object type Doesn't void pointer get typecasted to int pointer type when I assign an

Swap elements in LinkedList

痴心易碎 提交于 2019-12-18 07:43:52
问题 I want to maintain order of the elements being added in a list. So, I used a LinkedList in Java. Now I want to be able to swap two elements in the linked list. First of all, I cannot find an elementAt() for LinkedList . Also, there is no way to add element at a specified position. 回答1: There is a Collections.swap(List<?> list, int i, int j) that you can use to swap two elements of a List<?> . There's also LinkedList.get(int index) and LinkedList.add(int index, E element) (both are methods

C++ Add to linked list in sorted order

大城市里の小女人 提交于 2019-12-18 06:13:46
问题 Hi I have a linked list using structs. Right now I got it to add every element at the end. However I'd like to add each element in sorted order based on the ID. The struct has two elements: string name, and long ID. node* temp = new node; temp->name = nameRead; temp->id = idRead; //check if first item, if so add as head if(head == NULL) { head = temp; } else { node* temp2 = head; while(temp2->next != NULL) { temp2 = temp2->next; } temp2->next = temp; } 回答1: node* temp = new node; temp->name =

Pointer into Java LinkedList Node

倖福魔咒の 提交于 2019-12-18 05:56:01
问题 I am pushing n entries into Java LinkedList at O(1). There are few unique items i would like to remove later on at O(1). I though about keeping an array with "pointers" to the unique nodes at the LinkedList so i can later on remove them. is there a way to do it on LinkedList or any other java class? I tried storing iterators to the items. So i can use iter.remove() . But i understood that there can be only one iterator on a list at the time. I know that an easy solution could be implementing

clear() impl in Java's LinkedList

流过昼夜 提交于 2019-12-18 05:44:19
问题 I fear this is a really stupid question, but here goes: Why does the clear method in Java's default LinkedList implementation bother to walk the list and unhook all the nodes? Why not just unhook the header and leave the rest of the list connected -- the GC will get it anyway, no? Here's the method: /** * Removes all of the elements from this list. */ public void clear() { Entry<E> e = header.next; while (e != header) { Entry<E> next = e.next; e.next = e.previous = null; e.element = null; e =

Linked lists in C++

馋奶兔 提交于 2019-12-18 05:02:57
问题 I am trying to teach myself linked-lists with node structs and was hoping someone could help me with this. I would take input from the command line and it would make me a nested list and I could output it. Example: Input: "1 2 3 4 5" Output:"1 2 3 4 5" There are two things I am having trouble with: 1) When I run the program I keep getting warning: ‘typedef’ was ignored in this declaration [enabled by default] How can I get rid of this? EDIT: I have changed this to typedef struct Node* NodePtr

Linked lists in C++

冷暖自知 提交于 2019-12-18 05:02:23
问题 I am trying to teach myself linked-lists with node structs and was hoping someone could help me with this. I would take input from the command line and it would make me a nested list and I could output it. Example: Input: "1 2 3 4 5" Output:"1 2 3 4 5" There are two things I am having trouble with: 1) When I run the program I keep getting warning: ‘typedef’ was ignored in this declaration [enabled by default] How can I get rid of this? EDIT: I have changed this to typedef struct Node* NodePtr

Linked Lists in C without malloc

夙愿已清 提交于 2019-12-18 02:39:29
问题 #include <stdio.h> typedef struct node { int i; struct node *next; }node; node getnode(int a) { struct node n; n.i=a; n.next=NULL; return n; } main() { int i; node newtemp,root,temp; scanf("%d",&i); root=getnode(i); temp=root; while(i--) { newtemp=getnode(i); temp.next=&newtemp; if(root.next==NULL) { root=temp; } temp=*(temp.next); } temp=root; while( temp.next != NULL ) { printf(" %d ",temp.i); temp=*(temp.next); } } I am trying to create a linked-list without using malloc. The programming

Difference between a LinkedList and a Binary Search Tree

元气小坏坏 提交于 2019-12-17 21:46:45
问题 What are the main differences between a Linked List and a BinarySearchTree? Is BST just a way of maintaining a LinkedList? My instructor talked about LinkedList and then BST but did't compare them or didn't say when to prefer one over another. This is probably a dumb question but I'm really confused. I would appreciate if someone can clarify this in a simple manner. 回答1: Linked List: Item(1) -> Item(2) -> Item(3) -> Item(4) -> Item(5) -> Item(6) -> Item(7) Binary tree: Node(1) / Node(2) / \ /