Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1 Design and implement a class Menu which uses doubly linked lists as main

ID: 3766373 • Letter: P

Question

Part 1

Design and implement a class Menu which uses doubly linked lists as main data structures. A menu object consists of a set of main menu items, organized as a doubly linked list. Each main menu item has an associated submenu of items which is also organized as a doubly linked list (see an example of a Menu object in the attached file MenuExample.pdf).

The class Menu should define a default constructor which builds an empty menu and the following methods:

- addMainMenuItem, for adding a new main menu item with an empty submenu. The method should have the following signature:

public boolean addMainMenuItem(String newItem, String existingItem, int position)

The method works in the following way:
-if parameter existingItem is null, the newItem will be added at the end of the list;
-if the parameter existingItem is not null, the newItem will be added before the existingItem if parameter position is -1 and after the existinItem if parameter position is 1;

- deleteMainMenuItem, deletes the main menu item specified by name (supplied as method parameter);

- addSubMenuItem, adds a submenu item specified by name to a main menu item specified by name (both names should be supplied as method parameters);

- toString, traverses the linked lists data structures and generates the string representation of a menu object, organized on lines. Each line should include the main menu item followed by its submenu items. The items should be separated by a semicolon character. The string representation should also include as the last line, the number of main menu items and the total number of submenu items. The toString method will be invoked from other programs (such as the driver of Part 2) to display the menu.

Notes
1.1 For implementing the class Menu you may use either an appropriate data structure designed by you from scratch or the LinkedList data structure defined by JCF (Java Collection Framework).
1.2 If necessary, additionally methods may be defined for the class Menu. Give reasons for the new added methods.

The class Menu.java should compile without errors.

Part 2

Write a driver program MenuTest.java to test the Menu class. In the driver program, do the following tasks:

a) Read menu items from the input file Menu.txt and build a menu object which includes the specified items. Each file line contains a main menu item followed by its submenu items, all items being separated by the semicolon character. The main menu and its submenus should be built node by node with the values taken from the input file. After reading the file and building the menu, display the menu using the toString method.

Example of the content of the input file Menu.txt:

File;New;Open;Save;SaveAs;Close;
View;Color;Gray;Zoom;Ruler;Grid;
Process;Select;Move;Rotate;Scale;Delete;Group;
About;Release;Version;Contact;Description;

In the example above, File, View, Process and About are the main menu items. The main menu item File has New, Open, Save, SaveAs and Close as its submenu items.

b) Add new main menu items and their associated submenus before a main menu item specified by the user from the keyboard. Read the new main menu items and their submenus from the input file MenuAdd.txt. Using the toString method, display the menu.

c) Delete a main menu item specified by the user from the keyboard then display the menu using the toString method.

d) Add new main menu items and their associated submenus after a main menu item specified by the user from the keyboard. Read the main menu items and their submenus from the input file MenuAdd.txt. Using the toString method, display the menu.

The input file MenuAdd.txt contains one or more lines specifying the new main menu item(s) and their submenu items.

An example of the content of the input file MenuAdd.txt for adding two new menu items and their submenus is given below:

Layout;Margins;Orientation;Size;
Insert;Table;Picture;Shapes;Chart;


Notes
2.1 The input files Menu.txt and MenuAdd.txt will be created by the students using a simple text editor (such as Notepad).
2.2 Where appropriate, Part 2 tasks a – d should be accomplished by invoking the Menu class methods defined in Part 1.

Submission requirements

Submit the following before the due date listed in the Calendar:

1. Source files Menu.java and MenuTest.java and input files Menu.txt and MenuAdd.txt.

2. The solution description document <YourSecondName>_HW2 (.pdf or .doc / .docx) containing:
(2.1) assumptions, main design decisions, error handling, (2.2) test plan, test cases and two relevant screenshots, (2.3) lessons learned and (2.4) possible improvements. The size of the document file (including the screenshots) should be of two pages, single spaced, font size 12.

Instructions

Part 1

Design and implement a class Menu which uses doubly linked lists as main data structures. A menu object consists of a set of main menu items, organized as a doubly linked list. Each main menu item has an associated submenu of items which is also organized as a doubly linked list (see an example of a Menu object in the attached file MenuExample.pdf).

The class Menu should define a default constructor which builds an empty menu and the following methods:

- addMainMenuItem, for adding a new main menu item with an empty submenu. The method should have the following signature:

public boolean addMainMenuItem(String newItem, String existingItem, int position)

The method works in the following way:
-if parameter existingItem is null, the newItem will be added at the end of the list;
-if the parameter existingItem is not null, the newItem will be added before the existingItem if parameter position is -1 and after the existinItem if parameter position is 1;

- deleteMainMenuItem, deletes the main menu item specified by name (supplied as method parameter);

- addSubMenuItem, adds a submenu item specified by name to a main menu item specified by name (both names should be supplied as method parameters);

- toString, traverses the linked lists data structures and generates the string representation of a menu object, organized on lines. Each line should include the main menu item followed by its submenu items. The items should be separated by a semicolon character. The string representation should also include as the last line, the number of main menu items and the total number of submenu items. The toString method will be invoked from other programs (such as the driver of Part 2) to display the menu.

Notes
1.1 For implementing the class Menu you may use either an appropriate data structure designed by you from scratch or the LinkedList data structure defined by JCF (Java Collection Framework).
1.2 If necessary, additionally methods may be defined for the class Menu. Give reasons for the new added methods.

The class Menu.java should compile without errors.

Part 2

Write a driver program MenuTest.java to test the Menu class. In the driver program, do the following tasks:

a) Read menu items from the input file Menu.txt and build a menu object which includes the specified items. Each file line contains a main menu item followed by its submenu items, all items being separated by the semicolon character. The main menu and its submenus should be built node by node with the values taken from the input file. After reading the file and building the menu, display the menu using the toString method.

Example of the content of the input file Menu.txt:

File;New;Open;Save;SaveAs;Close;
View;Color;Gray;Zoom;Ruler;Grid;
Process;Select;Move;Rotate;Scale;Delete;Group;
About;Release;Version;Contact;Description;

In the example above, File, View, Process and About are the main menu items. The main menu item File has New, Open, Save, SaveAs and Close as its submenu items.

b) Add new main menu items and their associated submenus before a main menu item specified by the user from the keyboard. Read the new main menu items and their submenus from the input file MenuAdd.txt. Using the toString method, display the menu.

c) Delete a main menu item specified by the user from the keyboard then display the menu using the toString method.

d) Add new main menu items and their associated submenus after a main menu item specified by the user from the keyboard. Read the main menu items and their submenus from the input file MenuAdd.txt. Using the toString method, display the menu.

The input file MenuAdd.txt contains one or more lines specifying the new main menu item(s) and their submenu items.

An example of the content of the input file MenuAdd.txt for adding two new menu items and their submenus is given below:

Layout;Margins;Orientation;Size;
Insert;Table;Picture;Shapes;Chart;


Notes
2.1 The input files Menu.txt and MenuAdd.txt will be created by the students using a simple text editor (such as Notepad).
2.2 Where appropriate, Part 2 tasks a – d should be accomplished by invoking the Menu class methods defined in Part 1.

Submission requirements

Submit the following before the due date listed in the Calendar:

1. Source files Menu.java and MenuTest.java and input files Menu.txt and MenuAdd.txt.

2. The solution description document <YourSecondName>_HW2 (.pdf or .doc / .docx) containing:
(2.1) assumptions, main design decisions, error handling, (2.2) test plan, test cases and two relevant screenshots, (2.3) lessons learned and (2.4) possible improvements. The size of the document file (including the screenshots) should be of two pages, single spaced, font size 12.

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#define NULL 0

struct linkedlist
{
int item;
struct linkedlist *right,*left;
};

typedef struct linkedlist node;

void main()
{
node *start,*end;
int choice;
int menu(void);
node *create(node **lastnode);
void display(node *first,node *last);
void insert(node **first,node **last);
void del(node **first,node **last);
clrscr();
printf(“ DOUBLY LINKED LIST”);
printf(“ ******************”);
do
{
printf(“ Main menu”);
printf(“ 1.Create 2.Insert 3.Delete 4.Display 5.Exit”);
choice =menu();

switch(choice)
{

case 1:
printf(“ Enter the data(-999 to stop):”);
start=create(&end);
continue;

case 2:
insert(&start,&end);
printf(“ ”);
continue;

case 3:
del(&start,&end);
printf(“ ”);
continue;

case 4:
display(start,end);
printf(“ ”);
continue;

case 5:
exit(0);
default:
printf(“ INVALID CHOICE…”);
}
}while(1);
}

int menu()
{
int choice;
do
{
printf(“ Enter your choice:”);
scanf(“%d”,&choice);
if(choice<1||choice>5)
printf(“ Wrong choice”);
}while(choice<1||choice>5);
printf(“ ”);
return(choice);
}

node *create(node **lastnode)
{
node *temp,*firstnode;
int info;
*lastnode=NULL;
firstnode=NULL;
scanf(“%d”,&info);
while(info!=-999)
{
temp=(node *)malloc(sizeof(node));
temp->item=info;
temp->right=NULL;
if(firstnode==NULL)
{
temp->left=NULL;
firstnode=temp;
}
else
{
temp->left=(*lastnode);
(*lastnode)->right=temp;
}
(*lastnode)=temp;
scanf(“%d”,&info);
}
if(firstnode!=NULL)
(*lastnode)=temp;
return(firstnode);
}

void display(node *first,node *last)
{
printf(“ Forward traversal ”);
while(first!=NULL)
{
printf(“%d ”,first->item);
first=first->right;
}
printf(“ Backward traversal ”);
while(last!=NULL)
{
printf(“%d ”,last->item);
last=last->left;
}
return;
}

void insert(node **first,node **last)
{
node *newnode;
int newitem;
int position;
node *temp;
int i;
printf(“ New data item:”);
scanf(“%d”,&newitem);
do
{
printf(“ Position of insertion:”);
scanf(“%d”,&position);
}while(position<=0);
if(((*first)==NULL)||(position==1))
{
newnode=(node *)malloc(sizeof(node));
newnode->item=newitem;
newnode->right=*first;
newnode->left=NULL;
if((*first)!=NULL)
(*first)->left=newnode;
else
(*last)=newnode;
*first=newnode;
}
else
{
i=1;
temp=*first;
while((i<position-1)&&(temp->right!=NULL))
{
i++;
temp=temp->right;
}
newnode=(node *)malloc(sizeof(node));
newnode->item=newitem;
newnode->right=temp->right;
if(temp->right!=NULL)
temp->right->left=newnode;
newnode->left=temp;
temp->right=newnode;
}
if(newnode->right==NULL)
*last=newnode;
}

void del(node **first,node **last)
{
node *temp,*prev;
int target;
printf(“ Enter the data to be deleted:”);
scanf(“%d”,&target);
if(*first==NULL)
printf(“ List is empty”);
else if((*first)->item==target)
{
if((*first)->right==NULL)
*first=*last=NULL;
else
{
*first=(*first)->right;
(*first)->left=NULL;
}
}
else
{
temp=*first;
prev=NULL;
while((temp->right!=NULL)&&(temp->item!=target))
{
prev=temp;
temp=temp->right;
}
if(temp->item!=target)
printf(“ Element not found”);
else
{
if(temp==*last)
*last=prev;
else
temp->right->left=temp->left;
prev->right=temp->right;
}
}
}

OUTPUT:
DOUBLY LINKED LIST
******************

Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:1
Enter the data(-999 to stop):89
45
78
12
-999
Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:4
Forward traversal
89 45 78 12
Backward traversal
12 78 45 89
Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:2
New data item:45

Position of insertion:2

Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:4
Forward traversal
89 45 45 78 12
Backward traversal
12 78 45 45 89
Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:3

Enter data to be deleted : 78

Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:4
Forward traversal
89 45 45 12
Backward traversal
12 45 45 89
Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:5