linked-list

Why is my printList function not working?

#include <iostream> using namespace std; struct Node { char item; Node *next; }; void inputChar ( Node * ); void printList (Node *); char c; int main() { Node *head; head = NULL; c = getchar(); if ( c != '.' ) { head = new Node; head->item = c; inputChar(head); } cout << head->item...

How do I write a public function to return a head pointer of a linked list?

class Newstring { public: Newstring(); void inputChar ( char); void display (); int length (); void concatenate (char); void concatenate (Newstring); bool substring (Newstring); void createList (); Node * getHead (); // error private: struct Node { char item; Node *next; }; N...

Why the time complexity in dll is faster than deletion of a node in sll?

Why the time complexity in doubly linked list which is O(1) is faster than deletion of a node in singly linked list which is O(n)? ...

Does anyone have C# code to repair a linked list that cycles?

http://stackoverflow.com/questions/1285560/how-do-you-remove-a-cycle-in-a-single-linked-list Before I write some sample code to do what this answer describes, does anyone already have a C# example of repairing a singly linked list that points back into itself? I understand the detection part (tortoise/hare) but the repair part is a lit...

linked list adding to the tail, confusion

Hello, Visual Studio 2008 C What I can't understand about this linked list is the adding to the tail in the else part of the if statement. When the head and tails is assigned the memory address of the node_temp to both tail and head both point to the same memory location. However, in the else part is the head in fact still pointing ...

Lists in Haskell : data type or abstract data type?

Hi, From what I understand, the list type in Haskell is implemented internally using a linked list. However, the user of the language does not get to see the details of the implementation, nor does he have the ability to modify the "links" that make up the linked list to allow it to point to a different memory address. This, I suppose, ...

Delete a node in singly link list

How to delete a node in a singly link list with only one pointer pointing to node to be deleted? [Start and end pointers are not known, the available information is pointer to node which should be deleted] ...

Tetris: Layout of Classes

Hi all. I've written a working tetris clone but it has a pretty messy layout. Could I please get feedback on how to restructure my classes to make my coding better. I focuses on making my code as generic as possible, trying to make it more an engine for games only using blocks. Each block is created seperately in the game. My game has ...

How do I insert a node at the beginning of a linked list?

What is wrong with addAtBegin? The list seems to be correct after assigning the newly created node to start, but when control returns to main, the new value is not saved. typedef struct node { int data; struct node *link; }node; node* createList(int data) { node *tempNode=(node*)malloc(sizeof(node)); tempNode->data=data...

Recursion in Linked List

I want to print the reverse of a linked list. I am doing it by recursion. But while calling read(temp) in function read, it gives a BUS ERROR. Any reasons why is this happening ?? #include <iostream> using namespace std; struct node { int info; node *next; }; void read(node *start) { node *temp = start->next; if(sta...

Manually sort a linked list in the C programming language

How do you sort a linked list by name in a function in C? struct rec{ char name[20]; int nr; struct rec *nextRec; }; typedef struct rec Rec; /* synonym for struct rec */ typedef Rec *RecPtr; /* synonym for pointer */ void SortLinkedList(RecPtr *sPtr, int p_size);/* prototype */ int main() { RecPtr startPtr = NULL; /...

How to iterate a class of my creation in Java?

I created a class MyList that has a field private LinkedList<User> list; I would like to be able to iterate the list like this: for(User user : myList) { //do something with user } (when my list is an instance of MyList). How? What should I add to my class? ...

java implementation of Linked List Data Structure

Hello, I am trying to implement the linked list data structure using java, it works fine for insertion or removing first elements but fails to remove the last element via using the removeLast() method. My Linked List Node class: public class LLNode { String value; LLNode next; public LLNode(String value){ ...

linked-list in C++ how to go to "next element" using STL list

I have a very basic question. I want to use STL's list instead of creating my own linked-list ( my code is shown below) struct myList { myList *next; myList *previous; }; myList->next = NULL; Using STL list: #include <list> std::list<int> L; L.push_back(1); My question is, how to access the "next" element in STL's list? ...

Advantages of linked lists over binary trees?

The title is mostly self-explanatory: what are the advantages of linked lists over binary trees? The only case I can think of in which a linked list is more efficient is for iterating over every element, in which case it's still pretty close. It looks like binary trees are faster at both accessing data and inserting new elements. So why ...

C++ linked-list legacy code to STL -- allocating linked list size on the fly.

hi, I am working on some legacy code that defines a linked list (not using STL container) I want to convert this code so as to use STL list. As you can see in the following example, the linked list is assigned an initial value for all Ns. Then certain elements in the list are assigned some value. Then the "empty elements" in the list a...

Reading File Data Into a Linked List in C

I am trying to create a simple phonebook program that reads data from a file and stores the content into specific nodes in the list. It works fine if I use my addEntry function with static data, such as: addEntry("First", "Last", "555-555-5555"); If I try to read more than 1 entry from the file, each entry just appears to be whatever ...

Why are linked lists almost always used with separate chaining?

It seems as though every time I see a mention of separate chaining in a hash table, a linked list is used as the data structure to store items with collisions. Why is this? For instance, why couldn't you use a vector/array data structure? ...

C++ Templates - LinkedList

Hello, EDIT -- Answered below, missed the angled braces. Thanks all. I have been attempting to write a rudimentary singly linked list, which I can use in other programs. I wish it to be able to work with built-in and user defined types, meaning it must be templated. Due to this my node must also be templated, as I do not know the info...

How to Use LLIST *mylist[N];

I get this: LLIST *mylist[N]; Where N is the number of rows of the input file. Then mylist[i] is a pointer to the ith linked list. I call a function LLIST *list_add(LLIST **p, int i){ LLIST *n; n = (LLIST *) malloc(sizeof(LLIST)); if (n == NULL) return NULL; n->next = *p; /* the previous element (*p) now ...