Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > deletion in link list

Reply
Thread Tools

deletion in link list

 
 
Mark Bluemel
Guest
Posts: n/a
 
      12-04-2007
ratika wrote:
> On Dec 3, 4:36 pm, ratika <(E-Mail Removed)> wrote:
>> can anyone tell me how to delete a certain node in a doubly circular
>> link list


> //circular doubly link list
> #include<stdio.h>
> #include<conio.h>


Non-standard header

> #include<alloc.h>


Non-standard header

[snip]

> void main()
> {
> int item,ch;
> struct node **t;


It's not clear what "t" should be - I think it's probably the "head" of
the list and should probably be simply "struct node *t". A more
meaningful name would be good.

> clrscr();


Non-standard function.

> *t=NULL;


Fails here - t is not initialized, so you can't dereference it safely.

> do
> {
> printf("\nMenu:\n1.add node\n2.display\n3.delete first node\n4.exit
> \nenter the choice");
> scanf("%d",&ch);


scanf() is a poor choice for interactive input (and for much other
input, IMHO)

> switch(ch)
> {
> case 1:
> printf("enter the value to be inserted");
> scanf("%d",&item);
> addnode(t,item);


I think you want to pass "&t" not "t" here.

> break;
> case 2:
> display(*t);


If I'm right about t, then you should just pass t.

> break;
> case 4:
> break;
> case 3:
> delfirst(*t);


And here you probably need to pass "&t" again.

> break;
> default:
> printf("wrong choice try again");
> }
> }while(ch!=3);


Wrong exit check...

> getch();


Non-standard function

> }
>
>
> //functions starts
> void addnode(struct node **t,int val)


Where do you want to add your new nodes - are they ordered?

> {
> struct node *temp,*temp1;


Hopeless names for your variables.

> temp=(struct node*)malloc(sizeof(struct node));
> temp->pre=NULL;
> temp->data=val;
> temp->next=NULL;
> if(*t==NULL)
> {
> *t=temp;
> temp->pre=*t;
> temp->next=*t;
> }
> else
> {
> *t=temp1;


What is this supposed to mean?

> while(temp1->next!=*t)
> {
> temp1=temp1->next;
> }
> temp1->next->pre=temp;
> temp->next=temp1->next;
> temp->pre=temp1;
> temp1->next=temp;
> }
> }
>
> void display(struct node *t)
> {
> struct node *temp;


Stupid name and not needed.

> if(t==NULL)
> {
> printf("link list empty");
> }
> else
> {
> temp=t;
> while(temp->next!=NULL)


This is a doubly-linked list, isn't it?
This condition won't be encountered.

> {
> printf("%d",temp->data);
> temp=temp->next;
> }
> }
> }
> void delfirst(struct node *st)
> {
> if(st==NULL)
> {
> printf("link list empty");
> }
> else
> {
> struct node *temp;
> temp=st;
> temp->pre->next=temp->next;
> st=temp->next;
> temp->next->pre=temp->pre;
> }
> }


This doesn't update the list head that you track in your main routine,
does it? Nor does it deal with removing the last element.

Here's a version, with fairly minimal changes, that seems to work

//circular doubly link list
#include<stdio.h>
#include<stdlib.h>

struct node
{
struct node *pre;
int data;
struct node *next;
};
void addnode (struct node **, int);
void display (struct node *);
void delfirst (struct node **);
int
main ()
{
int item, ch;
struct node *head;
head = NULL;
do
{
printf ("\nMenu:\n1.add node\n2.display\n3.delete first
node\n4.exit\nenter the choice");
scanf ("%d", &ch);
switch (ch)
{
case 1:
printf ("enter the value to be inserted");
scanf ("%d", &item);
addnode (&head, item);
break;
case 2:
display (head);
break;
case 4:
break;
case 3:
delfirst (&head);
break;
default:
printf ("wrong choice try again");
}
}
while (ch != 4);
}


//functions starts
void
addnode (struct node **head, int val)
{
struct node *newNode;
newNode = (struct node *) malloc (sizeof *newNode);
newNode->pre = NULL;
newNode->data = val;
newNode->next = NULL;
if (*head == NULL)
{
*head = newNode;
newNode->pre = *head;
newNode->next = *head;
}
else
{
struct node *currentNode = *head;
while (currentNode->next != *head)
{
currentNode = currentNode->next;
}
newNode->next = currentNode->next;
newNode->pre = currentNode;
currentNode->next->pre = newNode;
currentNode->next = newNode;
}
}

void
display (struct node *head)
{
struct node *currentNode;
if (head == NULL)
{
printf ("link list empty");
}
else
{
currentNode = head;
do
{
printf ("%d %p %p %p\n", currentNode->data, currentNode,
currentNode->pre, currentNode->next);
currentNode = currentNode->next;
} while (currentNode != head);
}
}
void
delfirst (struct node **head)
{
if (*head == NULL)
{
printf ("link list empty");
}
else if ((*head)->next == *head) { // last entry
*head = NULL;
}
else
{
struct node *nodeToDelete = *head;
nodeToDelete->pre->next = nodeToDelete->next;
nodeToDelete->next->pre = nodeToDelete->pre;
*head = nodeToDelete->next;
}
}
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      12-04-2007
Mark Bluemel wrote:

Oh! and main returns int - I fixed it in my version but forgot to
mention it. (Ideally I should have used "int main(void)")
 
Reply With Quote
 
 
 
 
ratika
Guest
Posts: n/a
 
      12-12-2007
On 4 Dec, 15:09, Mark Bluemel <(E-Mail Removed)> wrote:
> ratika wrote:
> > On Dec 3, 4:36 pm, ratika <(E-Mail Removed)> wrote:
> >> can anyone tell me how to delete a certain node in a doubly circular
> >> link list

> > //circular doubly link list
> > #include<stdio.h>
> > #include<conio.h>

>
> Non-standard header
>
> > #include<alloc.h>

>
> Non-standard header
>
> [snip]
>
> > void main()
> > {
> > int item,ch;
> > struct node **t;

>
> It's not clear what "t" should be - I think it's probably the "head" of
> the list and should probably be simply "struct node *t". A more
> meaningful name would be good.
>
> > clrscr();

>
> Non-standard function.
>
> > *t=NULL;

>
> Fails here - t is not initialized, so you can't dereference it safely.
>
> > do
> > {
> > printf("\nMenu:\n1.add node\n2.display\n3.delete first node\n4.exit
> > \nenter the choice");
> > scanf("%d",&ch);

>
> scanf() is a poor choice for interactive input (and for much other
> input, IMHO)
>
> > switch(ch)
> > {
> > case 1:
> > printf("enter the value to be inserted");
> > scanf("%d",&item);
> > addnode(t,item);

>
> I think you want to pass "&t" not "t" here.
>
> > break;
> > case 2:
> > display(*t);

>
> If I'm right about t, then you should just pass t.
>
> > break;
> > case 4:
> > break;
> > case 3:
> > delfirst(*t);

>
> And here you probably need to pass "&t" again.
>
> > break;
> > default:
> > printf("wrong choice try again");
> > }
> > }while(ch!=3);

>
> Wrong exit check...
>
> > getch();

>
> Non-standard function
>
> > }

>
> > //functions starts
> > void addnode(struct node **t,int val)

>
> Where do you want to add your new nodes - are they ordered?
>
> > {
> > struct node *temp,*temp1;

>
> Hopeless names for your variables.
>
> > temp=(struct node*)malloc(sizeof(struct node));
> > temp->pre=NULL;
> > temp->data=val;
> > temp->next=NULL;
> > if(*t==NULL)
> > {
> > *t=temp;
> > temp->pre=*t;
> > temp->next=*t;
> > }
> > else
> > {
> > *t=temp1;

>
> What is this supposed to mean?
>
> > while(temp1->next!=*t)
> > {
> > temp1=temp1->next;
> > }
> > temp1->next->pre=temp;
> > temp->next=temp1->next;
> > temp->pre=temp1;
> > temp1->next=temp;
> > }
> > }

>
> > void display(struct node *t)
> > {
> > struct node *temp;

>
> Stupid name and not needed.
>
> > if(t==NULL)
> > {
> > printf("link list empty");
> > }
> > else
> > {
> > temp=t;
> > while(temp->next!=NULL)

>
> This is a doubly-linked list, isn't it?
> This condition won't be encountered.
>
>
>
>
>
> > {
> > printf("%d",temp->data);
> > temp=temp->next;
> > }
> > }
> > }
> > void delfirst(struct node *st)
> > {
> > if(st==NULL)
> > {
> > printf("link list empty");
> > }
> > else
> > {
> > struct node *temp;
> > temp=st;
> > temp->pre->next=temp->next;
> > st=temp->next;
> > temp->next->pre=temp->pre;
> > }
> > }

>
> This doesn't update the list head that you track in your main routine,
> does it? Nor does it deal with removing the last element.
>
> Here's a version, with fairly minimal changes, that seems to work
>
> //circular doubly link list
> #include<stdio.h>
> #include<stdlib.h>
>
> struct node
> {
> struct node *pre;
> int data;
> struct node *next;};
>
> void addnode (struct node **, int);
> void display (struct node *);
> void delfirst (struct node **);
> int
> main ()
> {
> int item, ch;
> struct node *head;
> head = NULL;
> do
> {
> printf ("\nMenu:\n1.add node\n2.display\n3.delete first
> node\n4.exit\nenter the choice");
> scanf ("%d", &ch);
> switch (ch)
> {
> case 1:
> printf ("enter the value to be inserted");
> scanf ("%d", &item);
> addnode (&head, item);
> break;
> case 2:
> display (head);
> break;
> case 4:
> break;
> case 3:
> delfirst (&head);
> break;
> default:
> printf ("wrong choice try again");
> }
> }
> while (ch != 4);
>
> }
>
> //functions starts
> void
> addnode (struct node **head, int val)
> {
> struct node *newNode;
> newNode = (struct node *) malloc (sizeof *newNode);
> newNode->pre = NULL;
> newNode->data = val;
> newNode->next = NULL;
> if (*head == NULL)
> {
> *head = newNode;
> newNode->pre = *head;
> newNode->next = *head;
> }
> else
> {
> struct node *currentNode = *head;
> while (currentNode->next != *head)
> {
> currentNode = currentNode->next;
> }
> newNode->next = currentNode->next;
> newNode->pre = currentNode;
> currentNode->next->pre = newNode;
> currentNode->next = newNode;
> }
>
> }
>
> void
> display (struct node *head)
> {
> struct node *currentNode;
> if (head == NULL)
> {
> printf ("link list empty");
> }
> else
> {
> currentNode = head;
> do
> {
> printf ("%d %p %p %p\n", currentNode->data, currentNode,
> currentNode->pre, currentNode->next);
> currentNode = currentNode->next;
> } while (currentNode != head);
> }}
>
> void
> delfirst (struct node **head)
> {
> if (*head == NULL)
> {
> printf ("link list empty");
> }
> else if ((*head)->next == *head) { // last entry
> *head = NULL;
> }
> else
> {
> struct node *nodeToDelete = *head;
> nodeToDelete->pre->next = nodeToDelete->next;
> nodeToDelete->next->pre = nodeToDelete->pre;
> *head = nodeToDelete->next;
> }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


thankyou sir for correcting my mistakes..i would run and tell you the
results which i get
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
list box with items for deletion passed from php array student4lifer@gmail.com Javascript 2 12-27-2008 11:21 AM
RE: Link Link Link =?Utf-8?B?REw=?= Windows 64bit 0 05-17-2005 12:15 PM
Re: Link Link Link DANGER WILL ROBINSON!!! Kevin Spencer ASP .Net 0 05-17-2005 10:41 AM
Google History Deletion Frank Adey Firefox 1 02-23-2005 05:24 PM
Deletion of read messages in newsgroup heim Firefox 10 12-12-2004 12:39 PM



Advertisments