Results 1 to 4 of 4

Thread: c++ code help version 3.     submit to reddit submit to twitter

  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    c++ code help version 3.

    Ok basicly for this assigment we have to make a linked list of size n(user defined). Then have to cout every element in the list. After this we have to ask the user which group of numbers to delete (even numbers/odd numbers) then cout the new list.

    ex:
    user input n: 5

    cout on screen: 1,2,3,4,5

    user input: even

    cout on screen: 1,3,5

    i've got the linked list down fine but the odd/even is confusing me. was trying to do something like:

    Code:
    for(int i = 0; i < n; i++) {
       if(i % 2 == 0) arr[i] = true;
    }
    cout << arr[i] << endl;
    but not really sure can anyone give some advice please.

    Current code is:

    Code:
    #include <iostream>
    using namespace std;
    
    struct Node {
        int data;
        Node* next;
    };
    
    void print(Node* line) {
        Node* list = line;
        while(list != NULL) {
            cout << list->data << ", ";
            list = list->next;
        }
    	
    }
    
    
    void main() {
    	int n;
    	cout << "Enter Positive int:";
    	cin >> n;
        Node* head;
        head = new Node;
        head->data = 1;
        head->next = NULL;
        Node* temp = head;
        for(int i = 2; i < n; i++) {
            temp->next = new Node;
            temp = temp->next;
            temp->data = i;
            temp->next = NULL;
        }
    	cout <<" " << endl;
        print(head);
    	cout << n << endl;
    	cout <<" " << endl;
    	int choice;
    	cout <<"Even or odd (1/2): ";
    	cin >> choice;
    	
    	if(choice == 1){
    	//do even
    	}
    	else{
    	//do odd
    	}
    	
    	
        while(head != NULL) {
            temp = head;
            head = head->next;
            delete temp;
        }
    }

  2. #2
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Ok i figured out a solution kinda. Its a getto way of doing it but it works. I would perfer to do it actualy using the linked list numbers. also when it prints the list minus the odd numbers it leaves out 1.

    Code:
    #include <iostream>
    using namespace std;
    
    struct Node {
        int data;
        Node* next;
    };
    
    void print(Node* line) {
        Node* list = line;
        while(list != NULL) {
           cout << list->data << ", ";
           list = list->next;
        }
    }
    
    void main() {
    	int n;
    	cout << "Enter Positive int:";
    	cin >> n;
        Node* head;
        head = new Node;
        head->data = 1;
        head->next = NULL;
        Node* temp = head;
        for(int i = 2; i < n; i++) {
            temp->next = new Node;
            temp = temp->next;
            temp->data = i;
            temp->next = NULL;
        }
    	cout <<" " << endl;
        print(head);
    	cout << n << endl;
    	cout <<" " << endl;
    	int choice;
    	cout <<"Would you like to delete the Even or odd (1/2) numbers: ";
    	cin >> choice;
    	
    	if(choice == 1){
    		for (int i = 0; i <= n; i++)
    			if (i % 2 != 0) {
    			cout << i << endl;
    			}
    		cout << "Even Numbers Deleted" << endl;
    		
    	}
    	else{
    		for (int i = 0; i <= n; i++)
    			if (i % 2 == 0) {
    			cout << i << endl;
    			}
    		cout << "Odd Numbers Deleted" << endl;
    	}
    	while(head != NULL) {
           temp = head;
           head = head->next;
           delete temp;
        }
    }

  3. #3
    Fake Numbers
    Join Date
    Jul 2008
    Posts
    90
    BG Level
    2

    Since 1 is at arr[0], 2 is at arr[1], etc...
    to print odd numbers:
    Code:
    for (int i = 0; i < n; i += 2) {
        cout << arr[i];
    }
    to print even numbers:
    Code:
    for (int i = 1; i < n; i += 2) {
        cout << arr[i];
    }

  4. #4
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    If I was your teacher/TA I'd give you no credit for avoiding the actual delete from the linked list (after all, you don't even need to build a linked list for the initial/post-delete couts since you know what's being displayed...but that's not the point of the assignment).

    What you really need to do is make an actual linked list structure/class (not just the node) and make a function to add/delete from the linked list, which will take care of the internal logic of adding/removing nodes, rather than doing it in the main function.

    Then use a node pointer to traverse the list, call delete on the appropriate nodes, and have your delete function return a pointer to the next node (or null) so you can continue traversing.

    it'd be something like
    Code:
    NodePtr=head;
    while(NodePtr!=NULL)
    {
       if(NodePtr->data == (whatever criteria for deleting))
           NodePtr = delete(NodePtr); //will return the new (Previous Node in the list)->Next
       else
           NodePtr=NodePtr->next;
    }
    print etc...

Similar Threads

  1. Java Code help
    By Skie in forum Tech
    Replies: 11
    Last Post: 2010-10-11, 14:37
  2. c++ code help
    By Skie in forum Tech
    Replies: 19
    Last Post: 2010-02-17, 21:27
  3. CS Code help, number program
    By Skie in forum Tech
    Replies: 8
    Last Post: 2009-11-13, 13:57
  4. Windows XP code help
    By Etice in forum Tech
    Replies: 3
    Last Post: 2009-07-16, 14:25
  5. Japanese version help
    By rushyrushrush in forum Tech
    Replies: 2
    Last Post: 2007-11-22, 21:37