-9
#include<iostream>
using namespace std;


struct Node{
    int x;
    Node *next;
};

typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
void Print(List L)
{
    Position P = L;
    while (P != NULL)
    {
        cout << P->x << " ";
        P = P->next;
    }

}


List InsertBeg(List L, int x){
    Node *tmp = new Node;
    tmp->x = x;
    if (L == NULL)
    {
        tmp->next = NULL;
        L = tmp;
    }
    else
    {
        tmp->next = L;
        L = tmp;
    }
    return L;
}

Position FindLast(List L){
    Position P = L;
    if (L != NULL)
    while (P->next != NULL)
        P = P->next;
    return P;
}


List deleteFirst(List L) {
    Position P = L;
    L = L->next;
    P->next = NULL;
    delete P;
    return L;
}
Position FindX(List L, int x){
    Position P = L;
    while (P != NULL && P->x != x){
        P = P->next;
    }
    return P;
}

Position FindPrevPos(List L, Position P){
    Position prev = L;
    if (L != NULL)
    if (P != NULL && (P != L))
    while (prev->next != P)
        prev = prev->next;
    return prev;
}
List deleteLast(List L) {
    Position last = FindLast(L);
    Position prev = FindPrevPos(L, last);
    prev->next = NULL;
    delete last;
    return L;
}






List deleteX(List L, int x)
{
    Position P = FindX(L, x);
    if (P == L)
        L = deleteFirst(L);
    else if (P->next == NULL)
        L = deleteLast(L);
    else {
        Position prev;
        prev = FindPrevPos(L, P);
        prev->next = P->next;
        P->next = NULL;
        delete P;
    }
    return L;
}
List deleteeven(List L){
    Position P = L;
    while (P != NULL)
    {
        if (P->x % 2 == 0)
            P = deleteX(L, P->x);
        P = P->next;
    }
    return L;
}





int main()
{
    List L = NULL;
    L = InsertBeg(L, 4);
    L = InsertBeg(L, 8);
    L = InsertBeg(L, 1);
    L = InsertBeg(L, 21);
    Print(L);

    L = deleteeven(L);
    cout << "After deleting the even numbers";
    Print(L);

    cin.get();
    cin.get();
    return 0;
}

I need to delete odds not evens, I have this for evens but I need to modify it and make it to delete odds, I'm trying and I can't modify it to delete odds! If you could modify this and make it to delete odds instead I would appreciate it a lot.

Thanks!

gtcodes
  • 15
  • 2
  • 5
  • 4
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Feb 06 '17 at 22:03
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Feb 06 '17 at 22:03
  • 3
    Looks like someone is teaching you C with IOstreams. Before you head down this path too far, [get a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn the differences between C and C++. – user4581301 Feb 06 '17 at 22:20
  • 1
    *Write a code which in a given linked list will delete elements with odd values* -- ok. `std::iist L;...L.remove_if([](int n){return n%2;});` -- Any other requests? – PaulMcKenzie Feb 06 '17 at 22:42

2 Answers2

1

Using std::list is a good solution, you can define your number like this:

list<int> numbers;

and remove Even numbers with this line of code:

numbers.remove_if([](int value) { return !(value % 2); });
Sam Mokari
  • 461
  • 3
  • 11
0

Try this.

List RemoveOdd(List L) {

    Position* prev = &L;
    Position cur   = L;
    while (cur) {
        if (cur->x % 2) {
            *prev = cur->next;
            delete cur;
            cur = *prev;
        } else {
            prev = &cur->next;
            cur  = cur->next;
        }
    }

    return L;

}
Sam Marinelli
  • 999
  • 1
  • 6
  • 16