Question

In: Computer Science

Can you make this singular linked list to doubly linked list Create a Doubly Linked List....

Can you make this singular linked list to doubly linked list

Create a Doubly Linked List.

Use this to create a Sorted Linked List,

Use this to create a prioritized list by use. Bring to front those links recently queried.

-----link.h------
#ifndef LINK_H
#define LINK_H

struct Link{
int data;
Link *lnkNxt;
};

#endif /* LINK_H */

----main.cpp----

//System Level Libraries
#include <iostream> //I/O Library
using namespace std; //Libraries compiled under std
#include"Link.h"
//Global Constants - Science/Math Related
//Conversions, Higher Dimensions

//Function Prototypes
void pop_front(Link *&front);
void pop_back(Link *&front);
void push_front(int data, Link *&front);
void push_back(int data, Link *&front);
void prntLst(Link *front);
void dstryLst(Link *&front);
void prntLst(Link* front);
Link *fillLnk(int); //Fill a Link
Link *fillLst(int); //Populate the List
//Execution Begins Here!

int main(int argc, char** argv) {
//Random Number Seed Set Here


//Variable Declarations
Link *lnk1;

//Variable Initialization
lnk1 = fillLst(5);

//Mapping Process Inputs to Outputs

//Display Outputs
cout << endl << "The Printed List" << endl;
prntLst(lnk1);


//Test new functions
pop_front(lnk1);
cout << endl << "The Printed List after popfront function" << endl << endl;
prntLst(lnk1);
pop_back(lnk1);
cout << endl << "The Printed List after popback function" << endl << endl;
prntLst(lnk1);
push_front(10, lnk1);
cout << endl << "The Printed List after pushfront function" << endl << endl;
prntLst(lnk1);
push_back(20, lnk1);
cout << endl << "The Printed List after pushback function" << endl << endl;
prntLst(lnk1);
//Clean Up, Delete all the elements in the list
delete (lnk1);

//Exit stage right!
return 0;
}

// function to remove the front element

void pop_front(Link *&front) {
if (front != NULL) { //if not the end of the list
Link *temp = front; // new pointer set equal to front
front = front->lnkNxt; //front points to the next node
delete(temp);
}
}

// function to remove the back element

void pop_back(Link *&front) {
if (front != NULL) {
Link *curr = front;
Link *prev = NULL;

while (curr->lnkNxt != NULL) {
prev = curr;
curr = curr->lnkNxt;
}

if (prev == NULL)
front = NULL;
else
prev->lnkNxt = curr->lnkNxt;
delete(curr);
}
}

// function to insert data at the front

void push_front(int data, Link *&front) {
Link *node = new Link;
node->data = data;
node->lnkNxt = front;
front = node;
}

// function to insert data at the end

void push_back(int data, Link *&front) {
Link *node = new Link;
node->data = data;
node->lnkNxt = NULL;

if (front == NULL)
front = node;
else {
Link *curr = front;

while (curr->lnkNxt != NULL)
curr = curr->lnkNxt;

curr->lnkNxt = node;
}
}

// function to print the list

void prntLst(Link *front) {
Link *curr = front;
while (curr != NULL) {
cout << curr->data << endl;
curr = curr->lnkNxt;
}
}

Link *fillLst(int nLinks) {
Link *front = fillLnk(1); //first Link
Link *next = front; // pointer to move thru the List
for (int i = 2; i <= nLinks; i++) {//attaches one link after another
Link *newLnk = fillLnk(i);
next->lnkNxt = newLnk;
next = newLnk;
}
next->lnkNxt = NULL;
return front;
}
//fills the list
Link *fillLnk(int data) {
Link *lnk = new Link;
lnk->data = data;
return lnk;
}

// function to destroy the list
void dstryLst(Link *&front) {
while (front != NULL) {
Link *temp = front;
front = front->lnkNxt;
delete(temp);
}
}

Solutions

Expert Solution

Here created function void prntLstBackward(Link* ptr) is for backward traversing in doubly linked list

Modified link.h

#ifndef LINK_H
#define LINK_H

struct Link
{
   int data;
   Link *lnkNxt;
   Link *lnkPrv;

};

#endif /* LINK_H */

Modified main.cpp

//System Level Libraries
#include <iostream.h> //I/O Library
//using namespace std; //Libraries compiled under std
#include"Link.h"
#include<stdlib.h>
//Global Constants - Science/Math Related
//Conversions, Higher Dimensions

//Function Prototypes
void pop_front(Link *&front);
void pop_back(Link *&front);
void push_front(int data, Link *&front);
void push_back(int data, Link *&front);
void prntLst(Link *front);
void dstryLst(Link *&front);
void prntLst(Link* front);
void prntLstBackward(Link* ptr);

Link *LastAddr(Link* front);
Link *fillLnk(int); //Fill a Link
Link *fillLst(int); //Populate the List
//Execution Begins Here!

int main(int argc, char** argv)
{
   //Random Number Seed Set Here


   //Variable Declarations
   Link *lnk1,*ptr;

   //Variable Initialization
   lnk1 = fillLst(5);

   //Mapping Process Inputs to Outputs

   //Display Outputs
   cout << endl << "The Printed List" << endl;
   prntLst(lnk1);
   cout <<endl<<" The list traversing from last to first"<<endl;
   ptr=LastAddr(lnk1);
   prntLstBackward(ptr);


   //Test new functions
   pop_front(lnk1);
   cout << endl << "The Printed List after popfront function" << endl << endl;
   prntLst(lnk1);
   pop_back(lnk1);
   cout << endl << "The Printed List after popback function" << endl << endl;
   prntLst(lnk1);
   push_front(10, lnk1);
   cout << endl << "The Printed List after pushfront function" << endl << endl;
   prntLst(lnk1);
   push_back(20, lnk1);
   cout << endl << "The Printed List after pushback function" << endl << endl;
   prntLst(lnk1);
   //Clean Up, Delete all the elements in the list
   delete (lnk1);

   //Exit stage right!
   return 0;
}

// function to remove the front element

void pop_front(Link *&front)
{
   if (front != NULL)
   { //if not the end of the list
       Link *temp = front; // new pointer set equal to front
       front = front->lnkNxt;
       front->lnkPrv=NULL; //front points to the next node
       delete(temp);
   }
}

// function to remove the back element

void pop_back(Link *&front)
{
   if (front != NULL)
   {
       Link *curr = front;
       Link *prev = NULL;

       while (curr->lnkNxt != NULL)
       {
           prev = curr;
           curr = curr->lnkNxt;
       }

       if (prev == NULL)
           front = NULL;
       else
       {

           prev->lnkNxt = curr->lnkNxt;
           curr->lnkNxt->lnkPrv=prev;
       }

       delete(curr);
   }
   getch();
}

// function to insert data at the front
void push_front(int data, Link *&front)
{
   Link *node = new Link;
   node->data = data;
   node->lnkNxt = front;
   node->lnkPrv=NULL;
   front = node;
}

// function to insert data at the end

void push_back(int data, Link *&front)
{
   Link *node = new Link;
   node->data = data;
   node->lnkNxt = NULL;
   node->lnkPrv=NULL;

   if (front == NULL)
       front = node;
   else
   {
       Link *curr = front;

       while (curr->lnkNxt != NULL)
           curr = curr->lnkNxt;

       curr->lnkNxt = node;
       node->lnkPrv=curr;
   }
}

// function to print the list

void prntLst(Link *front)
{
   Link *curr = front;
   while (curr != NULL)
   {
       cout << curr->data << endl;
       curr = curr->lnkNxt;
   }
}
void prntLstBackward(Link *ptr)
{
   Link *curr = ptr;
   while (curr != NULL)
   {
       cout << curr->data << endl;
       curr = curr->lnkPrv;
   }
}

Link *fillLst(int nLinks)
{
   Link *front = fillLnk(1); //first Link
   Link *next = front; // pointer to move thru the List
   for (int i = 2; i <= nLinks; i++)
   {//attaches one link after another
       Link *newLnk = fillLnk(i);
       next->lnkNxt = newLnk;
       newLnk->lnkPrv=next;
       next = newLnk;
   }
   next->lnkNxt = NULL;
   return front;
}
//fills the list
Link *fillLnk(int data)
{
   Link *lnk = new Link;
   lnk->data = data;
   lnk->lnkNxt=NULL;
   lnk->lnkPrv=NULL;
   return lnk;
}

// function to destroy the list
void dstryLst(Link *&front)
{
   while (front != NULL)
   {
       Link *temp = front;
       front = front->lnkNxt;
       delete(temp);
   }
}
Link * LastAddr(Link *front)
{
   Link *temp;
   temp=front;
   while(temp->lnkNxt!=NULL)
   temp=temp->lnkNxt;
   return temp;
}

Output


Related Solutions

A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
Can you fix please? this is adding the given location of doubly linked list in java...
Can you fix please? this is adding the given location of doubly linked list in java but if I make reverse is not adding the new node that I added but is printing forward correctly. just fix to me that part public void addAtLocation(E newNode, int location) {        Node node = new Node(newNode);               node.data = newNode;               Node previous = head;                      int counter = 1;...
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT