C++ Or C. Looking for help with this assignment. Please and thank you Objective:
ID: 3700872 • Letter: C
Question
C++ Or C. Looking for help with this assignment. Please and thank you
Objective: Create a command line program that adds and removes values from an ordered double linked list.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int count = 0;
struct node
{
int data;
node *next;
node *prev;
}*p = NULL, *nn = NULL, *head = NULL, *tail = NULL,*r = NULL;
void create_node(int x)
{
nn = new node;
nn->data = x;
nn->next = NULL;
nn->prev = NULL;
if (count == 0)
{
tail = nn;
head = nn;
p = head;
p->next = NULL;
p->prev = NULL;
count++;
}
else
{
p = head;
r = p;
if (nn->data < p->data)
{
nn->next = p;
p->prev = nn;
nn->prev = NULL;
head = nn;
p = head;
do
{
p = p->next;
}
while (p->next != NULL);
tail = p;
}
else if (nn->data > p->data)
{
while (p != NULL && nn->data > p->data)
{
r = p;
p = p->next;
if (p == NULL)
{
r->next = nn;
nn->prev = r;
nn->next = NULL;
tail = nn;
break;
}
else if (nn->data < p->data)
{
r->next = nn;
nn->prev = r;
nn->next = p;
p->prev = nn;
if (p->next != NULL)
{
do
{
p = p->next;
}
while (p->next !=NULL);
}
tail = p;
break;
}
}
}
}
}
node* delete_even(node* head, int* count)
{
if (head == NULL)
{
*count = 0;
return head;
}
node* temp = delete_even(head->next, count);
if (head->data % 2 != 0)
{
if (temp != NULL)
{
temp->prev = head;
}
head->next = temp;
return head;
}
else
{
if (temp != NULL)
{
temp->prev = NULL;
}
free(head);
*count += 1;
return temp;
}
}
void trav_tail()
{
node *t = tail;
while (t != NULL)
{
cout<<t->data<<" ";
t = t->prev;
}
cout<<endl;
}
void trav_head()
{
node *t = head;
while (t != NULL)
{
cout<<t->data<<" ";
t = t->next;
}
cout<<endl;
}
int main( int argc, char *argv[])
{
int i=1;
while (i < argc)
{
int data=atoi(argv[i]);
create_node(data);
i+=1;
}
cout<<"***Original Data***";
cout<<" In- Order : ";
trav_head();
cout<<" Reverse Order:";
trav_tail();
cout<<" ***Delete Even Values**";
int counter=0;
head = delete_even(head, &counter);
cout<<" In- Order : ";
trav_head();
cout<<" Reverse Order:";
trav_tail();
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.