How can I convert this single linked list into a double linked list that has the
ID: 671289 • Letter: H
Question
How can I convert this single linked list into a double linked list that has the function; delete, reverse print, and insert
any help is appreciated thanks in advance
using namespace std;
#include<iostream>
#include<sstream>
#include<string>
#include<cstdlib>
class Stock
{
public:
int info;
Stock *link;
Stock *back;
string stockSymbol;
double cost;
int shares;
};
Stock* generateStocks();
void printStocks(Stock *head);
Stock* MiddleList(Stock *head);
Stock* SplitStocks(Stock *head);
//generate the linked list Stocks
Stock* generateStocks()
{
Stock *front, *end, *newNode;
front = NULL;
end = NULL;
for(int i = 1; i < 21; i++)
{
stringstream s;
s << i;
newNode = new Stock;
newNode ->cost = rand() % 100;
newNode ->shares = rand() % 100;
newNode ->stockSymbol = s.str();
newNode ->link = NULL;
if(front == NULL)
{
front = newNode;
end = newNode;
}
else
{
end ->link = newNode;
end = newNode;
}
}
newNode = front;
return newNode;
}
//print the Stocks
void printStocks(Stock *head)
{
Stock *newNode = head;
while(newNode != NULL)
{
cout << "Stock Symbol:A" << newNode ->stockSymbol << " Cost:$" << newNode ->cost
<< " Shares:" << newNode ->shares << endl;
newNode = newNode ->link;
}
cout << endl;
}
//find and print the element found in the middle of the stocks
Stock* MiddleList(Stock *head)
{
Stock *slow = head, *fast = head;
int pos = 1;
int count = 0;
while(fast ->link != NULL)
{
count++;
if(slow == NULL)
{
exit(-1);
}
fast = fast->link;
if(count % 2 == 0)
{
slow = slow->link;
pos++;
}
}
cout << "The middle element is: A" << slow ->stockSymbol << " Cost:$" << slow ->cost
<< " Shares:" << slow ->shares << endl;
cout << "The position of the stock is: " << pos << endl << endl;
return slow;
}
//split the stocks in half
Stock* SplitStocks(Stock *head)
{
Stock *slow = head, *fast = head;
int pos = 1;
int count = 0;
while(fast ->link != NULL)
{
count++;
if(slow == NULL)
{
exit(-1);
}
fast = fast->link;
if(count % 2 == 0)
{
slow = slow->link;
pos++;
}
}
Stock *head2 = slow ->link;
slow ->link = NULL;
return head2;
}
int main()
{
cout << "Here are the 20 stocks! " << endl;
Stock *head = generateStocks();
printStocks(head);
Stock *a = MiddleList(head);
Stock *b = SplitStocks(head);
cout << "Stocks found in the first half of the list " << endl;
printStocks(head);
cout << "Stocks found in the second half of the list " << endl;
printStocks(b);
}
Explanation / Answer
In order to make a single linked list into a doubly one,you will need a pointer to point back to the previous node. So you will have a node that points to the next pointer and it will also point to the previous one.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct listNode
{
char * name;
struct listNode * nextPtr;
};
void print_function ( struct listNode *startPtr);
void insert_function(struct listNode **startPtr, char *data);
void delete_function(struct listNode **startPtr, char *value);
main()
{
char name[128];
struct listNode * startPtr = NULL;printf("enter the names you want to insert into a linked list ");
print_function(startPtr);
printf("enter name 1 ");
scanf("%s",name);
insert_function(&startPtr,name);
print_function(startPtr);
printf(" ");
printf("enter name 2 ");
scanf("%s", name);
insert_function(&startPtr,name);
print_function(startPtr);
printf(" ");
printf("enter name 3 ");
scanf("%s", name);
insert_function(&startPtr,name);
print_function(startPtr);
printf(" ");
printf("enter a name to delete ");
scanf("%s",name);
delete_function(&startPtr,name);
print_function(startPtr);
printf(" ");
}
void insert_function(struct listNode **startPtr, char *value)
{
struct listNode *prevPtr = NULL;
struct listNode *currentPtr = NULL;
struct listNode *newPtr = NULL;
int string = strlen(value);
currentPtr = *startPtr;
newPtr = (struct listNode *)malloc(sizeof(struct listNode));
if (newPtr == NULL)
{
printf("can't allocate memory ");
return;
}
newPtr->name = (char *)malloc(string *sizeof(char));
strncpy(newPtr->name,value,string);
newPtr->nextPtr = NULL;
if (*startPtr == NULL)
{
*startPtr = newPtr;
return;
}
while (currentPtr != NULL && strncmp(newPtr->name,currentPtr->name,string) >0)
{
prevPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if (prevPtr == NULL)
{
newPtr->nextPtr = currentPtr;
*startPtr = newPtr;
return;
}
if (currentPtr == NULL)
{
prevPtr->nextPtr = newPtr;
return;
}
prevPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
void delete_function(struct listNode **startPtr, char *value)
{
struct listNode *currentPtr =NULL;
struct listNode *prev =NULL;
currentPtr = *startPtr;
if(*startPtr == NULL)
{
printf("the list is empty, can't delete ");
return;
}
if((*startPtr)->name == value)
{
printf("list beginning ");
*startPtr= currentPtr->nextPtr;
free(currentPtr);
return;
}
while(currentPtr!=NULL && (strcmp(value,currentPtr->name) != 0))
{
prev = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if(currentPtr == NULL)
{
printf("name not in list ");
return;
}
prev->nextPtr = currentPtr->nextPtr;
}
void print_function ( struct listNode *startPtr)
{
struct listNode *currentPtr = startPtr;
while(currentPtr!=NULL)
{
printf("|%2s| -->",currentPtr->name);
currentPtr = currentPtr->nextPtr;
}
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.