linked-list

Find cardinal number of a list in C

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-27 06:31:39
问题 How can i find only the elements that appears once in the list and return the cardinal number?For example if my list consist of {3,2,1,1,2,4} i expect for return the counter to be 4 and not 6 cause we do not count the duplicate numbers. Here is the code that i have written so far. struct Node { int data; struct Node *next; }; int Find_cardinal(struct Node *start) { struct Node *ptr1, *ptr2 ptr1 = start; int counter=0; /* Pick elements one by one */ while (ptr1 != NULL && ptr1->next != NULL) {

How do I use arguments sent to the program in a class other than the main class?

穿精又带淫゛_ 提交于 2020-12-13 03:12:33
问题 My program consists of 2 classes named Main and LinkedList and my program takes 3 arguments. For example, while I can access args[1] in the Main class, I cannot access it in the LinkedList class. It says array access args[2] will produce NullPointerException and If I try to run it, it gaves me NullPointerException . How can I solve this? Here is my code: Main Cass: import java.io.*; import java.util.Scanner; // Import the Scanner class to read text files public class Main { public static void

C++ Linked list using smart pointers

偶尔善良 提交于 2020-11-26 04:49:20
问题 I have only been using raw pointers for linked list with templates. For example, the member data, Node<T>* head; and when I am inserting a node one of the lines would be head = new Node<T>(data); . However, now I need to use a smart pointer and I am not sure how I would change it to use smart pointers. Would the member data be changed to shared_ptr<Node<T>> head; and the other line would change to head = shared_ptr<Node<T>>( new <Node<T>>(data) ); ? 回答1: You do not "need" to use a smart

C++ Linked list using smart pointers

大憨熊 提交于 2020-11-26 04:48:42
问题 I have only been using raw pointers for linked list with templates. For example, the member data, Node<T>* head; and when I am inserting a node one of the lines would be head = new Node<T>(data); . However, now I need to use a smart pointer and I am not sure how I would change it to use smart pointers. Would the member data be changed to shared_ptr<Node<T>> head; and the other line would change to head = shared_ptr<Node<T>>( new <Node<T>>(data) ); ? 回答1: You do not "need" to use a smart

C++ Linked list using smart pointers

对着背影说爱祢 提交于 2020-11-26 04:48:41
问题 I have only been using raw pointers for linked list with templates. For example, the member data, Node<T>* head; and when I am inserting a node one of the lines would be head = new Node<T>(data); . However, now I need to use a smart pointer and I am not sure how I would change it to use smart pointers. Would the member data be changed to shared_ptr<Node<T>> head; and the other line would change to head = shared_ptr<Node<T>>( new <Node<T>>(data) ); ? 回答1: You do not "need" to use a smart