Skip to main content

Algorithm Analysis & Design code for single linked-list program in c++

Single Linked -listCODES FOR THE FOLLOWING TASKS ARE AS UNDER:

Tasks: 

1.       Insertion of value at any center point.
2.       Insertion of value before and after a node.
3.       Print the Total no. of nodes.
4.       Values swapping.
5.       Printing the  values.
6.       Deleting the values.



codes:



#include <iostream>
#include <stdlib.h>
using namespace std;

struct Node
{
       int data;
       Node *next;
};

void InsertAnodeBeforeAnode(Node *&head, Node *&last, int data ) {
       if (head == NULL)
       {
              Node *temp = new Node;
              temp->data = data;
              temp->next = NULL;
              head = temp;
              last = temp;
       }
       else
       {
              Node *temp = new Node;
              temp->data = data;
              temp->next = NULL;
              last->next = temp;
              last = temp;
       }
}



void filledNodeList(Node *&head, Node *&last) {
       int data;
       for (int i = 0; i <= 5; i++) {
              data = rand() % 100;
              InsertAnodeBeforeAnode(head, last, data);
       }
}



void AddBeforeSpecificNode(Node *&head, Node *&last, int data, int number) {
       Node *temp=NULL;
       Node *tempHead = head;

       while (head->data != number) {
              temp = head;
              head = head -> next;
       }
      
       Node *temp1 = new Node;
       temp1->data = data;
       temp->next = temp1;
       temp1->next = head;
       head = tempHead;

}
int Print(Node *head) {
       int items = 0;
       if (head == NULL)
       {
              cout << "The List is Empty!" << endl;
       }

       else {
              cout << "current LinkedList : ";
              while (head != NULL)
              {
                     cout << head->data << ", ";
                     head = head->next;
                     items++;
              }
              cout << endl << endl;
       }
       return items;
}

void DeletePrevious(Node *&head, Node *&last, int number) {
       Node *prevTemp = NULL;
       Node *temp=NULL;
       Node *tempHead = head;
       int counter = 0;
       while (head->data != number) {
              prevTemp = temp;
              temp = head;
              head = head->next;
              counter++;
       }
       if (counter ==1 ) {
              delete(temp);
              Print(head);
       }

       else
       {
              prevTemp->next = head;
              delete(temp);
              head = tempHead;
              Print(head);
       }
}

void DeleteBothSides(Node *&head, Node *&last, int number) {
       Node *prevTemp = NULL;
       Node *temp = NULL;
       Node *tempNext = NULL;
       Node *tempNextToNext = NULL;
       Node *tempHead = head;
       int counter = 0;
       while (head->data != number) {
              prevTemp = temp;
              temp = head;
              head = head->next;
              tempNext = head->next;
              tempNextToNext = tempNext->next;
              counter++;
       }
      
      
       if (counter == 1) {
              delete(temp);
              delete(tempNext);
              Print(head);
       }     
       else
       {
              prevTemp->next = head;
              head->next = tempNextToNext;
              delete(temp);
              delete(tempNext);
              head = tempHead;
              Print(head);
       }
}

void swap(Node *head, Node *last, Node *head1, Node *last1, int number, int number1) {

       if (head == NULL) {
              cout << "The List is empty" << endl;
       }
       else if (head == last) {
              cout << "Invalid move" << endl;

       }
       else {
              while (head->data != number) {
                     head = head->next;
              }

              while (head1->data != number1) {
                     head1 = head1->next;
              }

              int n;
              n = head1->data;
              head1->data = head->data;
              head->data = n;
       }

}
bool compareInput(Node *head, int number) {
       bool flag = false;
       while (head != NULL) {
              if (head->data == number)
              {
                     flag = true;
                     break;
              }
              else
                     flag = false;
              head = head->next;
       }
       return flag;
}
void main() {
       Node *head = NULL;
       Node *last = NULL;
       int opt;

       filledNodeList(head, last);
       cout << "\t\t\t List of 5 Nodes including Random values from 0-100" <<endl;
       cout << "\t\t\t\t\t\t\t\t\t\t\t\t " << endl;
       Print(head);
start:
       cout << "\t\t\t***** MAIN MENU TO SELECT *******" << endl;
              cout << "\t\t\t\t\t\t\t\t\t\t\t\t " << endl;
       cout << "\t1) Add a node before a specific node with your choice value" << endl;
       cout << "\t2) Delete a node before specific node" << endl;
       cout << "\t3) Delete a before and after node of a specific node" << endl;
       cout << "\t4) Totak number of nodes" << endl;
       cout << "\t5) Swap a node with another node" << endl;
       cout << "\t6) Print full current linked list" << endl;
       cout << " " << endl;
       cout<<"Enter your choice below:";
       cin >> opt;

       if(opt==1){
              bool flag = false;
              int number, data;
              cout << "Enter a Node's Number to add a node before it with user input value: ";
              cin >> number;
              flag = compareInput(head, number);
              if (flag) {
                     cout << "Enter the value to add in the node: ";
                     cin >> data;
                     AddBeforeSpecificNode(head, last, data, number);
                     Print(head);
              }
              else if (!flag)
                     cout << "The Number You've entered does not found in the List" << endl;
              cout<<"Enter the correct number" << endl;
              }

       else if(opt==2){
              int number;
              bool flag = false;

              cout << "Enter the value of the node to delete its previous node and next node: ";
              cin >> number;
              flag = compareInput(head,number);
              if (flag) {
                     DeletePrevious(head, last, number);
              }
              else
                     cout << "The Number You've entered does not found in the List" << endl;
              cout<<"Please Enter The Correct n umber" << endl;
       }

       else if (opt == 3) {
              int number;
              bool flag = false;
              cout << "Enter number to delete its previous node: ";
              cin >> number;
              flag = compareInput(head, number);
              if (flag) {
                     DeleteBothSides(head, last, number);
              }
              else
                     cout << "The Number does not found! Enter The Correct number" << endl;
       }
      
       else if (opt == 4) {
              int listLength;
              listLength = Print(head);
              cout << "List Length: " << listLength << endl;
       }

       else if (opt == 5) {
              int number1, number2;
              cout << "PLease enter two values from the given linked list to swap" << endl;
              cout << "Enter 1st value: ";
              cin >> number1;
              cout << "Enter 2nd value: ";
              cin >> number2;
              swap(head, last, head, last, number1, number2);
              Print(head);
       }

       else if (opt == 6) {
              Print(head);
       }
       else
              cout << "INVALID OPTION SELECTED!" << endl;
       goto start;
}





THANKS !


Comments

Popular posts from this blog

DATA LOGIC DESIGN

C# basic code using functions | MODERN PROGRAMMING LANGUAGE

TASK:                     MAKE A PROGRAM USING ENUM METHOD:  PROGRAM:   MAKE A PROGRAM THAT DISPLAYS A BAG AND INSIDE A BAG TOTAL NUMBER OF BOOKS AND PEN. CODE: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace bag {     public class bagd     {         public double price;         public string color;         private List < Object > l;         public bagd( double p, string c)         {             price = p;             color = c;       ...
                  Software Engineering  a report on:                      "Standards" What is meant by Standard? A level of quality or attainment Something used as a measure , norm or model in comparative evaluations Used or accepted as normal or average What is Software Engineering Standard? Standards are documented agreements containing technical specifications or other precise criteria to be used consistently as rules, guidelines or definitions of characteristics, to ensure that materials, products, processes and services are fit for their purpose. [ISO 1997] Standards are about providing rules, guidelines and heuristics which, if followed deliver an assurance of “good practice” – they are not intended to be about “best practice”. Why adopt a standard? As a means of transferring ‘good practice’ in software engineering As a result of the deman...