Suppose the names \"Bob\", \"Dan\", and \"Ben\", are stored as shown below: ----
ID: 3550296 • Letter: S
Question
Suppose the names "Bob", "Dan", and "Ben", are stored as shown below: ----- ----- head -->| B |---------------->| D | ----- ----- | | | ----- ----- | ----- -->| Ben |-->| Bob | -->| Dan | ----- ----- ----- If the names "Sue" and "Sarah" are added, it looks like this: ----- ----- ----- head -->| B |---------------->| D |-------->| S | ----- ----- ----- | | | | ----- ----- | ----- | ------- ----- -->| Ben |-->| Bob | -->| Dan | -->| Sarah |-->| Sue | ----- ----- ----- ------- ----- Details: Create a class called Index (not generic) that stores strings and implements the structure shown above. The top-level list is kept in sorted order, as is each sublist. You may not use the LinkedList class. You should create your own list nodes and link them together as shown in the illustration. Your class should support these methods: add - adds a new string. remove - removes a string. find - finds a string toString - prints the list like this (using the first list above as input): B Ben Bob D Dan main - demonstrates the methods of your Index class
Explanation / Answer
//C++ Code
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct charlist
{
char a;
struct charlist *next;
};
struct stringlist
{
string s;
struct stringlist *next;
};
struct llist
{
struct charlist *cl;
struct stringlist *sl;
};
struct llist *l = NULL;
void get_initialize()
{
l->cl = NULL;
l -> sl = NULL;
}
int presentchrlist(char s)
{
while(l->cl != NULL && l ->cl ->a != s)
{
cl = cl->next;
if(cl->a == s)
return 1
}
return 0;
}
int presentstrlist(char s)
{
while(l->sl != NULL && l ->sl ->s != s)
{
sl = sl->next;
if(sl->s == s)
return 1
}
return 0;
}
void putinslist(string s)
{
struct stringlist *tempsl = (struct stringlist *)malloc(sizeof(struct stringlist));
tempsl->s = s;
tempsl->next = NULL;
while(l->sl->s.compare( s))
{
sl = sl->next;
}
if(sl != l->sl){
tempsl->next = sl->next;
sl->next = tempsl;
}
else
{
tempsl->next = sl;
sl = tempsl;
}
}
void putinclist(char s)
{
struct charlist *tempcl = (struct charlist *)malloc(sizeof(struct charlist));
tempcl->a = s;
tempcl->next = NULL;
while(l->cl->a > s)
{
cl = cl->next;
}
if(cl != l->cl){
tempcl->next = cl->next;
cl->next = tempcl;
}
else
{
tempcl->next = cl;
cl = tempcl;
}
}
void insert(string s)
{
l = (struct llist *)malloc(sizeof(struct llist));
if(!presentchrlist(s[0]))
{
putinclist(s[0]);
}
if(!presentstrlist(s))
{
putinstrlist(s);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.