Skip to content

Commit

Permalink
Merge pull request rathoresrikant#393 from Vatuu/master
Browse files Browse the repository at this point in the history
Removing duplicated numbers.
  • Loading branch information
Srikant Singh authored Oct 31, 2018
2 parents 86afb6c + 8767301 commit c26266f
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/cpp/remove_duplicated_numbers/linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "linked_list.h"

int linked_list::get(unsigned int position)
{
if (position == 0)
return head->value;
if (position > list_size)
return -1;

linked_list_node *previous = new linked_list_node;
linked_list_node *current = new linked_list_node;
current = head;
for(unsigned int i = 1; i < position; i++)
{
previous = current;
current = current->next;
}
return current->value;
}

void linked_list::set(unsigned int position, int value)
{
if (position == 0)
head->value = value;
if (position > list_size)
return;

linked_list_node *previous = new linked_list_node;
linked_list_node *current = new linked_list_node;
current = head;
for (unsigned int i = 1; i < position; i++)
{
previous = current;
current = current->next;
}
current->value = value;
}

void linked_list::add_node(int value)
{
linked_list_node *temp = new linked_list_node;
temp->value = value;
temp->next = nullptr;
if(head == nullptr)
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
list_size++;
}

void linked_list::insert_node(unsigned int position, int value)
{
if (position == 0)
insert_at_head(value);
else if (position > list_size)
add_node(value);
else
{
linked_list_node *previous = new linked_list_node;
linked_list_node *current = new linked_list_node;
linked_list_node *temp = new linked_list_node;
current = head;
for(unsigned int i = 1; i < position; i++)
{
previous = current;
current = current->next;
}
temp->value = value;
previous->next = temp;
temp->next = current;
}
list_size++;
}



void linked_list::insert_at_head(int value)
{
linked_list_node *temp = new linked_list_node;
temp->value = value;
temp->next = head;

head = temp;

list_size++;
}

void linked_list::delete_node(unsigned position)
{
if (position > list_size)
return;

linked_list_node *previous = new linked_list_node;
linked_list_node *current = new linked_list_node;
current = head;
for (unsigned int i = 1; i < position; i++)
{
previous = current;
current = current->next;
}
previous->next = current->next;
current = nullptr;

list_size--;
}



size_t linked_list::size()
{
return list_size;
}
31 changes: 31 additions & 0 deletions src/cpp/remove_duplicated_numbers/linked_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
struct linked_list_node
{
int value;
linked_list_node *next;
};

class linked_list
{
linked_list_node *head, *tail;
size_t list_size;

public:
linked_list()
{
head = nullptr;
tail = nullptr;
list_size = 0;
}

int get(unsigned int position);
void set(unsigned int position, int value);
void add_node(int value);
void insert_node(unsigned int position, int value);
void delete_node(unsigned int position);
void insert_at_head(int value);

size_t size();
};
#endif
35 changes: 35 additions & 0 deletions src/cpp/remove_duplicated_numbers/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "linked_list.h"
#include <iostream>

int main()
{
linked_list *list = new linked_list;
list->add_node(1);
list->add_node(2);
list->add_node(2);
list->add_node(3);
list->add_node(3);
list->add_node(4);
list->add_node(5);
list->add_node(1);

int numbers[10];

for(int i = 0; i < list->size(); i++)
{
int value = list->get(i);
for(int j = 0; j < 10; j++)
{
if (numbers[j] != value)
continue;

list->delete_node(i);
break;
}
}

for(int i = 0; i < list->size(); i++)
cout << list->get(i) << endl;

return 0;
}

0 comments on commit c26266f

Please sign in to comment.