linked-list

Segmentation fault (core dumped) when I delete pointer

巧了我就是萌 提交于 2019-12-02 09:44:05
问题 I am trying to remove duplicates from a linked list, and encountered a problem, which is probably obvious and straightforward but I haven't used C++ in many years and I couldn't find out what I am doing wrong by reading similar questions on SO. Below is parts of my code. I removed irrelevant parts (eg. constructors, other methods, etc). template<class T> class Node { Node() : data(NULL), next(NULL), prev(NULL) {} explicit Node(T d) : data(d), next(NULL), prev(NULL) {} explicit Node(T d, Node

Print an ordered linked list

空扰寡人 提交于 2019-12-02 08:57:32
问题 Just did some editing on it, i tried what you said but it didnt work, so i tried something i am a little more familiar with but it doesnt seem to work correctly. It prints the information weirdly then crashes.. For exmaple: When i imput 9-8-7-6-5-4-3-2-1 then 0 to print, it prints back to me 0-0-0-9-1-2-3-4-5-6-7-8 and then crashes? when i imput 1-2-3-4-5-6-7-8-9 then 0 to print, it prints back to me 0-0-0-1-2-3-4-5-6-7-8-9 and then crashes. #include <stdio.h> #include <stdlib.h> struct

Single linked list

怎甘沉沦 提交于 2019-12-02 08:28:49
问题 I have created a single linked list. Everything works fine. I just want to know if I have done anything potentially dangerous in my code. The code snippets I am concerned about is my push, pop, and clean-up. The parts of the code is just for user interaction so not really important (I posted anyway so that it was more clear in what I was doing). Just the linked list application. Many thanks for any suggestions, as this is my fist attempt. #include <stdio.h> #include <stdlib.h> #include

C++ Generic Linked List

时间秒杀一切 提交于 2019-12-02 08:27:51
For the code below: #include <iostream> #include <string> using namespace std; class Foo2; class Foo3; template <class T> class Foo1 { public: Foo1(); void print() { cout << "My name is: " << name << endl; } T getNext(){ return nextLink; } string name; T nextLink; }; class Foo2 : public Foo1 { public: Foo2(){ name = "Foo2"; } }; class Foo3 : public Foo1 { public: Foo3(){ name = "Foo3"; } }; template <class T> class LinkedList { public: T curr; T first; void add(T node){ if(first == NULL){ first = node } node->nextLink = this; curr = node; } T getNext(){ return next; } void printAll(){ T curr =

Deleting first node in linked list has problems

假装没事ソ 提交于 2019-12-02 08:08:51
问题 I'm implementing a linked list and it needs to have a function that when given a head of a linked list and a cstring, it finds and deletes a node whose value is the cstring. typedef struct node { char entry[21]; struct node* next; } node; /*returns true if node with phrase value found, otherwise false*/ bool findAndRemove(node* root, char phrase[21]) { if(root != NULL) { node* previous = NULL; while(root->next != NULL) { if(strcmp(root->entry, phrase) == 0)//found { if(previous == NULL)//node

Correctly implementing a singly linked list C++

大憨熊 提交于 2019-12-02 08:03:27
I have a list with names of employers such as: Node 1: Jill, Matt , Joe, Bob, Matt Node 2: Jeff, James, John , Jonathan, John , Edward Node 3: Matt, Doe, Ron , Pablo, Ron , Chase , Ron , Chase, Loui and I'm trying to get it to where if it sees a repeat it will send it to the front of the list and delete that current node, so that it will look like this Node 1: Matt , Jill, Joe, Bob Node 2: John , Jeff, James, Jonathan, Edward Node 3: Chase , Ron , Matt, Doe, Pablo, Loui Unfortunately, My output is close to what I would like. It's deleting the duplicate entries, but it's not sending to the

context switch during doubly linked list creation

懵懂的女人 提交于 2019-12-02 07:24:19
问题 There's an example in Maurice Bach's The Design of the Unix Operating System that mentions how it's possible for a doubly linked list to be destroyed due to a context switch during its creation. (He goes on to say that this is prevented by raising the processor level during such critical regions of code, but I'm having trouble understanding his reasoning that tries to show the problem in the first place) The sample code that he includes is as follows: struct queue { } *bp, *bp1; bp1 -> forp =

How would I make my custom generic type linked list in Java sorted?

China☆狼群 提交于 2019-12-02 07:21:26
I am writing my own linked list in java that is of generic type instead of using the java collections linked list. The add method for the linked list is made up of the following code: public void add(T item, int position) { Node<T> addThis = new Node<T>(item); Node<T> prev = head; int i; if(position <= 0) { System.out.println("Error: Cannot add element before position 1."); } else if(position == 1) { addThis.setNext(head); head = addThis; } else { for(i = 1; i < position-1; i++) { prev = prev.getNext(); if(prev == null) { System.out.println("Cannot add beyond end of list"); } } // end for

Flatten a list using common lisp

泄露秘密 提交于 2019-12-02 07:03:02
I was reading the book On Lisp by Paul Graham. In Chapter 4, Utility Functions, he gives examples of small functions that operate on lists, which would be helpful while writing a larger program. One of them is flatten . Given a nested list at any arbitrary level as argument, flatten will remove all the nested elements and put them on the top level. Below is my attempt at implementing flatten: (defun flatten (lst) (labels ((rflatten (lst1 acc) (dolist (el lst1) (if (listp el) (rflatten el acc) (push el acc))) acc)) (reverse (rflatten lst nil)))) But the above function does not flatten lists

context switch during doubly linked list creation

怎甘沉沦 提交于 2019-12-02 06:43:11
There's an example in Maurice Bach's The Design of the Unix Operating System that mentions how it's possible for a doubly linked list to be destroyed due to a context switch during its creation. (He goes on to say that this is prevented by raising the processor level during such critical regions of code, but I'm having trouble understanding his reasoning that tries to show the problem in the first place) The sample code that he includes is as follows: struct queue { } *bp, *bp1; bp1 -> forp = bp -> forp; bp1 -> backp = bp; bp -> forp = bp1; /* consider possible context switch here */ bp1 ->