using namespace std;
class Cart{
public:
int fruitPrice;
Cart* next;
Cart(int price){
fruitPrice=price;
next=NULL;
}
};
class SinglyLinkedList{
public:
Cart* head;
SinglyLinkedList(){
head=NULL;
}
void add_item(int price){
Cart* itemToAdd= new Cart(price);
if (head==NULL){
head=itemToAdd;
itemToAdd->next=NULL;
}
else{
Cart* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=itemToAdd;
itemToAdd->next=NULL;
}
}
void returnList(){
if (head==NULL){
cout<<"List is empty";}
else{
Cart* temp=head;
while (temp->next!=NULL){
cout<<temp->fruitPrice<<endl;
temp=temp->next;
}
cout<<temp->fruitPrice;
}
}
void DivideEvenAndOdd(){
if(head==NULL){
cout<<"List is empty";
}
else{
SinglyLinkedList newlist;
Cart* temp;
Cart* temp2=NULL;
while(this->head!=NULL && this->head->fruitPrice%2==0 ){
if (newlist.head==NULL){
newlist.head=this->head;
temp2=this->head;
this->head=this->head->next;
temp2->next=NULL;
}
else{
temp2->next=this->head;
this->head=this->head->next;
temp2=temp2->next;
temp2->next=NULL;
}
}
temp=head;
while (temp!=NULL && temp->next!=NULL){
if(temp->next->fruitPrice%2==0){
if(newlist.head==NULL){
newlist.head=temp2=temp->next;
temp2->next=NULL;
temp->next=temp->next->next;
}
else{
temp2->next=temp->next;
temp2=temp2->next;
temp2->next=NULL;
temp->next=temp->next->next;
}
}
else{
temp=temp->next;
}
}
this->returnList();
cout<<endl<<endl;
newlist.returnList();
}
}
};
int main(){
SinglyLinkedList sll;
sll.add_item(10);
sll.add_item(15);
sll.add_item(35);
sll.add_item(16);
sll.DivideEvenAndOdd();
}```
#need help with my DSA assignment code
25 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
I made this code for my assignment question, but there's some logical error in this code and I know where it lies
but I am unable to fix it despite trying multiple solutions
The logical error lies in this part of code
the error lies in this part:
newlist.head=this->head;
temp2=this->head;
this->head=this->head->next;
temp2->next=NULL;
}```
!code
How to Format Code on Discord
Markup
```cpp
int main() {}
```
Result
int main() {}
Note: Back-tick (`) not quotes (')
How to Format Code on Discord
Markup
```cpp
int main() {}
```
Result
int main() {}
Note: Back-tick (`) not quotes (')
for some reason, my this->head starts pointing towards NULL after first iteration of the first while loop inside DivideEvenandOdd() function
This is the question I am trying to solve
#include <iostream>
using namespace std;
class Cart{
public:
int fruitPrice;
Cart* next;
Cart(int price){
fruitPrice=price;
next=NULL;
}
};
class SinglyLinkedList{
public:
Cart* head;
SinglyLinkedList(){
head=NULL;
}
void add_item(int price){
Cart* itemToAdd= new Cart(price);
if (head==NULL){
head=itemToAdd;
itemToAdd->next=NULL;
}
else{
Cart* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=itemToAdd;
itemToAdd->next=NULL;
}
}
void returnList(){
if (head==NULL){
cout<<"List is empty";}
else{
Cart* temp=head;
while (temp->next!=NULL){
cout<<temp->fruitPrice<<endl;
temp=temp->next;
}
cout<<temp->fruitPrice;
}
}
void DivideEvenAndOdd(){
if(head==NULL){
cout<<"List is empty";
}
else{
SinglyLinkedList newlist;
Cart* temp;
Cart* temp2=NULL;
while(this->head!=NULL && this->head->fruitPrice%2==0 ){
if (newlist.head==NULL){
newlist.head=this->head;
temp2=this->head;
this->head=this->head->next;
temp2->next=NULL;
}
else{
temp2->next=this->head;
this->head=this->head->next;
temp2=temp2->next;
temp2->next=NULL;
}
}
temp=head;
while (temp!=NULL && temp->next!=NULL){
if(temp->next->fruitPrice%2==0){
if(newlist.head==NULL){
newlist.head=temp2=temp->next;
temp2->next=NULL;
temp->next=temp->next->next;
}
else{
temp2->next=temp->next;
temp2=temp2->next;
temp2->next=NULL;
temp->next=temp->next->next;
}
}
else{
temp=temp->next;
}
}
this->returnList();
cout<<endl<<endl;
newlist.returnList();
}
}
};
int main(){
SinglyLinkedList sll;
sll.add_item(10);
sll.add_item(15);
sll.add_item(35);
sll.add_item(16);
sll.DivideEvenAndOdd();
}```
here @cedar tartan
have you stepped through your code in a debugger yet?
yep
it just runs for one iteration and then gets out of loop
Look at what's happening during the execution though, where do things go wrong with the pointers
ah ok, I think i found the logical error
it's not with the head pointer mis assignment, right?
it's the condition this->head->fruitPrice%2==0
@cedar tartanI fixed the code. Now there's one more error left that I need help with
#include <iostream>
using namespace std;
class Cart{
public:
int fruitPrice;
Cart* next;
Cart(int price){
fruitPrice=price;
next=NULL;
}
};
class SinglyLinkedList{
public:
Cart* head;
SinglyLinkedList(){
head=NULL;
}
void add_item(int price){
Cart* itemToAdd= new Cart(price);
if (head==NULL){
head=itemToAdd;
itemToAdd->next=NULL;
}
else{
Cart* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=itemToAdd;
itemToAdd->next=NULL;
}
}
void returnList(){
if (head==NULL){
cout<<"List is empty";}
else{
Cart* temp=head;
while (temp->next!=NULL){
cout<<temp->fruitPrice<<"=>";
temp=temp->next;
}
cout<<temp->fruitPrice;
}
}
void DivideEvenAndOdd(){
if(head==NULL){
cout<<"List is empty";
}
else{
SinglyLinkedList newlist;
Cart* temp;
Cart* temp2=NULL;
while(this->head!=NULL && this->head->fruitPrice%2==0 ){
if (newlist.head==NULL){
newlist.head=this->head;
temp2=this->head;
this->head=this->head->next;
temp2->next=NULL;
}
else{
temp2->next=this->head;
this->head=this->head->next;
temp2=temp2->next;
temp2->next=NULL;
}
}
temp=head;
while (temp!=NULL && temp->next!=NULL){
if(temp->next->fruitPrice%2==0){
if(newlist.head==NULL){
newlist.head=temp2=temp->next;
temp2->next=NULL;
temp->next=temp->next->next;
}
else{
temp2->next=temp->next;
temp2=temp2->next;
temp2->next=NULL;
temp=temp->next->next;
}
}
else{
temp=temp->next;
}
}
this->returnList();
cout<<endl<<endl;
newlist.returnList();
}
}
};
int main(){
SinglyLinkedList sll;
sll.add_item(10);
sll.add_item(15);
sll.add_item(35);
sll.add_item(16);
sll.add_item(24);
sll.add_item(367);
sll.DivideEvenAndOdd();
}```
this is the fixed code
10=>16```
This is the output I'm getting
Error lies somewhere in this part of code:
temp=head;
while (temp!=NULL && temp->next!=NULL){
if(temp->next->fruitPrice%2==0){
if(newlist.head==NULL){
newlist.head=temp2=temp->next;
temp2->next=NULL;
temp->next=temp->next->next;
}
else{
temp2->next=temp->next;
temp2=temp2->next;
temp2->next=NULL;
temp=temp->next->next;
}
}
else{
temp=temp->next;
}
}
this->returnList();
cout<<endl<<endl;
newlist.returnList();
}
}```