Coding done in C, builds off of these previous questions. Create a doubly linked
ID: 3736944 • Letter: C
Question
Coding done in C, builds off of these previous questions.
Create a doubly linked list of structs (or nodes in this case) containing an int ID, and a double
Value. The ID will count from 0 to the end of the list, and this list should support any number of nodes.
As part of this assignment your program must also implement linked list functions in the following
problems.
Create a function that steps through a linked list and prints the ID and val on each separate line.
This function will be used to test your linked list later.
Create a function that adds nodes to the end of the list. This function must work for ANY
conditions (add to empty list, add to one element list, etc), and you must test this function in your main
function for credit by adding five elements and then using the previous function to display the linked
list.
Create a function that removes nodes from the end of the list. This function must work for ANY
conditions (remove from one element list, remove from empty list, etc), and you must test this function
in your main function for credit by calling remove six times on your linked list and then using the
previous function to display the linked list (which should be empty).
Explanation / Answer
//--------I compiled and run program in dev cpp
//----------I didnt get what value of id you want to put so I assumed that id= position of node
//----------i.e id=1 for first node id=2 for second node and so on
//-------------------dont ferget to THUMBS UP------------------
//IF YOU HAV DOUBT COMMENT BELOW I WILL REPLY ASAP
/*
* C Program to Implement a Doubly Linked List & provide Insertion, Deletion at end& Display Operations
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *prev;
int id;
double d;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp3;
void insert();
void display();
void delete();
int count = 0;
void main()
{
int ch;
h = NULL;
temp = temp1 = temp2 = NULL;
// printf(" 1 - Insert at beginning");
printf(" 1 - Insert at end");
printf(" 2 - Delete at end");
printf(" 3 - Display from beginning");
printf(" 4 - Exit");
while (1)
{
printf(" Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf(" Wrong choice menu");
}
}
}
/* TO create an empty node */
//each time when you call insert create method will get called
void create()
{
int idnode;
double doubleval;
count++;
//malloc function create dynamic memory for node of size node
temp =(struct node *)malloc(1*sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf(" Enter double valaue to node : ");
scanf("%d", &doubleval);
temp->id = count;
temp->d=doubleval;
}
/* To insert at end */
//temp1 will always hold last node
void insert()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
/* To delete an element */
//here temp2 is used to delete last node
void delete()
{
if(h == NULL)
{
printf("Unable to delete. List is empty. "); //this will display when you are trying to delete empty list
}
else
{
if(count==1)
{
temp2=NULL;
h=NULL;
}
else
{
temp2=temp1;
temp1=temp1->prev;
temp1->next = NULL; // Remove link to of 2nd last node with last node
free(temp2);
} // Delete the last node
printf("SUCCESSFULLY DELETED NODE FROM END OF THE LIST. ");
count--;
}
}
/* Traverse from beginning */
void display()
{
temp3 = h;
int c=count;
if (c==0)
{
printf("List empty to display ");
return;
}
printf(" Linked list elements from begining : ");
while (c)
{
printf(" id= %d ", temp3->id);
printf(" double value= %d ", temp3->d);
temp3 = temp3->next;
c--;
}
//printf(" id= %d ", temp3->id);
//printf(" double value= %d ", temp3->d);
}