HW2.docx

Exercise 1

Write a program in C Language that implement 2 functions:

a) convertDoublyToCircular that takes in a doubly linked list and converts it into a circular list

b) convertCircularToStack that takes in a circular linked list and returns a stack with all the

nodes of the linked list on the stack.

//////////////////////////////////////////////////////

Exercise 2

Using only the given implementation of SinglyLinkedList class and Stack class add method into

SinglyLinkedList that received as a parameter another SinglyLinkedList and checks if the

received list is a subset of the current list and return either true or false based on the result. You must use the

given Stack implementation to perform this check.

Implementations :

            1) Doubly LinkedList
        
            2) Singly LinkedList
        
            3) Stack
        

Will be given in another attachment.

//////////////////////////////////////////////////

Exercise 3

(Solve in C LANGUAGE) instead of c++

Using only std::queue do the following:

a. Write a recursive C++ method recursiveDisplayReverseOrder that takes as parameters a

Queue of strings, where each element is a word and display the words in reverse order.

b. Write a C++ method iterativeDisplayReverseOrder which behaves just like the method in

part a but doesn’t use recursion.

Note that std::queue has a slightly different way to access data. Function pop() will only remove the

first element from the queue without returning anything. To get the data you have to use function front()

then do pop() to remove it. For more details: https://en.cppreference.com/w/cpp/container/queue

//////////////////////////////////////////////////////////

Exercise 4

Write a C program that implements a dequeue (double ended queue) abstract data type using 1) an unsorted

doubly linked list, and 2) using a sorted doubly linked list. You must use the DoublyLinkedList provided

to you with this assignment.

.

Example of how my C program syntaxes are like:

(please try to follow them as much as possible)

1)Linked Lists:

#include<stdio.h>

#include<stdlib.h>

typedef struct Node{

int data;

struct Node *next;

}node;

node *head=NULL;

////// LINKED LISTS //////

void insertAtFirst(node **head,int x)

{

node *p;

p=(node*)malloc(sizeof(node));

p->data=x;

p->next=NULL;

if(*head==NUll)

*head=p;

else

{

p->next=(*head);

*head=p;

}

}//

void insertAtEnd(node **head,int x)

{

node *p,*q;

p=(node*)malloc(sizeof(node));

p->data=x;

p->next=NULL;

if(*head==NULL)

*head=p;

else

{

q=*head;

while(q->next!=NULL)

q=q->next;

q->next=p;

p->next=NULL;

}

}//

void deleteFrist(node **head)

{

node *p;

if(*head==NULL)

{

printf("Linked List is Empty");

return;

}

else

{

p=*head;

*head=(*head)->next;

free(p);

}

}//

void deleteLast(node **head)

{

node *p,*q;

if(*head==NULL)

{

printf("Linked List is Empty");

return;

}

else

{

q=*head;

p=(*head)->next;

while(p->next!=NULL)

{

p=p->next;

q=q->next;

}

q->next=NULL;

free(p);

}

}

2)Stacks:

#include<stdio.h>

#include<stdlib.h>

typedef struct Node{

int data;

struct Node *next;

}stack;

stack *Top=NULL;

////// Stacks //////

void push(stack **Top,int x)

{

stack *p;

p=(stack*)malloc(sizeof(stack));

p->data=x;

p->next=NULL;

if(*Top==NULL)

*Top=p;

else

{

p->next=*Top;

*Top=p;

}

}//

int pop(stack **Top)

{

stack *p;

int x=-1;

if(*Top==NULL)

{

printf("Stack is Empty\n");

returnx;

}

else

{

p=*Top;

x=(*Top)->data;

*Top=(*Top)->next;

return x;

}

}//

void printStack(stack *Top)

{

stack *p;

for(p=Top;p!=NULL;p=p->next)

printf("%d",p->data);

}//

3)Queues:

#include<stdio.h>

#include<stdlib.h>

typedef struct Node{

int data;

struct Node *next;

}Queue;

Queue *front=NULL;

Queue *rear=NULL;

////// Queues //////

void enqueue(Queue **front,Queue **rear,int x)

{

Queue *p;

p=(Queue*)malloc(sizeof(Queue));

p->data=x;

p->next=NULL;

if(*front==NULL)

*front=*rear=p;

else

{

(*rear)->next=p;

*rear=p;

}

}//

int dequeue(Queue **front,Queue **rear)

{

int x=-1;

Queue *p;

if(*front==NULL)

{

printf("Queue is Empty.\n");

return x;

}

else

{

x=(*front)->data;

p=(*front);

if( (*front)->next==NULL )

{

*front=*rear=NULL;

free(p);

return x;

}

else

{

*front=(*front)->next;

free(p);

}

}

}/