Wait for code to load....
#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node *prev;
struct node *next;
};
struct node *head = NULL;
struct node *start = NULL;
struct node *
create_new_node ()
{
struct node *temp = (struct node *) malloc (sizeof (struct node));
if (!temp)
{
printf (" memory not allocated ..exiting...\n");
getchar ();
exit (1);
}
return temp;
}
void
insert (int x)
{
struct node *var = create_new_node ();
var->item = x;
var->next = NULL;
if (start == NULL && head == NULL)
{
var->prev = NULL;
head = start = var;
}
else
{
var->prev = head;
head->next = var;
head = var;
}
}
void
display (struct node *start)
{
struct node *temp;
temp = start;
if (temp == NULL)
return;
else
{
while (temp != NULL)
{
printf ("%d ->", temp->item);
temp = temp->next;
}
}
}
void
ldelete (int x, struct node *start)
{
struct node *temp = start;
struct node *temp2;
while (temp != NULL)
{
if (temp->item == x)
{
if (temp->prev == NULL)
start = temp->next;
else if (temp->next == NULL)
(temp->prev)->next = NULL;
else
{
temp2 = temp->prev;
temp2->next = temp->next;
}
free (temp);
return;
}
temp = temp->next;
}
printf ("no such data exist\n");
return;
}
void
main ()
{
int i, choice;
system ("clear");
do
{
printf
("\n\nEnter 1 to insert and 2 to display 3 to delete 4 to exit\n\n");
scanf ("%d", &choice);
if (choice == 1)
{
printf ("Enter data element to be inserted\n");
scanf ("%d", &i);
insert (i);
}
else if (choice == 2)
{
display (start);
}
else if (choice == 3)
{
printf ("Enter data to be deleted\n");
scanf ("%d", &i);
ldelete (i, start);
}
}
while (choice != 4);
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node *prev;
struct node *next;
};
struct node *head = NULL;
struct node *start = NULL;
struct node *
create_new_node ()
{
struct node *temp = (struct node *) malloc (sizeof (struct node));
if (!temp)
{
printf (" memory not allocated ..exiting...\n");
getchar ();
exit (1);
}
return temp;
}
void
insert (int x)
{
struct node *var = create_new_node ();
var->item = x;
var->next = NULL;
if (start == NULL && head == NULL)
{
var->prev = NULL;
head = start = var;
}
else
{
var->prev = head;
head->next = var;
head = var;
}
}
void
display (struct node *start)
{
struct node *temp;
temp = start;
if (temp == NULL)
return;
else
{
while (temp != NULL)
{
printf ("%d ->", temp->item);
temp = temp->next;
}
}
}
void
ldelete (int x, struct node *start)
{
struct node *temp = start;
struct node *temp2;
while (temp != NULL)
{
if (temp->item == x)
{
if (temp->prev == NULL)
start = temp->next;
else if (temp->next == NULL)
(temp->prev)->next = NULL;
else
{
temp2 = temp->prev;
temp2->next = temp->next;
}
free (temp);
return;
}
temp = temp->next;
}
printf ("no such data exist\n");
return;
}
void
main ()
{
int i, choice;
system ("clear");
do
{
printf
("\n\nEnter 1 to insert and 2 to display 3 to delete 4 to exit\n\n");
scanf ("%d", &choice);
if (choice == 1)
{
printf ("Enter data element to be inserted\n");
scanf ("%d", &i);
insert (i);
}
else if (choice == 2)
{
display (start);
}
else if (choice == 3)
{
printf ("Enter data to be deleted\n");
scanf ("%d", &i);
ldelete (i, start);
}
}
while (choice != 4);
}
Comments
Post a Comment
share your thoughts ....