codingdir logo sitemap sitemap |

Linked list issue - loop iterates over wrong nodes

By : , Category : c

You have placed the node removal line in the wrong place.

for (count = N; count > 1; --count) {
   for (i = 0; i < M - 1; ++i)
      p = p->next;

   p->next = p->next->next;

You placed this line after the loop that count M nodes from the beginning so it always start with removal of (M+1)th node. You should move it before it so that it starts from the first node.

for (count = N; count > 1; --count) {
   p->next = p->next->next;
            for (i = 0; i < M - 1; ++i) p = p->next;


Is this what You're looking for?

ReLated :

scanf("%c", &c); won't skip spaces. You're building a list with 6 nodes, 3 of which contain spaces.

change your inner while loop to

    while(!=null &&{

when your last two nodes are equal, you are making; but you not checking whether next is null or not, just checking &&, which is causing the problem.

Well I'd clean up your while loop to include everything in one statement. But that's just because I'm lazy. ;)

while (!JOptionPane.showInputDialog("Add another node?").equalsIgnoreCase("yes")) 
    //make a new node

As for the code to make a new node I would suggest implementing your List class from the List interface that Java has. You can read about it here. If you're just starting out in Java though it might be a little hard to understand. As a comment on your existing code here:

List front = new List();
front.x = Integer.parseInt(JOptionPane.showInputDialog("Enter a value"));

You're not exactly making a new node per-say, you're just creating new instances of your List class. In this case the object is labeled 'front' My suggestion is to read up more on how a List is meant to function. Here is a good example.

If the head node is the one you want to delete, it will be freed after the function delete is called. However, the head pointer is still pointing to the head node which is already freed. This is the reason that cause the seg fault.

One way to change is :

void delete(employee **head, int option) {

employee* current;
employee *del;
current = *head;

if (current != NULL) {
    if (current->EMP_ID == option) {
        del = *head;
        *head = *head->next;
    while (current->next != NULL) {
        if (current ->next->EMP_ID == option) {
            del = current -> next;
            current->next = current ->next->next;
    current = current -> next;

Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .