You are here:

C++/problem with linked list

Advertisement


Question
Hi

I have a problem with linked list,i want to delete a node.q is a node that i want to delete and p is the node before q,i signed my problem in the code below could you plz tell me whats my problem,because it doesnt work?(it's a Circularly-linked list)


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void Delete(struct node*,int);

struct node{

  char item[10];
  int NUM;
  struct node*link;

};

int n=0;

void main(){

  int i=0,num=1;

  struct node*head=NULL;

  struct node*p=NULL;

  printf("\nHow many item do you want to enter?\n");

  scanf("%d",&n);

  for(i=1;i<=n;i++)
  {
  if(head==NULL)
  head=p=(struct node*)malloc(sizeof(struct node));

  else
  {
  p->link=(struct node*)malloc(sizeof(struct node));

        p=p->link;

     }

     p->link=NULL;

     printf("\nEnter item:\n");

     scanf("%s",p->item);

     p->NUM=num++;

     }

         num--;

     p->link=head;

  for(p=head;;p=p->link){

  printf("NUM = %d , item = %s\n",p->NUM,p->item);

         num=num-1;

         if(num==0)
         break;
        }

     Delete(head,num);

     
}

void Delete(struct node*p,int num)
{

  int count=0;

  struct node*head,*q;

  head=p;

  num=n;

  count=n;
  
  while(num!=1)
  {

  int a=1 + int (10 * rand() / ( RAND_MAX + 1 ) );

  printf("a = %d\n",a);

        for(p=head;;p=p->link)
        {
        if(p->link->NUM==a)//here myproblem
         it search for 'a'
         through the list and
         if find it,then delete it
        {

         q=p->link->link;

         p->link=q->link;

         q->link=NULL;
         
         free(q);

         num--;

         break;

         }

         count=count-1;

         if(count==-1)
         {
         count=n;

         break;

         }
     
        }

        }
    for(p=head;;p=p->link){

   printf("NUM = %d , item = %s\n",p->NUM,p->item);

         num=num-1;

         if(num==0)
         break;
        }
}



Thanx

Bita


Answer
Hi Bita,

Ok, with a singly linked list, you can't be at the node you want to delete, because you're past the previous one that point to it.  You got that right.  Removing the item you find is the problem.  I use pictures - it makes it easier to see:

2->next => 6->next => 3->next

You want to delete 6.  2->next is 6.  So 2->next needs to be 3.  3 is 2->next->next:

if(p->link->NUM == a)
 {
 q = p->link;
 p->link = q->link; // or p->link = p->link->link;
 free(q);
 }

You're problem is q = p->link;  Not q = p->link->link.  Also you don't need to set q->link = NULL because the pointer is being freed.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.