forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rathoresrikant#393 from Vatuu/master
Removing duplicated numbers.
- Loading branch information
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |