R
ratika
can anyone tell me how to delete a certain node in a doubly circular
link list
link list
ratika said:can anyone tell me how to delete a certain node in a doubly circular
link list
ratika said:can anyone tell me how to delete a certain node in a doubly circular
link list
can anyone tell me how to delete a certain node in a doubly circular
link list
[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.
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?
ratika said:can anyone tell me how to delete a certain node in a doubly circular
link list
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
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>
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;
}
}
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.
*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 -
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.