linked-list

BJP5 Exercise 16.7: deleteBack — Help me understand the solution

拥有回忆 提交于 2020-01-16 14:07:45
问题 I have been working on this exercise to practice ListNode s, and was very frustrated because even though I thought I wrote the code correctly (as shown below), it didn't let me pass. public int deleteBack() { ListNode p = front; if(p == null) { throw new NoSuchElementException(); } if(p.next == null) { int data = p.data; p = null; return data; } while(p.next.next != null) { p = p.next; } int data = p.next.data; p.next = null; return data; } Next, I tried creating in total three new ListNode s

Arraylist mapping to linkedlist nodes

爱⌒轻易说出口 提交于 2020-01-16 06:27:06
问题 I want to be able to access a certain node in my Doubly Linked List in O(1) time. I know that if i traverse the list to find a certain node it would take O(n) time so I want to map the nodes to an array list where I can access the nodes in O(1) time. I'm really unsure how I would do this mapping. I would like to see an example of how this can be done. Edit: I would like to be able to access any node in the linked list so I can move the nodes around in O(1) time. Example: Move node with ID 5

How to remove any node in a singly linked list

╄→尐↘猪︶ㄣ 提交于 2020-01-15 14:03:05
问题 Here's my code: #include<stdio.h> #include<stdlib.h> struct node { int x; struct node *next; }; struct node *addNode(struct node *head, int y) { struct node *traverser; traverser = head; if(traverser!=NULL) { while(traverser->next!=NULL) traverser=traverser->next; traverser -> next = malloc(sizeof(struct node)); traverser -> next -> x = y; traverser -> next -> next = NULL; } else { head= malloc(sizeof(struct node)); head -> x=y; head -> next = NULL; } return head; } void display(struct node

How to implement a compact linked list with array?

℡╲_俬逩灬. 提交于 2020-01-15 12:07:07
问题 Here is the question of exercise CLRS 10.3-4 I am trying to solve It is often desirable to keep all elements of a doubly linked list compact in storage, using, for example, the first m index locations in the multiple-array representation. (This is the case in a paged, virtual-memory computing environment.) Explain how to implement the procedures ALLOCATE OBJECT and FREE OBJECT so that the representation is compact. Assume that there are no pointers to elements of the linked list outside the

c# linkedlist how to get the the element that is before the last element

£可爱£侵袭症+ 提交于 2020-01-15 12:06:41
问题 i try to implement a redo undo in my windows form application. i build a linkedlist , every entery of the list is a class that save the state of all the elemnts in the form. every click on the save button , insert to this list the last state of the form elements. when the user click on undo button i want to get the entery of the list (one before the last) and load it. i dont know what is the simple way to get this one before elemnts from the linked list ? my code like look like: public class

How to write a linked list to a file

时光怂恿深爱的人放手 提交于 2020-01-15 07:42:25
问题 I'm a new programmer and I am having some troubles writing a linked list to a text file. Here's my code: typedef struct N { int code; char name[MAX1]; int items; float price; struct N *next; } node_t; typedef struct { int code; char name[MAX1]; int items; float price; } product; product p; product *ptr = &p; node_t *iterator = head; FILE *fp; fp = fopen("newfile.txt", "w+"); while (iterator != NULL) { p.code = iterator->code; strcpy(p.name, iterator->name); p.items = iterator->items; p.price

Recursively reverse a linkedlist (code in Stanford CSed library) explanation

…衆ロ難τιáo~ 提交于 2020-01-15 06:09:04
问题 My recursion skill is pretty rusty. I've been thinking about this problem and searched the forum for a long time but still cannot understand. Right now I'm looking at the recursively reverse a linked list code from Stanford CS ed library. #include <stdio.h> struct Node { int x; struct Node *next; }; void Reverse(struct Node ** headRef){ struct Node* first; struct Node* rest; if(*headRef==NULL) return; first= *headRef; rest= first->next; if(rest==NULL) return; Reverse(&rest); printf("Rest%d\n"

Passing a pointer to a linked list in C++

走远了吗. 提交于 2020-01-14 04:14:14
问题 I have a fairly basic program that is intended to sort a list of numbers via a Linked List. Where I am getting hung up is when the element needs to be inserted at the beginning of the list. Here is the chunk of code in question Assume that root->x = 15 and assume that the user inputs 12 when prompted: void addNode(node *root) { int check = 0; //To break the loop node *current = root; //Starts at the head of the linked list node *temp = new node; cout << "Enter a value for x" << endl; cin >>

Cannot convert from Node<E> to Node<E>?

不想你离开。 提交于 2020-01-14 03:19:07
问题 I am just doing some practice from one of my books, and I was curious about why I am getting the following error in eclipse: Type mismatch: cannot convert from type DoublyLinkedList.Node<E> to DoublyLinkedList.Node<E> Code: import java.util.Iterator; import java.util.ListIterator; import java.util.NoSuchElementException; public class DoublyLinkedList<E extends Comparable<E>> implements Iterable<E>{ private int size = 0; private Node<E> head; private Node<E> tail; /** Returns a list iterator

How to implement a queue in a linked list in c?

假如想象 提交于 2020-01-13 07:22:10
问题 I am given these structure declarations in order to implement a queue collection that uses a circular linked list. typedef struct intnode { int value; struct intnode *next; } intnode_t; typedef struct { intnode_t *rear; // Points to the node at the tail of the // queue's linked list int size; // The # of nodes in the queue's linked list } intqueue_t; intnode_t *intnode_construct(int value, intnode_t *next) { intnode_t *p = malloc(sizeof(intnode_t)); assert (p != NULL); p->value = value; p-