#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!