Question

In: Computer Science

Linked List Course Outcome: CLO3    Build computer programs to implement appropriate data structures for solving computing...

Linked List

Course Outcome:

CLO3    Build computer programs to implement appropriate data structures for solving computing problems in a time frame given. (P3, PLO3)

Task

Write a complete program by applying linked list data structure that will perform the following operation on any records:

  1. Add record
  2. Delete record
  3. View record
  4. Find record

Your program also should be able to display the number of record in the linked list. Put your creativity to produce the program.

Your Submission should have the following components:

  1. Program description – explain in detail about the program that you are develop.
  2. Coding – Your developed program. Make sure you put appropriate comment in your program.
  3. Sample output – Give sample output for each operation with appropriate label or description.

Solutions

Expert Solution

Step1:

Everything About Code is Explained in Comments of Code:

#include <iostream>
using namespace std;
struct node
{
    int data;
    node *next;
    node(int val)
    {
        data = val;
        next = nullptr;
    }
};
class LinkedList
{
private:
    node *head;
    node *end;

public:
    //This functin will add the node
    LinkedList()
    {
        head = end = nullptr;
    }
    void add(int val)
    {
        node *temp = new node(val);
        if (end == nullptr)
        {
            end = temp;
            head = end;
        }
        else
        {
            end->next = temp;
            end = end->next;
        }
    }
    //This function will delete the node having the data as key
    void deleteNode(int key)
    {
        node *temp = head;
        node *prev = head;
        //checking if first node is same as key
        if (temp != nullptr && temp->data == key)
        {
            head = temp->next;
            free(temp);
            return;
        }
        while (temp != nullptr && temp->data != key)
        {
            prev = temp;
            temp = temp->next;
        }
        if (temp == nullptr)
            return;
        prev->next = temp->next;
        free(temp);
    }
    //This Function will print the data of Linked List
    void view_record()
    {
        cout << "Data : ";
        node *temp = head;
        while (temp != nullptr)
        {
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }
    //This Function will return index of Node at which value gets find otherwise -1
    int find(int val)
    {
        node *temp = head;
        int res = 0;
        while (temp != nullptr)
        {
            if (temp->data == val)
            {
                return res;
            }
            temp = temp->next;
            res++;
        }
        return -1;
    }
};
int main()
{
    LinkedList list;
    int n, val;
    cout << "Enter the number of Elements:\n";
    cin >> n;
    cout << "Enter the Data:\n";
    for (int i = 0; i < n; i++)
    {
        cin >> val;
        list.add(val);
    }
    list.view_record();
    cout << "Enter the value to be delete: \n";
    cin >> val;
    list.deleteNode(val);
    list.view_record();
    cout << "Enter the value to find: \n";
    cin >> val;
    int temp = list.find(val);
    if (temp != -1)
        cout << "Value is present in LinkedList at index: " << temp << endl;
    else
    {
        cout << "Value Entered by User is not present:\n";
    }
    return 0;
}

Output:

Thanks


Related Solutions

Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary...
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary data structure using the prototype. Run some tests that show that your code works. 2. Implement the hash table data structure using the prototypeRun some tests that show that your code works. 3. The book discusses linear probing, but their approach has a serious problem. What is the issue? 4. Complete the method below that adds all key-value pairs from one dictionary into another....
This is a C++ based question that involves Data Structures and Algorithms. Q. Application: Linked List...
This is a C++ based question that involves Data Structures and Algorithms. Q. Application: Linked List of Bus Transit and Passengers You are to implement a C++ program for City Bus Transit using linked list data structure to maintain record of passengers. Specifically, you are to implement the following methods/functions: For Passenger: o A function which can create a new node of the linked list using new for each newpassenger o A function that prints the time of single passenger...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
Implement in a computer the curve fitting. Generate appropriate data for your implementation (e.g., generate data...
Implement in a computer the curve fitting. Generate appropriate data for your implementation (e.g., generate data from a polynomial function and add noise with variance σ2). Show the mean square error of the estimator in your implementation. What can you say about under/over fitting (that is, when the degree of your approximator is too small or too large for your data).
Write a program to implement linked list data structure that will have following functions: a. Append...
Write a program to implement linked list data structure that will have following functions: a. Append a node in the list b. Insert a node in the list c. Delete a node from the list d. Display list e. Find maximum value in the list f. Find how many times a value exists in the list. g. Search Portion of the code is give below. You have to write code for the items (e, f, g) Program: #include<stdlib.h> #include<stdio.h> #include<iostream>...
By using javaFX as the GUI, design and implement java based algorithms using appropriate data structures...
By using javaFX as the GUI, design and implement java based algorithms using appropriate data structures for the following problem: Use depth-first search to find paths to all the vertices in a graph that are connected to a given start vertex s. A sample input file containing the number of vertices, number of edges and a list of edges called tinyCG.txt is provided for you to test your program.
PLEASE NO USE OF LINKED LIST OR ARRAYS(USE CHARACTER POINTERS INSTEAD) TO HOLD DATA STRUCTURES This...
PLEASE NO USE OF LINKED LIST OR ARRAYS(USE CHARACTER POINTERS INSTEAD) TO HOLD DATA STRUCTURES This lab, for which you may work in groups of two, will require you to use a C structure to hold a record of any kind of data, i.e., address book, library of books as with chapter 14, etc. These records will be held in dynamic memory using memory allocation (malloc) to create new memory and the function (free) to release memory created via (malloc)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT