Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this programming homework, we will be implementing sequence data structure as * doubly * linked list. One of the central type of data at
In this programming homework, we will be implementing sequence data structure as doubly linked list.
One of the central type of data at the base of many indexes and algorithms is called set data. Typically, the
set is of keys, which are positive integers. It is a subset S of the universal set U u When we
use sequence to represent a set, it may become an ordered collection of keys. There are different ways this
order can be seen it may be based on the values of keys, or it may just be about the positions of keys in
the data structure.
In this programming assignment, we shall be using doubly linked lists as against singly linked list we saw
in the class to implement sequence which can serve both a stack and a queue. We choose doubly linked
list here so that no traversal of the list is necessary, and all the operations can be done instantly with the
exception of printall Thus, Node structure here will have prev pointer in addition to next In a singly
linked list, next pointer of the tail element is null. In doubly linked list, in addition to this, prev pointer
of the head element is also set to null. Base cases to consider are: what if the list is empty before the
insertion? What if the list becomes empty after deletion? Please use the following helper code. Fill up
commented part by an actual code. Your class name must be DoublyLinkedList and filename either
DoublyLinkedList.java or DoublyLinkedList.cpp Submit this on gradescope.
You can use and build on the following partialpseudo code:
class Node
int key;
Node prev, next;
Nodeint key
this.key key;
this.prev null;
this.next null;
class DoublyLinkedList
Node head;
Node tail;
int count;
DoublyLinkedList
head tail null;
count ;
void insertFint key
Node p new Nodekey;
ifhead null
headtailp; it is a single element list now
else
we will do some pointer stitching carefully
prepare p to become new head so that ps next will point to current
head
set current heads prev to p
set new head to p
increment count
void insertLint key
Node p new Nodekey;
if tail null
headtailp;
else
set tails next to p
set ps prev to tail
set new tail as p
increment count
int deleteF
if count return ; error
Node p head;
int i pkey;
prepare to remove head by setting head to head.next
ifhead null tail null; if list becomes empty after deletion
else
new head lets go of the old head by setting new heads prev to null
decrement count
deallocate p optional
return i;
int deleteL
if count return ; error
Node p tail;
int i pkey;
tail tail.prev;
if tail null head null;
else tail.next null;
count;
deallocate p
return i;
void printall
Node p head;
while pnull
System.out.printpkey;
if pnext null System.out.print;
ppnext;
Systemout.println; do not use this for submission
public static void mainString args
DoublyLinkedList l new DoublyLinkedList;
linsertF;
linsertF;
linsertL;
linsertL;
int temp ldeleteF;
lprintall; output will be
Required Output:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started