Insert alphabetically linked list C

Problem when alphabetically inserting a node into a doubly linked list

Im trying to write an insertion method for a doubly linked list where as i'm placing the node into the list it is being inserted in alphabetical order. This idea is I will traverse through the list with a curnode and if the newNode comes before the curnode, I will simply place the newNode before the curnode. The code I have written so far works for inserting 1 node into the list, but i'm having problems with the second part that requires checking the order and placing before. How should I change the program to make it work properly? With the code I have now only 1 element [head] will be inserted.

void insert[String x]{ node curnode = head; node newNode = new node[x]; //list is empty so insert as normal - [works fine] if [head == null]{ head = newNode; head.prev = null; head.next = null; tail = newNode; } //Now insert node with respect to alphabetical order - [problem area] else { // while the list isn't empty while [curnode != null]{ // if newNode alphabetically comes before the curnode then place before if [curnode.data.compareTo[newNode.data] > 0]{ node temp = curnode.prev; curnode.prev = newNode; newNode.next = curnode; newNode.prev = temp; break; } } } }

Solutions

There are a few things missing with your implementation. Compare it with this working solution:

void insert[String x]{ node curnode = head; node lastnode = null; node newNode = new node[x]; //list is empty so insert as normal - [works fine] if [head == null]{ head = newNode; head.prev = null; head.next = null; tail = newNode; } //Now insert node with respect to alphabetical order - [problem area] else { // while the list isn't empty while [curnode != null]{ // if newNode alphabetically comes before the curnode then place before if [curnode.data.compareTo[newNode.data] > 0]{ node temp = curnode.prev; curnode.prev = newNode; newNode.next = curnode; newNode.prev = temp; if[temp != null] { temp.next = newNode; } else { // If newnode gets inserted in the head head = newNode; } break; } lastnode = curnode; curnode = curnode.next; } if [curnode == null] { // insert to the last lastnode.next = newNode; newNode.prev = lastnode; tail = newNode; } } }
Tags: Java / Algorithm / Sorting

Similar questions

Doubly-linked list insertion sort function: Why isn't my insertion sort function inserting the last two integer inputs into my linked list?
My insertion sort function does not seem to be inserting my last two integer inputs: 15 and 8, into my doubly linked list. My output leaves out 15 and 8. What am I doing wrong in the insertion sort function? I have provided my functions' code, input, and output. Thank you. Here is my insertion sort function: Here is my print function Here is how I ...
Java Doubly Linked List Insertion Sort
Adding new linked list after old linked list in a doubly-linked list
Hello there I have an interface; and a clear implementation as you see ; and have a constructor class; I didn't put my all methods above. Because of my project so, my problem is how can I implement the methods? I want to add a new linked list after the old one by the interface.
Java Data Structures Generic List
Inserting Node into middle of Linked List, and accidentally inserting null node as well
I'm working on a program that does not use Java's built in Linked List class; I'm building it from scratch. I've been successful with everything except writing a method that inserts a Node into a particular position of the linked list. I have a method that sets a particular Node as the "current" Node. So, for example, I have a linked list that look...
Java Singly Linked List
inserting first node into empty doubly-linked list [how to]
This is a follow-on to a previous post. I am now looking at how to insert a first node into an empty doubly-linked list. It's kind of tricky at first it seems...i would be grateful for a hint as to what is missing in my addFirst method [EDIT] Solution as proposed by typo.pl:
Java Linked List
Add Doubly Linked List Node in Singly Linked List
Given singly Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> null Modify middle element as doubly Linked List Node here middle element is 3 3 -> next should point to 4 3 -> prev should point to 1 Can any one suggest how can it be done ? interviewer gave me hint use interface. but I couldn't figure it out how. I have to iterate over this ...
Java C++ Algorithm
Java: When I insert into my circular doubly-linked list with a header dummy node, my dummy node is shuffled around. Why?
I have written a circular doubly-linked list with a dummy node at the front. During the initialization of my DLL class, I create the dummy node. When I use the debugger in jGrasp and use the visualization tool, after inserting several numbers, my dummy node gets shuffled around and does not stay at the front. I don't understand how I am modifying m...
Java Nodes Doubly Linked List

Also ask

Implementing a SelectionSort capable of handling array list, linked list, and doubly linked list in java How do I insert a new node before first node of a doubly-linked list? Doubly Linked List Java Remove method does not work for any node besides head node How Can I Print The Previous Node and Next Node for a Doubly Linked List? delete node after specified node in doubly linked list. [recursively] Having trouble inserting into a doubly linked, double ended list with Java Copying a linked list node and inserting it in the middle of a linked list How to output the correct linked list when Inserting a node at a specific position in a singly linked list recursively? How to insert an "info" node into a linked-list, numerically or alphabetically, without using a sorting method? Singly linked list to doubly linked list

Video liên quan

Chủ Đề