Write a generic class called Linked List. This class should have a method called
ID: 3802842 • Letter: W
Question
Write a generic class called Linked List. This class should have a method called add and a method called remove. Also, it should have two methods called display and recursive Display. All these methods are public methods. Both methods display and recursive display must display the values in the linked list. the method recursive Display must recursively display the values in the link list. in main create three different Linked List objects. One of them should hold integer values. the other one should hold string values and the last one should hold Car objects. Add different values to each object and test both display and recursive Display methods.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
struct Item
{
struct Item *prev;
int n;
struct Item *next;
}*h,*tem,*tem1,*tem2,*tem4;
void add1();
void add2();
void add3();
void trackbeg();
void trackend(int);
void sort();
void search();
void update();
void delete();
int count = 0;
void main()
{
int ch;
h = NULL;
tem = tem1 = NULL;
printf(" 1 - Add at beginning");
printf(" 2 - Add at end");
printf(" 3 - Add at space i");
printf(" 4 - Delete at i");
printf(" 5 - Display from beginning");
printf(" 6 - Display from end");
printf(" 7 - Search for term");
printf(" 8 - Sort the list");
printf(" 9 - Update an term");
printf(" 10 - Exit");
while (1)
{
printf(" Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
add1();
break;
case 2:
add2();
break;
case 3:
add3();
break;
case 4:
delete();
break;
case 5:
trackbeg();
break;
case 6:
tem2 = h;
if (tem2 == NULL)
printf(" Error : List blank to display ");
else
{
printf(" Reverse order of linked list is : ");
trackend(tem2->n);
}
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
exit(0);
default:
printf(" Wrong choice menu");
}
}
}
/* TO create an blank Item */
void create()
{
int data;
tem =(struct Item *)malloc(1*sizeof(struct Item));
tem->prev = NULL;
tem->next = NULL;
printf(" Enter value to Item : ");
scanf("%d", &data);
tem->n = data;
count++;
}
/* TO add at beginning */
void add1()
{
if (h == NULL)
{
create();
h = tem;
tem1 = h;
}
else
{
create();
tem->next = h;
h->prev = tem;
h = tem;
}
}
/* To add at end */
void add2()
{
if (h == NULL)
{
create();
h = tem;
tem1 = h;
}
else
{
create();
tem1->next = tem;
tem->prev = tem1;
tem1 = tem;
}
}
/* To add at any space */
void add3()
{
int pos, i = 2;
printf(" Enter space to be added : ");
scanf("%d", &pos);
tem2 = h;
if ((pos < 1) || (pos >= count + 1))
{
printf(" Space out of range to add");
return;
}
if ((h == NULL) && (pos != 1))
{
printf(" Blank list cannot add other than 1st space");
return;
}
if ((h == NULL) && (pos == 1))
{
create();
h = tem;
tem1 = h;
return;
}
else
{
while (i < pos)
{
tem2 = tem2->next;
i++;
}
create();
tem->prev = tem2;
tem->next = tem2->next;
tem2->next->prev = tem;
tem2->next = tem;
}
}
/* To delete an term */
void delete()
{
int i = 1, pos;
printf(" Enter space to be deleted : ");
scanf("%d", &pos);
tem2 = h;
if ((pos < 1) || (pos >= count + 1))
{
printf(" Error : Space out of range to delete");
return;
}
if (h == NULL)
{
printf(" Error : Blank list no terms to delete");
return;
}
else
{
while (i < pos)
{
tem2 = tem2->next;
i++;
}
if (i == 1)
{
if (tem2->next == NULL)
{
printf("Item deleted from list");
free(tem2);
tem2 = h = NULL;
return;
}
}
if (tem2->next == NULL)
{
tem2->prev->next = NULL;
free(tem2);
printf("Item deleted from list");
return;
}
tem2->next->prev = tem2->prev;
if (i != 1)
tem2->prev->next = tem2->next; /* Won't not require this statement if i == 1 check */
if (i == 1)
h = tem2->next;
printf(" Item deleted");
free(tem2);
}
count--;
}
/* To navigate from beginning */
void trackbeg()
{
tem2 = h;
if (tem2 == NULL)
{
printf("List blank to display ");
return;
}
printf(" Linked list terms from begining : ");
while (tem2->next != NULL)
{
printf(" %d ", tem2->n);
tem2 = tem2->next;
}
printf(" %d ", tem2->n);
}
/* To navigate from end recursively */
void trackend(int i)
{
if (tem2 != NULL)
{
i = tem2->n;
tem2 = tem2->next;
trackend(i);
printf(" %d ", i);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.