linked-list

Linked list sorting in C

独自空忆成欢 提交于 2019-12-02 06:21:37
I'm writing a simple file for one of my classes that is a simple linked list activity and I need to sort a linked list. This is my source code so far: /* * Simple list manipulation exercise. * 1. Create a list of integers. * 2. Print the list. * 3. Sort the list. * 4. Print the list * 5. Free the list nodes. */ #include <stdlib.h> #include <stdio.h> struct node { int value ; struct node *next ; } ; extern struct node *mk_node(int v) ; extern void print_list(struct node *head) ; extern struct node *sort_list(struct node *head) ; extern void free_list(struct node *head) ; #define NVALUES (6) int

Issues with reversing objects in a LinkedList

做~自己de王妃 提交于 2019-12-02 05:47:42
I'm writing code for an assignment that requires a method which reverses elements in a LinkedList, given the portion of the list to reverse. For example, if the user enters 3 the method would reverse the first 3 elements in the array. I've written code for it but instead of reversing the code it simply replaces the 2nd element with the element present in the first index. My only issue seems to be the reverseFirstSome method. I'm not asking you to write the code for me, but any pointer in the right direction would be appreciated. Here is my full code for the class: import java.util

Single linked list

牧云@^-^@ 提交于 2019-12-02 05:42:40
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 <string.h> typedef struct product_data product_data_t; struct product_data { int product_code; char product

How to check if a linked list is a palindrome or not in Java?

自古美人都是妖i 提交于 2019-12-02 05:42:17
问题 I wrote a code to check if a singly linked list is a palindrome. And I made two steps: 1st. reverse the original linked list. 2nd. Check if the original and reversed linked list have the same element. public static Boolean isPalindrome(Node input){ Node reversed= reverse(input); while (input!=null){ if(input.item!=reversed.item) return false; input=input.next; reversed=reversed.next; } return true; } static Node head; public static Node reverse(Node input){ if(input==null || input.next==null)

Deleting first node in linked list has problems

牧云@^-^@ 提交于 2019-12-02 04:29:50
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 to delete is at head { node* tmp = root; root = root->next; free(tmp); return true; } previous->next =

How to delete two items in a row in a linked list

馋奶兔 提交于 2019-12-02 04:28:00
void delete_double (LN<T>*& l) { if (l == nullptr) return; LN<T> *p = l; while ( p -> next != nullptr && p -> next -> next != nullptr) { if (p -> value == p -> next -> value) // the current value is equal to the next value in the linked list { if (p == l) // when the first two values are the same // not sure if it is correct { l = l -> next -> next; } else // Problem should be here { LN<T> *to_delete = p; // Also tried this (doesn't work) p = p->next; delete to_delete; // LN<T>* to_delete = p; // LN<T>* to_delete2 = p -> next; LN<T> *to_delete1 = p; // l = to_delete2 -> next; p = p->next; //

Singly-Linked List in Rust

限于喜欢 提交于 2019-12-02 04:25:44
I've been trying to teach myself some Rust lately and wanted to practice a bit by implementing a simple linked list. I took some inspiration from the Rust library's linked list and tried to replicate the parts I already understood. Also I decided to make it singly-linked for now. struct Node<T> { element: T, next: Option<Box<Node<T>>>, } impl<T> Node<T> { fn new(element: T) -> Self { Node { element: element, next: None, } } fn append(&mut self, element: Box<Node<T>>) { self.next = Some(element); } } pub struct LinkedList<T> { head: Option<Box<Node<T>>>, tail: Option<Box<Node<T>>>, len: u32, }

My program replaces all the string data types in all the nodes in the linked list

≯℡__Kan透↙ 提交于 2019-12-02 04:24:21
I have a program that basically adds a history(node) to the employee_record(linked list). Here is my code: #include <stdio.h> #include <stdlib.h> struct history{ char *department1; char *title1; int day; int month; int year; struct history *next; }; struct employee_record{ char firstname[20]; char lastname[20]; long int employee_id; char sex; int age; struct history *head; }; void addjob(struct employee_record *rec, char *department, char *title, int day, int month, int year); void print(struct employee_record *rec); int main(int argc, char *argv[]) { struct employee_record *worker=(struct

Segmentation fault (core dumped) when I delete pointer

落爺英雄遲暮 提交于 2019-12-02 04:22:08
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<T> *nxt, Node<T> *prv) : data(d), next(nxt), prev(prv) {} ~Node() { delete next; delete prev; } T data;

Copying nodes in linked list C

拈花ヽ惹草 提交于 2019-12-02 04:09:44
问题 I am trying to duplicate a node in linked list. I am not sure if I am doing it correctly. I tried making test cases but did they were not successful. If some one could tell me if where I went wrong and what I did right, also what is the best way to test my code. struct node { int id; char side; int quantity; double price; }; struct onode { struct node* data; struct onode* next; struct onode* prev; }; struct onode* newNode (struct node* data) { struct node* dataValue = (struct node*) malloc