Question

Build a double linked list to accommodate the following numbers: 6,3,2,1,5, 0 in C Programming language...

Build a double linked list to accommodate the following numbers: 6,3,2,1,5, 0 in C Programming language

A. Print list forward

B. Sort the list

C. Delete a node with 3

D Print the list backwards

E. Insert a node with 10 between the first and the second nodes

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<math.h>
#include<malloc.h>
//creatig node
struct node
{
   int data;
   struct node*next;
   struct node *prev;
};
void swapdata(struct node *temp,struct node *temp1)
{
   int d=temp->data;
   temp->data=temp1->data;
   temp1->data=d;
}
struct node *head;//head gloabl variable
struct node *insert(struct node *head,int x)//inserting node in the linked list(at end of list)
{
   struct node *newnode=(struct node*)malloc(sizeof(struct node));
   newnode->data=x;
   newnode->next=NULL;
   if(head==NULL)
   {
       newnode->prev=NULL;
       head=newnode;
   }
   else
   {
       struct node *temp=head;
       while(temp->next!=NULL)
       {
           temp=temp->next;
       }
       newnode->prev=temp;
       temp->next=newnode;
   }
   return head;
}
void printForward(struct node *head)//function to print data in forward direction
{
   struct node *t=head;
   while(t!=NULL)//travesign upto last value
   {
       printf("%d ",t->data);
       t=t->next;
   }
   printf("\n");
}
struct node *insertBetween(struct node *head,int value)//inserting node in between first and second nod
{
   struct node *temp=head;
   if(head==NULL)
   return head;
   if(head->next==NULL)
   return head;
   struct node *newnode=(struct node*)malloc(sizeof(struct node));
   newnode->data=value;
   newnode->prev=temp;
   newnode->next=temp->next;
   temp->next->prev=newnode;
   temp->next=newnode;
   return head;
}
struct node *Delete(struct node *head,int value)//deleting node having data 3
{
   if(head==NULL)
       return head;
   if(head->next==NULL)
   {
       free(head);
       head=NULL;
       return head;
   }
   struct node *temp=head;
   while(temp->data!=value)
   {
       temp=temp->next;
   }
  
   temp->prev->next=temp->next;
   temp->next->prev=temp->prev;
   free(temp);
  
   return head;
}
void sort(struct node *head) //sorting data in the linked list usign bubble sort
{
int f, i;
struct node *temp;
struct node *temp1 = NULL;

/* Checking for empty list */
if (head == NULL)
return;

do
{
f = 0;
temp = head;
while (temp->next != temp1)
{
if (temp->data > temp->next->data)
{
swapdata(temp, temp->next);
f = 1;
}
temp= temp->next;
}
temp1 = temp;
}
while (f);
}


void printBackward(struct node *head)//printing data in backward order
{
   struct node *t=head;
   while(t->next!=NULL)//traversing upto last node
       {
           t=t->next;
       }
   while(t!=NULL)//traversing in backward using prev pointer
   {
       printf("%d ",t->data);
       t=t->prev;  
   }  
   printf("\n");
}
int main()
{
   head=insert(head,6);
   head=insert(head,3);
   head=insert(head,2);
   head=insert(head,1);
   head=insert(head,5);
   head=insert(head,0);
   printf("Forward traversing\n");
   printForward(head);
   printf("Backward traversing\n");
   printBackward(head);
   printf("After inserting Node 10 in between first and second node\n");
   head=insertBetween(head,10);
   printForward(head);
   printf("After deleting node 3\n");
   head=Delete(head,3);
   printForward(head);
   printf("After Sorting\n");
   sort(head);
   printForward(head);
  
}

Add a comment
Know the answer?
Add Answer to:
Build a double linked list to accommodate the following numbers: 6,3,2,1,5, 0 in C Programming language...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using C++, NOT C language 1. Write a function called insert() to insert a node to...

    using C++, NOT C language 1. Write a function called insert() to insert a node to the beginning of a linked list. The data is passed into the function. For example insert(8) will insert a node at the beginning of the list with the number 8 in the data field. Each node in the linked list is a ListNode struct as discussed in class. 2. Write a function called print() that will traverse the entire linked list and print out...

  • Build a singly linked list with 10 noes. The data are "This is my first project...

    Build a singly linked list with 10 noes. The data are "This is my first project in Data structure and algorithm". First node is "This" second "is" and so on. Build insert function, insert "the " before "Data" and delete function, delete "my" before "first". Search "in" and output it location and then append "C++" and output the linked list again. In writing this program use menu which includes ”1. Create; 2. Insert; 3. Delete; 4. Append; 5. Search; 6....

  • Write a function to implement linked list consisting of five nodes. Store the data in the...

    Write a function to implement linked list consisting of five nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming George, Paul, Ross, Joe, Elaine, Robert1 Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes in the following order. (You have...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • build a singly linked list in C++, input data from keyboard, this list includes 10 nodes....

    build a singly linked list in C++, input data from keyboard, this list includes 10 nodes. The data are "This is my first project in Data Structure and algorithm", the first node is is "This", second "is" and so on.

  • c++ only Program 2: Linked List Class For this problem, let us take the linked list...

    c++ only Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we’ll expand its functionality and make it a doubly linked list with the ability to traverse in both directions.    Since the list is doubly linked, each node will have the following structure: struct Node { int number; Node * nextNode;...

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • Write a function to implement linked list consisting of six nodes. Store the data in the...

    Write a function to implement linked list consisting of six nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming use these as names as nodes George, Paul, Ross, Joe, Elaine, Robert1. Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • a. Using C++, define a node structure of the linked list (e.g. value is an integer,...

    a. Using C++, define a node structure of the linked list (e.g. value is an integer, next is a node type pointer), construct a linked list of 10 nodes and assign random numbers as the nodes’ values. Use loop to track and print from the first node to the last and output all nodes’ values. Finally, free all memories of the linked list.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT