singly-linked-list

Copy the linked list pointed by a parameter

旧巷老猫 提交于 2019-12-11 08:34:33
问题 I'm trying to implement a function which will make a copy of the list pointed by parameter. I store head pointers to nodes in some array and the parameter is just its index. This is my attempt: void copy(node **array, int *amount_of_lists, int parameter) { node *current = array[parameter]; array[(*amount_of_lists) - 1] = malloc(sizeof(node)); node *new_list = array[(*amount_of_lists) - 1]; while(current->next != NULL) { new_list->next = malloc(sizeof(node)); new_list->str = current->str;

implementing stack with a linked list in C++ , copy constructor

心不动则不痛 提交于 2019-12-11 07:12:57
问题 I am trying to implement a stack using a linked list in C++, and I don't know how to write correctly the copy constructor of the stack class. I am using the following classes: class Node{ int m_num; Node* m_pNext; public: Node(); ~Node(); //and the standard get&set functions.. } class LinkedList{ int m_size; Node* m_pHead; public: LinkedList(); LinkedList(const LinkedList& obj); ~LinkedList(); //and the standard get&set functions.. } class Stack{ LinkedList m_stack; public: Stack(); Stack

Sorting a Singly Linked List With Pointers

限于喜欢 提交于 2019-12-11 06:55:43
问题 I am trying to sort a singly linked list using bubble sort by manipulating ONLY the pointers, no keys. The following gets stuck in the for loop and loops infinitely. I don't understand why this is. Can anybody explain to me why the end of the list is not being found? Node* sort_list(Node* head) { Node * temp; Node * curr; for(bool didSwap = true; didSwap; ) { didSwap = false; for(curr = head; curr->next != NULL; curr = curr->next) { if(curr->key > curr->next->key) { temp = curr; curr = curr-

How a LinkedList Keeps Track of All Nodes - C#?

泪湿孤枕 提交于 2019-12-11 05:07:16
问题 I have been struggling trying to develop my own singly linked list, I cant understand how a node is inserted at the end of a Linked List? Here's the code: class LinkedList { private Node head; public void AddLast(int value) { if (head == null) { head = new Node(); head.value = value; head.next = null; } else { Node temp = new Node(); temp.value = value; Node current = head; while (current.next != null) { current = current.next; } current.next = temp; } } public void PrintAll() { Node current

Singly linked list in C(Inserting a node)

∥☆過路亽.° 提交于 2019-12-11 02:36:58
问题 I have this problem when I input. The program will freeze and a pop out window will open and says " .exe has stopped working. " It is just a simple insert and display fuction of a singly linked list. I tried everything. I rewrote the code and find another way of inserting. I tried different compiler.. It works on turbo C but I am using devC++. Is this a compiler error? Here is the code: #include <conio.h> #include <stdio.h> #include <stdlib.h> #include<windows.h> #include <string.h> typedef

Linked lists, operations with parameter

…衆ロ難τιáo~ 提交于 2019-12-10 12:12:13
问题 I'm trying to implement program in with i can create ~arbitrary number of singly linked lists dynamically and perform operations on particular one (defined by parameter). I create dynamic array of head pointers so that i can refer to the certain head node defined by paramater(index of an array + 1). Parameter is just (1,2,3..number of lists). So far I have managed to implement only initialise and push function but the program after complilation doesn't work as expected. Where is the problem?

Insertion sort on linked list in C?

安稳与你 提交于 2019-12-07 06:25:51
问题 I've tried searching for a problem similar to mine, but haven't found much help. I have a linked list of structs of this type: struct PCB { struct PCB *next; int reg1, reg2; }; I first create 10 PCB structs linked together in this way: for(i=20;i<=30;i++) { curr = (struct PCB *)malloc(sizeof(struct PCB)); curr->reg1 = i; curr->next = head; head = curr; } I then need to create 20 more PCB structs, but their reg1 values need to be generated using rand() . I'm currently doing that as so: for (j

What is the difference between SGI slist and C++11 forward_list?

不打扰是莪最后的温柔 提交于 2019-12-07 06:10:22
问题 Both SGI slist and C++11 std::forward_list appear identical to me unless I have missed something; both implement a singly-linked list. I assume there is a difference though as the C++ Standard Commitee didn't adopt the name slist and instead chose a new name, forward_list, when they added the container into the Standard Library for C++0x. 回答1: One major difference is that std::forward_list lacks a size() member function, where as the sgi::slist doesn't. The motivation for this is that an O(N)

Big O complexity to merge two lists

前提是你 提交于 2019-12-04 13:59:16
Given 2 singly linked lists already sorted, merge the lists. Example: list1: 1 2 3 5 7 list2: 0 4 6 7 10 ---> 0 1 2 3 4 5 6 7 7 10 Despite from the fact that the solution is quite simple and there are several different implementations of the problem with or without using recursion (like this http://www.geeksforgeeks.org/merge-two-sorted-linked-lists/ see Method 3), I was wondering what would be the O big complexity of this implementation: If one of the lists is empty just return the other Otherwise insert each node of the second list into the first one using the sortedInsert function which

Creating a linked list with a for loop

雨燕双飞 提交于 2019-12-02 17:28:33
问题 Here is my struct struct ListItem{ int data; struct ListItem *next; }; Assuming the first node of the linked list will have data = 0, I want to write a for loop that creates a linked list of size 5 but I'm not sure how to work I tried the following int main(int argc, char* argv[]){ struct ListItem a; a.data = 0; for (int i = 1; i < 5; i++){ struct ListItem *pointer = &a; struct ListItem nextnode; nextnode.data = i; a.next = &nextnode; pointer = pointer->next; } } But the result is a.data = 0