Question

In: Computer Science

For the following two questions you need to provide a code in C 1. Build a...

For the following two questions you need to provide a code in C 1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node with 3 Delete the node with 10 3. Print the resulting list forward and backwards.

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

struct node 
{
   int data;    
   struct node *next;
   struct node *prev;
};

struct node *head = NULL;

struct node *last = NULL;

struct node *current = NULL;

bool isEmpty()
{
   return head == NULL;
}

void display1() 
{

   struct node *ptr = head;    
   while(ptr != NULL) 
   {        
      printf("%d    ",ptr->data);
      ptr = ptr->next;
   }
    
}

void display2() 
{

   struct node *ptr = last;
    
   while(ptr != NULL) 
   {    
      printf("%d    ",ptr->data);  
      ptr = ptr ->prev;
   }
}

void insertFirst( int data) 
{
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->data = data;
    
   if(isEmpty()) 
   {
      last = link;
   }
   else 
   {
      head->prev = link;
   }
   link->next = head;
   head = link;
}

void insertLast( int data) 
{
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->data = data;   
   if(isEmpty()) 
   {
      last = link;
   } 
   else 
   {
      last->next = link;     
      link->prev = last;
   }
   last = link;
}

struct node* deleteFirst() 
{
   struct node *tempLink = head;   
   if(head->next == NULL)
   {
      last = NULL;
   } 
   else 
   {
      head->next->prev = NULL;
   } 
   head = head->next;
   return tempLink;
}

struct node* deleteLast() 
{ 
   struct node *tempLink = last;   
   if(head->next == NULL) 
   {
      head = NULL;
   } 
   else 
   {
      last->prev->next = NULL;
   } 
   last = last->prev;
   return tempLink;
}

struct node* delete(int key) 
{
   struct node* current = head;
   struct node* previous = NULL;   
   if(head == NULL) 
   {
      return NULL;
   }
   while(current->data != key) 
   {     
      if(current->next == NULL) 
      {
         return NULL;
      } 
      else 
      {
         previous = current;
         current = current->next;             
      }
   }
   if(current == head) 
   {
      head = head->next;
   } 
   else 
   {
      current->prev->next = current->next;
   }    
   if(current == last) 
   {
      last = current->prev;
   } 
   else 
   {
      current->next->prev = current->prev;
   } 
   return current;
}

bool insertAfter(int data, int key) 
{   
   struct node *current = head;    
   if(head == NULL) 
   {
      return false;
   }
   while(current->data != key) 
   { 
      if(current->next == NULL) 
      {
         return false;
      } 
      else 
      {           
         current = current->next;
      }
   }

   struct node *newLink = (struct node*) malloc(sizeof(struct node));
   newLink->data = data;

   if(current == last) 
   {
      newLink->next = NULL; 
      last = newLink; 
   } 
   else 
   {
      newLink->next = current->next;         
      current->next->prev = newLink;
   } 
   newLink->prev = current; 
   current->next = newLink; 
   return true; 
}

int main() 
{
   insertFirst(5);
   /*display1();
   printf("\n");
   display2();
   printf("\n");*/
   insertFirst(3);
   /*display1();
   printf("\n");
   display2();
   printf("\n");*/
   insertLast(10);
   /*display1();
   printf("\n");
   display2();
   printf("\n");*/
   insertAfter(7,5);
   /*display1();
   printf("\n");
   display2();
   printf("\n");*/
   delete(7);
   /*display1();
   printf("\n");
   display2();
   printf("\n");*/
   delete(3);
   /*display1();
   printf("\n");
   display2();
   printf("\n");*/
   delete(10);
   display1();
   printf("\n");
   display2();
}

Stepwise output of code:

Desired output:


Related Solutions

C++ Simple Programming Assignment you need to build off of the code below: #include using namespace...
C++ Simple Programming Assignment you need to build off of the code below: #include using namespace std; // class definition class Fraction {         // two data members         // one representing numerator         int numerator;         // other, representing denominator         int denominator;         public:                 void set (int numerator, int denominator);                 Fraction addedTo (Fraction f);                 Fraction subtract (Fraction f);                 Fraction multipliedBy (Fraction f);                 Fraction dividedBy (Fraction f);                 bool isEqualTo (Fraction f);                 void print (); }; void Fraction :: set (int numerator, int denominator) {         this...
NEED IN C++ For the following programming problems, you need to time a section of code...
NEED IN C++ For the following programming problems, you need to time a section of code in C++. For example, the following statements time the execution of the function doSomething: #include clock_t start = clock(); doSomething(); clock_t finish = clock(); double overallTime = static_cast(finish - start)/ CLOCKS_PER_SEC; Consider the following two loops: //Loop A for(i = 1; i <= n; i++)    for(j = 1; j <= 10000; j++)     sum = sum + j; //Loop B for(i = 1;...
Code in C# Part 1 Build a Series class that has the following attributes and behaviors:...
Code in C# Part 1 Build a Series class that has the following attributes and behaviors: Attributes 1.) A name -- this can be any string value (i.e. “Firefly”) 2.) A number of episodes -- this can be any non-negative integer value 3.) An episode length -- this can be a double value, in hours, representing the length of an episode (i.e. 0.5 for a half-hour episode, or 0.37 for a 23 minute episode) 4.) A forKids attribute that holds...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names intArray17.b. Create a loop to calculate the sum of every element in the first column.c. Create a loop to calculate the sum of every element in the array.
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names...
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names intArray17. Create a loop to calculate the sum of every element in the first column. Create a loop to calculate the sum of every element in the array.
Need C++ code for following with a document describing the code as well: Write a program...
Need C++ code for following with a document describing the code as well: Write a program to implement the game of Tic Tac Toe Four. The game is played on a 4 × 4 chessboard, within 2 players. The player who is playing "X" always goes first. Players alternate placing Xs and Os on the board until either one player has four in a row, horizontally, vertically, or diagonally; or all 16 squares are filled(which is a draw). The program...
Provide a thoughtful 250+ total word response to the following questions (you will need to answer...
Provide a thoughtful 250+ total word response to the following questions (you will need to answer all parts of this question) by clicking REPLY below. Please separate your answers to clearly differentiate your answers to each question below. It makes it easier to see your specific response to each part. – The Price is Right? As a customer, which pricing strategy do you prefer – high/low pricing or everyday low-pricing (EDLP)? Why? Looking at the advantages of each strategy listed...
Provide a thoughtful 250+ total word response to the following questions (you will need to answer...
Provide a thoughtful 250+ total word response to the following questions (you will need to answer all parts of this question) by clicking REPLY below. Please separate your answers to clearly differentiate your answers to each question below. It makes it easier to see your specific response to each part. DQ2 – Variety Show Variety, assortment, and product availability are the cornerstones of the merchandise planning process. You’ll see the importance of this in Chapter 11! Provide an example of...
Provide a thoughtful 250+ total word response to the following questions (you will need to answer...
Provide a thoughtful 250+ total word response to the following questions (you will need to answer all parts of this question) by clicking REPLY below. Please separate your answers to clearly differentiate your answers to each question below. It makes it easier to see your specific response to each part. DQ4 – Staples? Yeah, We’ve Got That What are the differences among staple, fashion and seasonal merchandise? NOTE: Don’t just explain what each is, I want you to explain the...
answer is following short questions : 1) The accountants provide useful information and the users need...
answer is following short questions : 1) The accountants provide useful information and the users need something else, explain? 2) According to accounting income should have high quality to be useful, explain? 3) The accounting standards and procedures are in a good situation to achieve accounting objective, but still users looking for more, explain?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT