Created
January 22, 2019 03:05
-
-
Save pprathameshmore/f2d21b40d2f750440828c5ff219dbbb4 to your computer and use it in GitHub Desktop.
Data Structure using C++
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
#include <iostream> | |
using namespace std; | |
int main(int argc, char const *argv[]) { | |
int a = 10; | |
int b = 20; | |
int c = 30; | |
//Pointer to variable | |
//int *newPtr; | |
//*newPtr = c; | |
//std::cout << "*newptr " << *newPtr << '\n'; | |
int *ptr = &a; | |
int **ptrDouble = &ptr; | |
std::cout <<"ptr = "<< ptr << '\n'; | |
std::cout << "&a = " << &a << '\n'; | |
std::cout <<"ptrDouble = "<< ptrDouble << '\n'; | |
std::cout <<"&ptr = "<< &ptr << '\n'; | |
std::cout <<"*ptrDouble = "<< *ptrDouble << '\n'; | |
std::cout <<"**ptrDouble = "<< **ptrDouble << '\n'; | |
std::cout << "*ptr = " << *ptr << '\n'; | |
*ptr = c; | |
std::cout << "ptr = "<< ptr << '\n'; | |
std::cout << "*ptr = " << *ptr << '\n'; | |
std::cout << "a = "<< a << '\n'; | |
return 0; | |
} |
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
#include <iostream> | |
#include <cstdio> | |
using namespace std; | |
struct Node { | |
int info; | |
struct Node *next; | |
struct Node *prev; | |
} *start; | |
class DoublyLinkedList { | |
public: | |
Node *create_node(int); | |
void insert_begin(); | |
void insert_last(); | |
void insert_position(); | |
void delete_pos(); | |
void search(); | |
void update(); | |
void sort(); | |
void print_reverse(); | |
void display(); | |
int counter(); | |
DoublyLinkedList() { | |
start = NULL; | |
} | |
}; | |
main() { | |
int choice; | |
DoublyLinkedList d; | |
start = NULL; | |
while (1) { | |
cout << endl << "-----------------------------------" << endl; | |
cout << endl << "Operation on Doubly LinkedList" << endl; | |
cout << endl << "-----------------------------------" << endl; | |
cout << "1. Insert at beginning" << endl; | |
std::cout << "2. Enter at last" << '\n'; | |
std::cout << "3. Enter at position" << '\n'; | |
std::cout << "4. Delete position" << '\n'; | |
std::cout << "5. Search" << '\n'; | |
std::cout << "6. Update" << '\n'; | |
std::cout << "7. Sort" << '\n'; | |
std::cout << "8. Print reverse" << '\n'; | |
std::cout << "9. Display" << '\n'; | |
std::cin >> choice; | |
switch (choice) { | |
case 1: | |
d.insert_begin(); | |
break; | |
case 2: | |
d.insert_last(); | |
break; | |
case 3: | |
d.insert_position(); | |
break; | |
case 4: | |
d.delete_pos(); | |
break; | |
case 5: | |
d.search(); | |
break; | |
case 6: | |
d.update(); | |
break; | |
case 7: | |
d.sort(); | |
break; | |
case 8: | |
d.print_reverse(); | |
break; | |
case 9: | |
d.display(); | |
break; | |
} | |
} | |
} | |
Node* DoublyLinkedList::create_node(int info) { | |
struct Node *temp = new (struct Node); | |
if (temp == NULL) { | |
std::cout << "Memory is not allocated" << '\n'; | |
return 0; | |
} else { | |
temp->info = info; | |
temp->next = NULL; | |
temp->prev = NULL; | |
return temp; | |
} | |
} | |
int DoublyLinkedList::counter() { | |
int count = 0; | |
struct Node *ptr = start; | |
while (ptr != NULL) { | |
count++; | |
ptr = ptr->next; | |
} | |
return count; | |
} | |
void DoublyLinkedList::insert_begin(){ | |
int value; | |
std::cout << "Inser the value" << '\n'; | |
std::cin >> value; | |
struct Node *temp = new (struct Node); | |
struct Node *ptr = new (struct Node); | |
temp = create_node(value); | |
ptr = start; | |
if (start == NULL) { | |
start = temp; | |
} else { | |
ptr->prev = temp; | |
temp->next = start; | |
temp->prev = NULL; | |
start = temp; | |
} | |
} | |
void DoublyLinkedList::insert_last(){ | |
int value; | |
std::cout << "Insert the value" << '\n'; | |
std::cin >> value; | |
struct Node *temp = create_node(value); | |
struct Node *ptr = start; | |
if (start == NULL) { | |
start = temp; | |
} else { | |
while (ptr->next != NULL) { | |
ptr = ptr->next; | |
} | |
temp->prev = ptr; | |
ptr->next = temp; | |
} | |
} | |
void DoublyLinkedList::insert_position() { | |
int value; | |
int position; | |
std::cout << "Enter the value" << '\n'; | |
std::cin >> value; | |
std::cout << "Enter the position" << '\n'; | |
std::cin >> position; | |
struct Node *temp = create_node(value); | |
struct Node *ptr = start; | |
struct Node *save = new (struct Node); | |
int count = counter(); | |
if (position == 0 || position > count) { | |
std::cout << "Invalid postion" << '\n'; | |
} | |
if (position == 1) { | |
ptr->prev = temp; | |
temp->next = ptr; | |
temp->prev = NULL; | |
start = temp; | |
} else { | |
ptr = start; | |
for (size_t i = 1; i < position; i++) { | |
save = ptr; | |
ptr = ptr->next; | |
} | |
temp->next = ptr; | |
temp->prev = save; | |
ptr->prev = temp; | |
save->next = temp; | |
} | |
} | |
void DoublyLinkedList::delete_pos() { | |
int position; | |
struct Node *ptr = start; | |
struct Node *save = new (struct Node); | |
std::cout << "Enter position" << '\n'; | |
std::cin >> position; | |
int count = counter(); | |
if (start == NULL) { | |
std::cout << "List is empty" << '\n'; | |
} | |
if (position == 0 || position > count) { | |
std::cout << "Invalide position" << '\n'; | |
} | |
if (position == 1) { | |
ptr = ptr->next; | |
ptr->prev = NULL; | |
start = ptr; | |
} else if (position < count) { | |
ptr = start; | |
for (size_t i = 1; i < position; i++) { | |
save = ptr; | |
ptr = ptr->next; | |
} | |
struct Node *ptr1 = ptr->next; | |
save->next = ptr1; | |
ptr1->prev = save; | |
free(ptr); | |
} else { | |
while (ptr->next != NULL) { | |
save = ptr; | |
ptr = ptr->next; | |
} | |
save->next = NULL; | |
free(ptr); | |
} | |
} | |
void DoublyLinkedList::search() { | |
int value; | |
std::cout << "Enter value to search" << '\n'; | |
std::cin >> value; | |
struct Node *ptr = start; | |
bool isFound = false; | |
int count = 0; | |
while (ptr != NULL) { | |
if (value == ptr->info) { | |
isFound = true; | |
} | |
count++; | |
ptr = ptr->next; | |
} | |
if (isFound) { | |
std::cout << "Element found at " << count << '\n'; | |
} else { | |
std::cout << "Element not found" << '\n'; | |
} | |
} | |
void DoublyLinkedList::update() { | |
int value; | |
int position; | |
std::cout << "Enter the value" << '\n'; | |
std::cin >> value; | |
std::cout << "Enter the position" << '\n'; | |
std::cin >> position; | |
struct Node *ptr = start; | |
struct Node *save = new (struct Node); | |
int count = counter(); | |
if (position == 0 || position > count) { | |
std::cout << "Invalid postion" << '\n'; | |
} | |
if (position == 1) { | |
ptr->info = value; | |
} else { | |
for (size_t i = 1; i < position; i++) { | |
ptr = ptr->next; | |
} | |
ptr->info = value; | |
} | |
} | |
void DoublyLinkedList::sort() { | |
struct Node *ptr = new (struct Node); | |
struct Node *temp = new (struct Node); | |
struct Node *next = new (struct Node); | |
ptr = start; | |
if (start == NULL) { | |
std::cout << "List is empty" << '\n'; | |
} else { | |
while (ptr->next != NULL) { | |
next = ptr->next; | |
if (ptr->info > next->info) { | |
temp->info = ptr->info; | |
ptr->info = next->info; | |
next->info = temp->info; | |
} | |
ptr = ptr->next; | |
} | |
} | |
} | |
void DoublyLinkedList::print_reverse() { | |
struct Node *ptr = new (struct Node); | |
ptr = start; | |
while (ptr->next != NULL) { | |
ptr = ptr->next; | |
} | |
struct Node *last = ptr; | |
while (last->prev != NULL) { | |
std::cout << last->info << "->"; | |
} | |
} | |
void DoublyLinkedList::display() { | |
struct Node *ptr = new (struct Node); | |
ptr = start; | |
if (start == NULL) { | |
std::cout << "List is empty" << '\n'; | |
} else { | |
while (ptr != NULL) { | |
std::cout << ptr->info << "->"; | |
ptr = ptr->next; | |
} | |
std::cout << "NULL" << '\n'; | |
} | |
} |
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
#include <iostream> | |
int add(int a, int b) { | |
return a + b; | |
} | |
int main(int argc, char const *argv[]) { | |
int (*p)(int, int); | |
p = &add; | |
int a = 10; | |
int b = 10; | |
int c = (*p)(a,b); | |
std::cout << "&c = " << &c << '\n'; | |
std::cout << "&add = " << &add << '\n'; | |
std::cout << "p = "<< *p << '\n'; | |
std::cout << c << '\n'; | |
return 0; | |
} |
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
#include <iostream> | |
using namespace std; | |
void A() { | |
std::cout << "Hello world" << '\n'; | |
} | |
void B(void (*ptr)()) { | |
ptr(); | |
} | |
int main(int argc, char const *argv[]) { | |
void (*p)() = A; | |
B(p); | |
return 0; | |
} |
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
#include <iostream> | |
#include <cstdio> | |
#include <cstdlib> | |
using namespace std; | |
struct Node { | |
int info; | |
struct Node *next; | |
} *start; | |
// Class Declaration | |
class SinglyLinkedList { | |
public: | |
Node *create_node(int); | |
void insert_begin(); | |
void insert_pos(); | |
void insert_last(); | |
void search(); | |
int count(); | |
void display(); | |
void sort(); | |
void delete_pos(); | |
//void reverse(); | |
void update(); | |
SinglyLinkedList() { | |
start = NULL; | |
} | |
}; | |
//Main Declaration | |
main() { | |
int choice; | |
SinglyLinkedList s; | |
start = NULL; | |
while (1) { | |
cout << endl << "-----------------------------------" << endl; | |
cout << endl << "Operation on Singly LinkedList" << endl; | |
cout << endl << "-----------------------------------" << endl; | |
cout << "1. Insert at beginning" << endl; | |
cout << "2. Insert at position" << endl; | |
cout << "3. Insert at last" << endl; | |
std::cout << "4. Display" << '\n'; | |
std::cout << "5. Searching" << '\n'; | |
std::cout << "6. Sort" << '\n'; | |
std::cout << "8. Reverse" << '\n'; | |
std::cout << "7. Delete position" << '\n'; | |
std::cout << "9. Update" << '\n'; | |
std::cin >> choice; | |
switch (choice) { | |
case 1: | |
std::cout << "Inserting at beginning" << '\n'; | |
s.insert_begin(); | |
break; | |
case 2: | |
std::cout << "Inserting at position" << '\n'; | |
s.insert_pos(); | |
break; | |
case 3: | |
std::cout << "Inserting at last" << '\n'; | |
s.insert_last(); | |
break; | |
case 4: | |
std::cout << "Display" << '\n'; | |
s.display(); | |
break; | |
case 5: | |
std::cout << "Search" << '\n'; | |
s.search(); | |
break; | |
case 6: | |
std::cout << "Sort" << '\n'; | |
s.sort(); | |
break; | |
case 7: | |
std::cout << "Delete postion" << '\n'; | |
s.delete_pos(); | |
break; | |
case 8: | |
std::cout << "Update value" << '\n'; | |
s.update(); | |
break; | |
} | |
} | |
} | |
int SinglyLinkedList::count() { | |
struct Node *temp = new (struct Node); | |
temp = start; | |
int count = 0; | |
while (temp != NULL) { | |
count++; | |
temp = temp->next; | |
} | |
return count; | |
} | |
Node *SinglyLinkedList::create_node(int value) { | |
struct Node *temp = new (struct Node); | |
if (temp == NULL) { | |
std::cout << "Memory is not allocated" << '\n'; | |
return 0; | |
} else { | |
std::cout << "Node creating" << '\n'; | |
temp->info = value; | |
temp->next = NULL; | |
return temp; | |
std::cout << "Node created" << '\n'; | |
} | |
} | |
void SinglyLinkedList::insert_begin() { | |
int value; | |
std::cout << "Insert the value" << '\n'; | |
std::cin >> value; | |
struct Node *temp = new (struct Node); | |
struct Node *p = new (struct Node); | |
temp = create_node(value); | |
if (start == NULL) { | |
start = temp; | |
start->next = NULL; | |
} else { | |
p = start; | |
start = temp; | |
start->next = p; | |
} | |
std::cout << "Node added" << '\n'; | |
} | |
void SinglyLinkedList:: insert_pos() { | |
int value; | |
int pos; | |
cout << "Ente the value to be inserted"; | |
cin >> value; | |
cout << "Enter the position to be inserted"; | |
cin >> pos; | |
Node *temp = create_node(value); | |
Node *p = new (struct Node); | |
p = start; | |
int counter; | |
counter = count(); | |
if (pos > counter || pos == 0) { | |
cout << "Invalid position"; | |
} | |
if (pos == 1) { | |
temp->next = start->next; | |
start = temp; | |
} else { | |
for (int i = 1; i < pos - 1; i++) { | |
p = p->next; | |
} | |
temp->next = p->next; | |
p->next = temp; | |
} | |
} | |
void SinglyLinkedList::insert_last() { | |
struct Node *temp = new (struct Node); | |
struct Node *p = new (struct Node); | |
int value; | |
cout << "Ente the value to be inserted"; | |
cin >> value; | |
temp = create_node(value); | |
p = start; | |
if (start == NULL) { | |
start = temp; | |
} else { | |
while (p->next != NULL) { | |
p = p->next; | |
} | |
p->next = temp; | |
} | |
} | |
void SinglyLinkedList::search() { | |
int value, pos = 0; | |
bool flag = false; | |
if (start == NULL) | |
{ | |
cout<<"List is empty"<<endl; | |
return; | |
} | |
cout<<"Enter the value to be searched: "; | |
cin>>value; | |
struct Node *s; | |
s = start; | |
while (s != NULL) | |
{ | |
pos++; | |
if (s->info == value) | |
{ | |
flag = true; | |
cout<<"Element "<<value<<" is found at position "<<pos<<endl; | |
} | |
s = s->next; | |
} | |
if (!flag) | |
cout<<"Element "<<value<<" not found in the list"<<endl; | |
} | |
void SinglyLinkedList::display() { | |
struct Node *temp = new(struct Node); | |
temp = start; | |
if (start == NULL) { | |
std::cout << "The list is empty" << '\n'; | |
return; | |
} | |
std::cout << "Elements of list are" << '\n'; | |
while (temp != NULL) { | |
std::cout << temp->info << "->"; | |
temp = temp->next; | |
} | |
std::cout << "NULL" << '\n'; | |
} | |
void SinglyLinkedList::sort() { | |
struct Node *temp = new (struct Node); | |
struct Node *ptr = new (struct Node); | |
ptr = start; | |
int value; | |
int c = 0; | |
c = count(); | |
while (ptr != NULL) { | |
for (temp = ptr->next; temp != NULL; temp = temp->next) { | |
if (ptr->info > temp->info) { | |
value = ptr->info; | |
ptr->info = temp->info; | |
temp->info = value; | |
} | |
} | |
ptr = ptr->next; | |
} | |
} | |
void SinglyLinkedList::delete_pos() { | |
int c = count(); | |
int position; | |
struct Node *ptr = new (struct Node); | |
struct Node *prev = new (struct Node); | |
ptr = start; | |
if (start == NULL) { | |
std::cout << "List is empty" << '\n'; | |
} | |
std::cout << "Enter position" << '\n'; | |
std::cin >> position; | |
if (position == 1) { | |
start = ptr->next; | |
} else { | |
if (position <= c && position > 0) { | |
ptr = start; | |
for (size_t i = 1; i < position; i++) { | |
prev = ptr; | |
ptr = ptr->next; | |
} | |
prev->next = ptr->next; | |
} else { | |
std::cout << "Positon is not valid" << '\n'; | |
} | |
free(ptr); | |
} | |
} | |
void SinglyLinkedList::update() { | |
struct Node *temp = new (struct Node); | |
int position; | |
temp = start; | |
int value; | |
int c = count(); | |
std::cout << "Enter the node postion to be updated" << '\n'; | |
std::cin >> position; | |
std::cout << "Enter the new value" << '\n'; | |
std::cin >> value; | |
if (start == NULL) { | |
std::cout << "List is empty" << '\n'; | |
} | |
if (position == 1) { | |
temp->info = value; | |
} | |
if (position > 0 && position <= c) { | |
temp = start; | |
for (size_t i = 1; i < position; i++) { | |
temp = temp->next; | |
} | |
temp->info = value; | |
} else { | |
std::cout << "Invalid position" << '\n'; | |
} | |
} |
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
#include <iostream> | |
using namespace std; | |
struct node { | |
int info; | |
struct node *link; | |
} *top; | |
class stack_list { | |
public: | |
node *push(node *, int); | |
node *pop(node *); | |
void traverse(node *); | |
stack_list() { | |
top = NULL; | |
} | |
}; | |
int main(int argc, char const *argv[]) { | |
int choice, item; | |
stack_list s1; | |
while (1) { | |
std::cout << "Operations on stack" << '\n'; | |
std::cout << "1. Push" << '\n'; | |
std::cout << "2. Pop" << '\n'; | |
std::cout << "3. Traverse" << '\n'; | |
std::cout << "4. Quit" << '\n'; | |
std::cin >> choice; | |
switch (choice) { | |
case 1: | |
std::cout << "Enter value to pushed on stack" << '\n'; | |
std::cin >> item; | |
top = s1.push(top, item); | |
break; | |
case 2: | |
std::cout << "Popping element from stack" << '\n'; | |
top = s1.pop(top); | |
break; | |
case 3: | |
std::cout << "Traverse" << '\n'; | |
s1.traverse(top); | |
break; | |
case 4: | |
exit(1); | |
break; | |
default: | |
std::cout << "Wrong choice" << '\n'; | |
} | |
} | |
return 0; | |
} | |
node *stack_list :: push(node *top, int item) { | |
node *temp; | |
temp = new (struct node); | |
temp->info = item; | |
temp->link = top; | |
top = temp; | |
return top; | |
} | |
node *stack_list :: pop(node *top) { | |
node *temp = new (struct node); | |
if (top == NULL) { | |
std::cout << "Stack is empty" << '\n'; | |
} else { | |
temp = top; | |
std::cout << "Element opped" << '\n'; | |
top = top->link; | |
delete(temp); | |
} | |
return top; | |
} | |
void stack_list::traverse(node *top) | |
{ | |
node *ptr; | |
ptr = top; | |
if (top == NULL) | |
cout<<"Stack is empty"<<endl; | |
else | |
{ | |
cout<<"Stack elements :"<<endl; | |
while (ptr != NULL) | |
{ | |
cout<<ptr->info<<endl; | |
ptr = ptr->link; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment