I'm currently learning how to code C++ and I need help. I need to go through the list and remove all of the even integers. Then I need to output the list after inputting all of the integers and removing all of the even integers. I also need to test it with these integers : 7, 10, 5, 7, 9, 4, 2, 6, 10, 12, 3, 12, 5, 5, 2, 7, 2, 12, -1.
Can anyone help? I zuck ya dick.
Here's my code so far:
#include using namespace std;
struct Node { int data; Node* next; }; void sortedInsert(Node** head_ref, int num) { Node* new_node = new Node; new_node->data = num; new_node->next = NULL; /* Special case for the head end */ if (*head_ref == NULL || (*head_ref)->data >= new_node->data) { new_node->next = *head_ref; *head_ref = new_node; } else { Node* current = *head_ref; while (current->next!=NULL && current->next->data < new_node->data) { current = current->next; } new_node->next = current->next; current->next = new_node; } } /* Function to print linked list */ void printList( Node *head) { Node *temp = head; while(temp != NULL) { cout
This is probably homework for a data structures class. Also op learn to use code tags that shit is unreadable
Robert Thomas
It's not homework, It's me trying to learn a computing language and not get left behind when machines take over the planet.
Code Tags Included.
#include using namespace std; /* Program to insert in a sorted list */ /* Link list node */ struct Node { int data; Node* next; }; void sortedInsert(Node** head_ref, int num) { Node* new_node = new Node; new_node->data = num; new_node->next = NULL; /* Special case for the head end */ if (*head_ref == NULL || (*head_ref)->data >= new_node->data) { new_node->next = *head_ref; *head_ref = new_node; } else { /* Locate the node before the point of insertion */ Node* current = *head_ref; while (current->next!=NULL && current->next->data < new_node->data) { current = current->next; } new_node->next = current->next; current->next = new_node; } } /* Function to print linked list */ void printList( Node *head) { Node *temp = head; while(temp != NULL) { cout
No, write your own dynamic container. How is he going to learn c++ without stepping on some rakes himself?
Isaiah Johnson
Then do it using arrays. Linked lists have no use in the real world.
William Lopez
This, literally. Also use nullptr instead of NULL.
Jackson Martinez
Why aren’t you just validating whether the number is even or odd before adding it to the list? Why wait until all the numbers were added? Anyway I can think of two options to do what you want one is transverse the list and copy the odd numbers to a new list and return that one. Another is transverse the list and remove the even nodes. You will need to code a delete node function
Mason Rogers
Just get an array and put all odd numbers at the end and decrease the size counter lol.
If it's not homework and you want to learn C++ then stop programming in C with right now and pick up some tutorial or book from this decade...
This is the first step to improve your program. Don't code your own data structure. Also no new and delete.
Owen Foster
Stop trying to make him a code monkey, he needs to learn what happens under the hood instead of using magical libraries.
Owen Bell
use code tags op, i don't want to read your code otherwise
Justin Sullivan
cpp.sh/5cocq
Guys I hear you and all, but one step at a time. I've taken all your advice into consideration and will definitely get to that a bit later, but as says i'm trying to learn from the bare bones, every detail of C++, not just maximum efficiency stuff.
I'm using what I know to do it this way, so please, using the code I've already written can anyone help me achieve my goal of removing the even integers?
Data structures are taught after you pass OOP class, leave them for now.
Colton Smith
>learning OOP before data structures what's wrong with you
Kevin Jenkins
I did it in python
let sorter=function(array){ let neww=[]; for(i in array){ if(array[i]%2!=0){ neww.push(parseInt(array[i])); } } return neww; }
let notsepples=function(arr){ let srted=sorter(arr); while(true){ var a=prompt("this is actually ruby"); reg=/\D/; if(reg.test(a)){ break; } else{ srted.push(parseInt(a)); } } let srtedagain=sorter(srted); return srtedagain; }
Ryan Scott
is there a guide to code tags not butchering my spacing or something?
You shouldn't program without knowing what's underneath you. Rolling your own data structures for performance should only be done when profiling calls for it but as a learning exercise you've gotta know this stuff.