Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include <windows.h>
- using namespace std;
- typedef struct node Node;
- typedef struct node {
- int value;
- Node* next;
- };
- Node* create_node(int, Node*);
- void push_front(Node*&, int);
- void push_back(Node*&, int);
- int insert_node(Node*&, int, int);
- int pop_front(Node*&);
- int pop_back(Node*&);
- int delete_node(Node*&, int);
- void print_list(Node*);
- int main()
- {
- Node* head = NULL;
- char c, c1;
- int x, i, j, m;
- while (true) {
- cout << "Input '1' to push front" << endl;
- cout << "Input '2' to push back" << endl;
- cout << "Input '3' to insert at any index" << endl;
- cout << "Input '4' to pop front" << endl;
- cout << "Input '5' to pop back" << endl;
- cout << "Input '6' to delete at any index" << endl;
- cout << "Input 'X' to exit from the menu" << endl;
- cout << endl << "Enter your Choice: ";
- cin >> c;
- if (c == '1') {
- cout << endl << "Enter the integer to push front: ";
- cin >> x;
- push_front(head, x);
- cout << endl << "Pushing "<< x <<" at front";
- for (j = 0; j < 5; j++) {
- Sleep(100);
- cout << ".";
- }
- cout << endl;
- print_list(head);
- }
- else if (c == '2') {
- cout << endl << "Enter the integer to push back: ";
- cin >> x;
- push_back(head, x);
- cout << endl << "Pushing " << x << " at back";
- for (j = 0; j < 5; j++) {
- Sleep(100);
- cout << ".";
- }
- cout << endl;
- print_list(head);
- }
- else if (c == '3') {
- cout << endl << "Enter the integer to insert: ";
- cin >> x;
- cout << endl << "Enter the index position: ";
- cin >> i;
- m = insert_node(head, x, i);
- if (m) {
- cout << endl << "Inserting " << x << " at index " << i;
- for (j = 0; j < 5; j++) {
- Sleep(100);
- cout << ".";
- }
- }
- cout << endl;
- print_list(head);
- }
- else if (c == '4') {
- cout << endl << "Are you Sure to pop from front? Enter (Y/N) : ";
- cin >> c1;
- if (c1 == 'y' || c1 == 'Y') {
- m = pop_front(head);
- if (m) {
- cout << endl << "Poping from front";
- for (j = 0; j < 5; j++) {
- Sleep(100);
- cout << ".";
- }
- }
- cout << endl;
- }
- else {
- cout << endl << "Poping terminating";
- for (j = 0; j < 5; j++) {
- Sleep(100);
- cout << ".";
- }
- cout << endl;
- }
- print_list(head);
- }
- else if (c == '5') {
- cout << endl << "Are you Sure to pop from back? Enter (Y/N) : ";
- cin >> c1;
- if (c1 == 'y' || c1 == 'Y') {
- m = pop_back(head);
- if (m) {
- cout << endl << "Poping from back";
- for (j = 0; j < 5; j++) {
- Sleep(100);
- cout << ".";
- }
- }
- cout << endl;
- }
- else {
- cout << endl << "Poping terminating";
- for (j = 0; j < 5; j++) {
- Sleep(100);
- cout << ".";
- }
- cout << endl;
- }
- print_list(head);
- }
- else if (c == '6') {
- cout << endl << "Are you Sure to pop from back? Enter (Y/N) : ";
- cin >> c1;
- if (c1 == 'y' || c1 == 'Y') {
- cout << endl << "Enter the index to delete from : ";
- cin >> i;
- m = delete_node(head, i);
- if (m) {
- cout << endl << "Deteling at index " << i;
- for (j = 0; j < 5; j++) {
- Sleep(100);
- cout << ".";
- }
- }
- cout << endl;
- }
- else {
- cout << endl << "Deletion terminating";
- for (j = 0; j < 5; j++) {
- Sleep(100);
- cout << ".";
- }
- cout << endl;
- }
- print_list(head);
- }
- else if (c == 'X') {
- cout << endl << "Exitting";
- for (j = 0; j < 5; j++) {
- Sleep(300);
- cout << ".";
- }
- cout << endl;
- break;
- }
- else {
- cout << endl << "Invalid Input. Try again." << endl << endl;
- }
- }
- return 0;
- }
- Node* create_node(int value, Node* next) {
- Node* new_node = (Node*)malloc(sizeof(Node));
- if (new_node == NULL) {
- cout << "\nMemory Allocation Failed." << endl;
- return NULL;
- }
- new_node->value = value;
- new_node->next = next;
- return new_node;
- }
- void push_front(Node*& head, int value) {
- Node* new_node = create_node(value, NULL);
- if (new_node == NULL) {
- cout << "\nFailed to pop front." << endl;
- return;
- }
- if (head == NULL) {
- head = new_node;
- return;
- }
- Node* tmp = head;
- head = new_node;
- head->next = tmp;
- }
- void push_back(Node*& head, int value) {
- Node* new_node = create_node(value, NULL);
- if (new_node == NULL) {
- cout << "\nFailed to pop pack." << endl;
- return;
- }
- if (head == NULL) {
- head = new_node;
- return;
- }
- Node* tmp = head;
- while (tmp->next) {
- tmp = tmp->next;
- }
- tmp->next = new_node;
- }
- int insert_node(Node*& head, int value, int index) {
- if (index == 0) {
- push_front(head, value);
- return 1;
- }
- if (head == NULL) {
- cout << "\nHead is NUll. Failed to insert node at index " << index << endl;
- return 0;
- }
- int c = 1;
- Node* tmp = head;
- while (tmp->next && c < index) {
- tmp = tmp->next;
- c++;
- }
- if (c < index) {
- cout << "\nList is too short to insert node at index " << index << endl;
- return 0;
- }
- Node* new_node = create_node(value, NULL);
- if (new_node == NULL) {
- cout << "\nFailed to insert node at index " << index << endl;
- return 0;
- }
- Node* tmp2 = tmp->next;
- tmp->next = new_node;
- new_node->next = tmp2;
- return 1;
- }
- int pop_front(Node*& head) {
- if (head == NULL) {
- cout << "\nList is empty. Failed to pop at front." << endl;
- return 0;
- }
- head = head->next;
- return 1;
- }
- int pop_back(Node*& head) {
- if (head == NULL) {
- cout << "\nList is empty. Failed to pop at end." << endl;
- return 0;
- }
- if (head->next == NULL) {
- head = NULL;
- return 1;
- }
- Node* tmp = head;
- while ((tmp->next)->next) {
- tmp = tmp->next;
- }
- tmp->next = NULL;
- return 1;
- }
- int delete_node(Node*& head, int index) {
- int m;
- if (head == NULL) {
- cout << "\nList is empty. Failed to delete node at index " << index << endl;
- return 0;
- }
- if (index == 0) {
- m = pop_front(head);
- return m;
- }
- Node* tmp = head;
- if (tmp->next == NULL) {
- cout << "\nList is too short to delete node at index " << index << endl;
- return 0;
- }
- int c = 1;
- while ((tmp->next)->next && c < index) {
- tmp = tmp->next;
- c++;
- }
- if (c < index) {
- cout << "\nList is too short to delete node at index " << index << endl;
- return 0;
- }
- tmp->next = (tmp->next)->next;
- return 1;
- }
- void print_list(Node* head) {
- int j;
- cout << endl << "Printing List";
- for (j = 0; j < 3; j++) {
- Sleep(100);
- cout << ".";
- }
- cout << endl;
- while (head) {
- cout << head->value << " ";
- head = head->next;
- Sleep(500);
- }
- cout << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement