C++ Data Structure (You can download this source file as http://staffwww.fullcol
ID: 3797804 • Letter: C
Question
C++ Data Structure
(You can download this source file as http://staffwww.fullcoll.edu/aclifton/courses/cs133_fa16/files/dlist.h.)
You should save the above into dlist.h, and put the implementations of the functions (and any methods that you don’t want to implement in the class body) into dlist.cc. (If you want to, you can implement everything in dlist.h by writing the functions as inline, thus creating a “header-only” list library.) You should also create a file list_tests.cc containing a main which tests your list implementation in various ways, to make sure you’ve written everything correctly.
In this assignment, you will implement a doubly-linked list class. together with some list operations. To make things easier, you'll implement a list of int rather than a template class pragma once dlist. h Doubly linked lists of ints include Kostream class dlist public: d list struct node int value node next node prev; node' head() const return -head; node" tail() const t return -tail Implement ALL the following methods Returns the node at a particular index (0 is the head node at(int) Insert a new value, after an existing one void insert(node *previous int value Delete the given node void del (node which)Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int c = 0;
struct node
{
node *next, *prev;
int data;
}*head = NULL, *tail = NULL, *p = NULL, *r = NULL, *np = NULL;
void create(int x)
{
np = new node;
np->data = x;
np->next = NULL;
np->prev = NULL;
if (c == 0)
{
tail = np;
head = np;
p = head;
p->next = NULL;
p->prev = NULL;
c++;
}
else
{
p = head;
r = p;
if (np->data < p->data)
{
np->next = p;
p->prev = np;
np->prev = NULL;
head = np;
p = head;
do
{
p = p->next;
}
while (p->next != NULL);
tail = p;
}
else if (np->data > p->data)
{
while (p != NULL && np->data > p->data)
{
r = p;
p = p->next;
if (p == NULL)
{
r->next = np;
np->prev = r;
np->next = NULL;
tail = np;
break;
}
else if (np->data < p->data)
{
r->next = np;
np->prev = r;
np->next = p;
p->prev = np;
if (p->next != NULL)
{
do
{
p = p->next;
}
while (p->next !=NULL);
}
tail = p;
break;
}
}
}
}
}
void traverse_tail()
{
node *t = tail;
while (t != NULL)
{
cout<<t->data<<" ";
t = t->prev;
}
cout<<endl;
}
void traverse_head()
{
node *t = head;
while (t != NULL)
{
cout<<t->data<<" ";
t = t->next;
}
cout<<endl;
}
int main()
{
int i = 0, n, x, ch;
cout<<"enter the no of nodes ";
cin>>n;
while (i < n)
{
cout<<" enter value of node ";
cin>>x;
create(x);
i++;
}
cout<<" Traversing Doubly Linked List head first ";
traverse_head();
cout<<" Traversing Doubly Linked List tail first ";
traverse_tail();
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.