deletion in link list

C

Chris Dollin

ratika said:
can anyone tell me how to delete a certain node in a doubly circular
link list

(a) Yes, someone can [1].

(b) This is not a C question: comp.programming would be a better choice.

(c) You don't have books or chums or unsuccessful web searches? You haven't
tried it yourself /at all/? Trying first will help you understand the
problem; if you ask /then/, trying helps you to describe the problem,
us to understand it, and you to understand the answers.

(d) To remove a node X from its circular list, make its predecessor's
forward link point to X's successor, and vice-versa. Watch out for
the degenerate case.

(e) /Write test cases/.

(f) Reorder as applicable.

[1] Programmers [2] are notable for their literal [3] interpretation of
questions.

[2] OK, /some/ programmers [3].

[3] And maybe it's rhetorical, not literal [4].

[4] Or a cover for the control messages to the mind-control satellites [4].

[4] There is no footnote 4.
 
R

ravi

can anyone tell me how to delete a certain node in a doubly circular
link list

Let ptr holds the address of the node to be deleted then

(ptr->back)->next = ptr->next;
(ptr->next)->back = ptr->back;
free(ptr);

don't forget to check the boundary conditions
 
S

santosh

Chris Dollin wrote:

[1] Programmers [2] are notable for their literal [3] interpretation
[of
questions.

[2] OK, /some/ programmers [3].

[3] And maybe it's rhetorical, not literal [4].

[4] Or a cover for the control messages to the mind-control satellites
[[4].

[4] There is no footnote 4.

Maybe you should use structured control flow instead of gotos?
 
C

Chris Dollin

santosh said:
Chris Dollin wrote:

[1] Programmers [2] are notable for their literal [3] interpretation
[of
questions.

[2] OK, /some/ programmers [3].

[3] And maybe it's rhetorical, not literal [4].

[4] Or a cover for the control messages to the mind-control satellites
[[4].

[4] There is no footnote 4.

Maybe you should use structured control flow instead of gotos?

I do.
 
E

Eric Sosman

ratika said:
can anyone tell me how to delete a certain node in a doubly circular
link list

<off-topic reason="better asked in comp.programming">

Draw a picture of the list, showing the links as arrows.
Then draw another picture of the list as it would be if the
deleted node were simply not there at all. Study the two
pictures to see which arrows have changed, and then write
code to make those changes in the actual list.

</off-topic>
 
T

Thomas X. Iverson

can anyone tell me how to delete a certain node in a doubly circular
link list

read your book and understand the knowledge points yourself
try it yourself first
remember that practice makes perfect
 
R

ratika

read your book and understand the knowledge points yourself
try it yourself first
remember that practice makes perfect

i know the concept of the program .i had made the program too . but
the problem is that it is running only two times not more than that
 
R

ratika

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>
#include<alloc.h>
struct node
{
struct node *pre;
int data;
struct node *next;
};
void addnode(struct node** , int);
void display(struct node*);
void delfirst(struct node*);
void main()
{
int item,ch;
struct node **t;
clrscr();
*t=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(t,item);
break;
case 2:
display(*t);
break;
case 4:
break;
case 3:
delfirst(*t);
break;
default:
printf("wrong choice try again");
}
}while(ch!=3);
getch();
}


//functions starts
void addnode(struct node **t,int val)
{
struct node *temp,*temp1;
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;
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;
if(t==NULL)
{
printf("link list empty");
}
else
{
temp=t;
while(temp->next!=NULL)
{
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 is the program made by me please see and tell me the errors
 
M

Mark Bluemel

//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.

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...

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;
}
}
 
M

Mark Bluemel

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)")
 
R

ratika

ratika said:
//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.

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...

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top