linked-list

Reverse a single chained List

僤鯓⒐⒋嵵緔 提交于 2019-12-11 18:17:33
问题 I hope I am using the right terminology. I have made a single-chained list. class MyStack { public Node Initial { get; set; } public MyStack() { Initial = null; } public void Push(int data) { var node = new Node { Data = data, Next = Initial }; Initial = node; } public int Pop() { int res = Initial.Data; Initial = Initial.Next; return res; } public int Sum() { int sum = 0; Node currentNode = Initial; while (currentNode != null) { sum += currentNode.Data; currentNode = currentNode.Next; }

Segmentation Fault error - Implementing Stack using Linked lists

和自甴很熟 提交于 2019-12-11 18:09:22
问题 I am currently in a University program studying Data Structures in C and I am having a lot of trouble right now. I want to make clear that what I am asking help for is not for marks, just practice challenge problems. The goal is to implement a stack using Linked Lists. By looking through the lecture notes I think I have most of the functions down. I need to demonstrate Push() and Pop() will an append and a pretend. Using Cygwin, I compiled with no errors. but when I try to run it, I get a

NullPointerException in queue/linkedlist program

好久不见. 提交于 2019-12-11 18:08:54
问题 1 // This program helps staff manage customers' 2 // orders and decide who should be given a ready dish. 3 4 import java.util.*; 5 6 // This class represents all orders of customers 7 class ListOrder { 8 9 // Data member 10 private int numDishes; 11 // All dishes which the restaurant offers 12 private String[] dishes; 13 // Each dish has a queue of customers who ordered this dish 14 // All such queues are put inside an ArrayList called dishQueues 15 private ArrayList<Queue<Integer>>

Error implementing queue using linked list in C

自作多情 提交于 2019-12-11 18:05:17
问题 I'm implementing a queue using linked lists in C. Here's my structure - typedef struct llist node; struct llist { int data; node *next; }; I'm facing problem while executing push() . Here's my push() definition - void push(node *head,int n) { if (head==NULL) { head=(node *)(malloc((sizeof(node)))); head->data=n; head->next=NULL; printf("=>%d\n",head->data); } else { node *ptr; ptr=head; while(ptr->next!=NULL) { ptr=ptr->next; } ptr->next=(node *)(malloc((sizeof(node)))); ptr=ptr->next; ptr-

java.lang.OutOfMemoryError in my for loop

这一生的挚爱 提交于 2019-12-11 17:54:42
问题 I'm trying to get the size of a list but I get the error: Java HotSpot(TM) 64-Bit Server VM warning: Exception java.lang.OutOfMemoryError occurred dispatching signal UNKNOWN to handler- the VM may need to be forcibly terminated Exception in thread "main" Here is my code: public void wrapText(String text, int width) { List<String> items = new LinkedList<String>(Arrays.asList(text.split(" "))); for(int j = 0; j < items.size(); j++){ items.add(width, "\n"); } System.out.println(items); /* this

Sorting a linked list using merge sort

China☆狼群 提交于 2019-12-11 17:13:04
问题 Given this problem: Write a function that takes two date of birth structures as input and returns -1, 0, 1 if the first parameter is less than, equal to or greater than the second parameter respectively. Using your function, write a recursive mergeSort program to sort the linked list of student records in increasing order of their age (do not use arrays, use linked list only). here is what I wrote: #include <stdio.h> #include <stdlib.h> #include <string.h> /*Recursive Merge Sort to sort

Adding Char Value to a Linked List using Looping [C]

≡放荡痞女 提交于 2019-12-11 16:47:42
问题 I have a linked list which Im currently trying to add values to it. But I must have set my pointers incorrectly or there is something going on with the memory allocation. I want to add tokens to the list but everytime there is a new loop the data overlaps. For example: 1st time: repl> a a 2nd time: repl> b b b Notice how the a just disappears, I want to keep the previous values while adding in new values. Here's my code so far: struct node { int val; struct node *next; }; struct node *head =

[leetcode] Add Two Numbers

我是研究僧i 提交于 2019-12-11 16:08:08
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 https://oj.leetcode.com/problems/add-two-numbers/ 思路:模拟题,从头向后依次遍历两个链表相加。 注意:1. 用dummy head简便处理。2. 注意carry的处理,尤其最高位进位的情况。3. 注意引用null的判断。 public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { // printList(l1); // printList(l2); ListNode p = new ListNode(-1); ListNode head

Swapping Nodes on a single linked list

非 Y 不嫁゛ 提交于 2019-12-11 15:40:01
问题 Since long back I have not used C or C++ ,so forget completely about Pointers. I'm familiar with C# and have written a basic version of this. Need to know whether I'm doing right/wrong? Input:Linked List a->b->c->d->e->null Output: Linked List b->a->d->c->e->null We have to write code such that memory position is swapped and not the node value. public void SwapLinkedList(LinkedList<int> LL) { LinkedListNode<int> current = LL.First; while (current.Next != null) { int temp = current.Next.Value;

How to insert and element before another in a linked list

痞子三分冷 提交于 2019-12-11 15:24:33
问题 public void insertElementBefore(E element, E newElement) { MyNode<E> current = head; if (head != null) { while (current != null) { if (current.data.equals(element)) { MyNode<E> n = new MyNode<E>(newElement); n.next = current.next; current.next = n; return; } current = current.next; } } } This is what I have for this. I'm having troubles to insert the newElement before intended element. Can't seem to figure out the syntax for it. I've been tinkering with it for a while and the best I could get