#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}node;
void insert(node *pointer, int data)
{
/* Iterate through the list till we encounter the last node.*/
while(pointer->next!=NULL)
{
pointer = pointer -> next;
}
/* Allocate memory for the new node and put data in it.*/
pointer->next = (node *)malloc(sizeof(node));
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
}
int find(node *pointer, int key)
{
pointer = pointer -> next; //First node is dummy node.
/* Iterate through the entire linked list and search for the key. */
while(pointer!=NULL)
{
if(pointer->data == key) //key is found.
{
return 1;
}
pointer = pointer -> next;//Search in the next node.
}
/*Key is not found */
return 0;
}
void delete(node *pointer, int data)
{
/* Go to the node for which the node next to it has to be deleted */
while(pointer->next!=NULL && (pointer->next)->data != data)
{
pointer = pointer -> next;
}
if(pointer->next==NULL)
{
printf("Element %d is not present in the list\n",data);
return;
}
/* Now pointer points to a node and the node next to it has to be removed */
node *temp;
temp = pointer -> next;
/*temp points to the node which has to be removed*/
pointer->next = temp->next;
/*We removed the node which is next to the pointer (which is also temp) */
free(temp);
/* Beacuse we deleted the node, we no longer require the memory used for it .
free() will deallocate the memory.
*/
return;
}
void print(node *pointer)
{
if(pointer==NULL)
{
return;
}
printf("%d ",pointer->data);
print(pointer->next);
}
int main()
{
/* start always points to the first node of the linked list.
temp is used to point to the last node of the linked list.*/
node *start,*temp;
start = (node *)malloc(sizeof(node));
temp = start;
temp -> next = NULL;
/* Here in this code, we take the first node as a dummy node.
The first node does not contain data, but it used because to avoid handling special cases in insert and delete functions. */
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Print\n");
printf("4. Find\n");
printf("Enter your choice: ");
while(1)
{
int query;
scanf("%d",&query);
if(query==1)
{
int data;
printf("Enter the element: ");
scanf("%d",&data);
insert(start,data);
printf("Press 3 for display: ");
}
else if(query==2)
{
int data;
printf("Delete the element: ");
scanf("%d",&data);
delete(start,data);
printf("Press 3 for display: ");
}
else if(query==3)
{
printf("The list is ");
print(start->next);
printf("\n");
}
else if(query==4)
{
int data;
printf("Find the element: ");
scanf("%d",&data);
int status = find(start,data);
if(status)
{
printf("Element Found\n");
}
else
{
printf("Element Not Found\n");
}
}
}
}
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
struct Node *prev;
}node;
void insert(node *pointer, int data)
{
/* Iterate through the list till we encounter the last node.*/
while(pointer->next!=NULL)
{
pointer = pointer -> next;
}
/* Allocate memory for the new node and put data in it.*/
pointer->next = (node *)malloc(sizeof(node));
(pointer->next)->prev = pointer;
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
}
int find(node *pointer, int key)
{
pointer = pointer -> next; //First node is dummy node.
/* Iterate through the entire linked list and search for the key. */
while(pointer!=NULL)
{
if(pointer->data == key) //key is found.
{
return 1;
}
pointer = pointer -> next;//Search in the next node.
}
/*Key is not found */
return 0;
}
void delete(node *pointer, int data)
{
/* Go to the node for which the node next to it has to be deleted */
while(pointer->next!=NULL && (pointer->next)->data != data)
{
pointer = pointer -> next;
}
if(pointer->next==NULL)
{
printf("Element %d is not present in the list\n",data);
return;
}
/* Now pointer points to a node and the node next to it has to be removed */
node *temp;
temp = pointer -> next;
/*temp points to the node which has to be removed*/
pointer->next = temp->next;
temp->prev = pointer;
/*We removed the node which is next to the pointer (which is also temp) */
free(temp);
/* Beacuse we deleted the node, we no longer require the memory used for it .
free() will deallocate the memory. */
return;
}
void print(node *pointer)
{
if(pointer==NULL)
{
return;
}
printf("%d ",pointer->data);
print(pointer->next);
}
int main()
{
/* start always points to the first node of the linked list.
temp is used to point to the last node of the linked list.*/
node *start,*temp;
start = (node *)malloc(sizeof(node));
temp = start;
temp -> next = NULL;
temp -> prev = NULL;
/* Here in this code, we take the first node as a dummy node.
The first node does not contain data, but it used because to avoid handling special cases
in insert and delete functions.
*/
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Print\n");
printf("4. Find\n");
printf("Enter Choice: ");
while(1)
{
int query;
scanf("%d",&query);
if(query==1)
{
int data;
printf("Enter the element: ");
scanf("%d",&data);
insert(start,data);
printf("Enter 3 for display: ");
}
else if(query==2)
{
int data;
printf("Delete the element: ");
scanf("%d",&data);
delete(start,data);
printf("Enter 3 for display: ");
}
else if(query==3)
{
printf("The list is: ");
print(start->next);
printf("\n");
//main();
}
else if(query==4)
{
int data;
printf("Find the element: ");
scanf("%d",&data);
int status = find(start,data);
if(status)
{
printf("Element Found\n");
}
else
{
printf("Element Not Found\n");
}
}
}
}
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
int data;
struct DCLL
{
int info;
struct DCLL *rptr, *lptr;
}*start = NULL, *temp, *neww, *prev, *end = NULL;
void ins_beg_dcll();
void ins_end_dcll();
void del_beg_dcll();
void del_end_dcll();
void create_dcll();
void travers();
void insert();
void delet();
void search();
void update();
void main()
{
int choice;
char ch, ch2 = 'y';
do
{
if (start == NULL)
{
do
{
// clrscr();
printf("\nError : Linked LIst is Empty Want to Create (Y/N).. ");
fflush(stdin);
scanf("%c", &ch);
if (ch == 'n' || ch == 'N')
{
exit(0);
}
else if (ch == 'y' || ch == 'Y')
{
create_dcll();
break;
}
else
{
printf("\nError : Invalid Choice");
}
} while (ch == 'y' || ch == 'Y');
}
else
{
// clrscr();
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Traverse");
printf("\n4. Search");
printf("\n5. Update");
printf("\n\nPress 0 to Exit");
printf("\nEnter your Choice : ");
scanf("%d", &choice);
switch (choice)
{
case 0:
exit(0);
break;
case 1:
insert();
travers();
break;
case 2:
delet();
travers();
break;
case 3:
travers();
break;
case 4:
search();
break;
case 5:
update();
break;
default:
printf("\nError : Invalid Choice. \n");
}
printf("\nWant to Continue (Y/N)... ");
fflush(stdin);
scanf("%c", &ch2);
}
}while (ch2 == 'y' || ch2 == 'Y');
}
void create_dcll()
{
char ch;
do
{
//clrscr();
printf("\nEnter Node Info");
printf("\n\nEnter Info : ");
scanf("%d", &data);
temp = (struct DCLL *)malloc(sizeof(struct DCLL));
temp->info = data;
temp->rptr = start;
temp->lptr = start;
if (start == NULL)
{
start = temp;
end = temp;
start->rptr = start;
start->lptr = start;
}
else
{
temp->lptr = end;
end->rptr = temp;
temp->rptr = start;
start->lptr = temp;
end = temp;
}
printf("\nDo you want to insert another Node (Y/N)... ");
fflush(stdin);
scanf("%c", &ch);
} while (ch == 'y' || ch == 'Y');
}
void ins_beg_dcll()
{
neww->rptr = start;
start->lptr = neww;
neww->lptr = end;
end->rptr = neww;
start = neww;
}
void ins_end_dcll()
{
neww->lptr = end;
end->rptr = neww;
neww->rptr = start;
start->lptr = neww;
end = neww;
}
void travers()
{
temp = start;
if (start == NULL)
{
printf("\nError : Linked list is Empty");
}
else
{
if (start == end)
{
printf("\n%d", temp->info);
}
else
{
printf("\n%d", temp->info);
do
{
temp = temp->rptr;
printf("\n%d", temp->info);
} while (temp != end);
}
}
}
void insert()
{
int pos, count = 1;
printf("\nEnter Position : ");
scanf("%d", &pos);
printf("\nEnter Info :");
scanf("%d", &data);
neww = (struct DCLL *)malloc(sizeof(struct DCLL));
neww->info = data;
temp = start;
if (pos == 1)
{
ins_beg_dcll();
}
else
{
while (count != pos - 1 && temp->rptr != start)
{
count++;
temp = temp->rptr;
}
if (temp->rptr == start && count < pos - 1)
{
printf("\nError : Invalid Position...");
}
else if (temp->rptr == start && count == pos - 1)
{
ins_end_dcll();
}
else
{
neww->rptr = temp->rptr;
neww->lptr = temp;
temp->rptr->lptr = neww;
temp->rptr = neww;
}
}
}
void delet()
{
int pos, count = 1;
printf("\nEnter Position : ");
scanf("%d", &pos);
temp = start;
if (pos == 1)
{
del_beg_dcll();
}
else
{
while (count != pos && temp != end)
{
prev = temp;
temp = temp->rptr;
count++;
}
if (temp == end && count < pos - 1)
{
printf("\nError : Invalid Position.");
}
else if (temp == end && count == pos)
{
del_end_dcll();
}
else
{
prev->rptr = temp->rptr;
temp->rptr->lptr = prev;
free(temp);
}
}
}
void del_beg_dcll()
{
start = start->rptr;
start->lptr = end;
end->rptr = start;
free(temp);
}
void del_end_dcll()
{
end = end->lptr;
end->rptr = start;
start->lptr = end;
free(temp);
}
void search()
{
int src, count = 1;
printf("\nEnter Info : ");
scanf("%d", &src);
temp = start;
do
{
prev = temp;
if (prev->info == src)
{
printf("\nSearch Value found at %d Position.", count);
break;
}
count++;
temp = temp->rptr;
} while (prev != end);
if (temp == start)
{
printf("\nError : Search value doesn't exists.");
}
}
void update()
{
int pos, count = 1, dt;
printf("\nEnter Position : ");
scanf("%d", &pos);
printf("\nEnter New Info : ");
scanf("%d", &dt);
temp = prev = start;
do
{
if (count == pos && prev != end)
{
prev->info = dt;
break;
}
prev = temp;
temp = temp->rptr;
count++;
} while (prev != end);
if (temp == start)
{
printf("\nError : Invalid Choice ");
}
}