Question

In: Computer Science

C++ - Checks the relevance of a data structure in terms of following the interface specification...

C++ - Checks the relevance of a data structure in terms of following the interface specification for an ADT that represents a linear data structure:

Depending on the ADT of linear data structure, you must create the operations CRUD (Create, Read (search), Update, Delete) elements in the data structure. Some operations do not apply for certain data structures

Create:

Description: Insert an element in the data structure (create) according to the access policy of the structure

Input:Data structure and element to insert
Output: Valid data structure showing the insertion of the element
Precondition: A valid structure
Postcondition: Modified structure

Read:

Description: Read (search, obtain, etc.) an element within the data structure (read) according to the access policy of the structure
Input: The data structure and additional information about the item to retrieve (depending on the data structure)
Output: Element meeting the input or error criteria (there is no element with those characteristics)
Precondition: Valid data structure
Postcondition: Nothing

Update:

Description
Update an element within the data structure (update) according to the specific data structure

Input: Data structure, element to update, values ​​to modify
Output: Data structure duly updated
Precondition: A valid data structure
Postcondition: Data structure duly updated

Delete:

Description
Delete (remove) an element within the data structure (del) according to the specific data structure

Input: A data structure and criteria that determine the element to delete
Output: Updated data structure
Precondition: A valid data structure
Postcondition: Data structure duly updated

Solutions

Expert Solution

SOURCE CODE

#include <iostream>

using namespace std;

// node structure definition

struct node

{

    int data;

    struct node *next;

};

// defining list ADT

class linkedList

{

private:

    node *head;

public:

    // simple default constructor

    linkedList();

    // member functions

    void insert_first(int k);

    void delete_node(int pos);

    int search(int k);

    void update(int pos, int new_val);

    void display();

};

linkedList::linkedList()

{

    head = NULL;

}

// CREATE

// this function iunserts a value to the beginning of a list

void linkedList::insert_first(int k)

{

    node *ptr;

    ptr = new node;

    ptr->data = k;

    // if list is empty, insert new node at front

    if (head == NULL)

    {

        ptr->next = NULL;

        head = ptr;

    }

    // otherwise replace head with new node

    else

    {

        ptr->next = head;

        head = ptr;

    }

}

// READ

// This function is used to check the existance of a value

// in a linked list

int linkedList::search(int k)

{

    bool found = false;

    

    node *t = head;

    // if linked list is empty

    if(t == NULL)

        return false;

    

    // traverse the list to look for the value

    while(t->next != NULL)

    {

        // if the data has been found

        if(t->data == k)

        {

            found = true;

            break;

        }

        t = t->next;

    }

    // returns the result of reading the list

    return found;

}

// UPDATE

// alters the value of a particular node in

// linked list

void linkedList::update(int pos, int new_val)

{

    node *t = head;

    int i = 1;

    bool flag = false;

    // if list is empty

    if(t == NULL)

        cout << "\nLinked list is empty";

    else

    {

        // traverse the list

        while(t->next != NULL)

        {

            // if the value has been found

            // stop tarversing

            if(i == pos)

            {

                flag = true;

                break;

            }

            t = t->next;

            i++;

        }

        // if value was found, alter the value

        if (flag)

            t->data = new_val;

        else

            cout << "\nThe position was not found...";

        

    }

}

// DELETE

// function to delete a node at a particular position

void linkedList::delete_node(int pos)

{

    struct node *temp, *s, *ptr;

    int i, f = 0;

    // if position to delete is the first position

    if (pos == 1)

    {

        temp = head;

        head = head->next;

        delete temp;

    }

    // otherwise

    else

    {

        ptr = head->next;

        s = head;

        // traverse to the required position of list

        for (i = 2; i <= pos; i++)

        {

            if(ptr == NULL)

            {

                f = 1;

                break;

            }

            ptr = ptr->next;

            s = s->next;

        }

        if(f == 1)

        {

            cout <<"\nPosition is invalid";

            return;

        }

        s->next = ptr->next;

        delete ptr;

    }

}

// function to display the contents of linked list

void linkedList::display()

{

    int i;

    struct node *ptr;

    

    // if list is empty

    if (head == NULL)

        cout << "\n The list is empty";

    else

    {

        ptr = head;

        cout << "\nThe current state of linked list: ";

        // traverse through the list and print the elements

        while (ptr->next != NULL)

        {

            cout << ptr->data << "->";

            ptr = ptr->next;

        }

        cout << ptr->data;

    }

}

// main function to test the singly linked list implementation

int main()

{

    linkedList ll;

    cout << "\nInserting 10 at first of linked list...";

    ll.insert_first(10);

    cout << "\nInserting 20 at first of linked list...";

    ll.insert_first(20);

    cout << "\nInserting 30 at first of linked list...";

    ll.insert_first(30);

    cout << "\nInserting 40 at first of linked list...";

    ll.insert_first(40);

    ll.display();

    cout << "\nDeleting element at position 2...";

    ll.delete_node(2);

    ll.display();

    cout << "\nLooking for value 40 in linked list...";

    if(ll.search(40))

        cout << "\nFound";

    else

        cout << "\nNot found";

    cout << "\nLooking for value 30 in linked list...";

    if(ll.search(30))

        cout << "\nFound";

    else

        cout << "\nNot found";

    

    cout << "\nUpdating element at position 1...";

    ll.update(1, 21);

    ll.display();

    return 0;

}

OUTPUT


Related Solutions

data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer for all of them please. answer for all of them please
Define the following terms and, briefly, indicate their relevance in international finance: a)Absorption; b)Dollarization; c) Expenditure...
Define the following terms and, briefly, indicate their relevance in international finance: a)Absorption; b)Dollarization; c) Expenditure switching policy; d) A managed float;
Define the following terms. Give an example of the relevance of the term in an industrial...
Define the following terms. Give an example of the relevance of the term in an industrial setting. i.Boiling Point: ii. Vapor Pressure: iii. Lower Explosive Limit: iv. Upper Explosive Limit: v. Rate of Evaporation:
a. What is data mining? b. What is specification searching? c. Engaging in such behavior when...
a. What is data mining? b. What is specification searching? c. Engaging in such behavior when conducting empirical research is generally viewed negatively? Why?
C++, ArrayStack, data structure. Make a main using the following arraystack structure the main most do...
C++, ArrayStack, data structure. Make a main using the following arraystack structure the main most do the following Stack: Stack 1 and Stack 2 most have their own result I need to see the state of the stack1 and stack2 after doing the following operation. (stack1 and stack2 need to have individually there own state result) stack1.push(1) stack1.push(2) stack2.push(3) stack2.push(4) stack1.pop() stackTop = stack2.peek() stack1.push(stackTop) stack1.push(5) stack2.pop() stack2.push(6) ArrayStackInterface.h // Created by Frank M. Carrano and Tim Henry. // Copyright...
Define two (2) of the following terms and, briefly, indicate their relevance in international finance: a)...
Define two (2) of the following terms and, briefly, indicate their relevance in international finance: a) The Marshall-Lerner conditions; b) Absorption; c) Dollarization; d) Expenditure switching policy; e) A managed float; f) IMF conditionality; g) The Eurodollar (xenocurrency) market. h) External balance i) Direct controls j) Country risk
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an...
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an integer à The age of the vehicle       Price, a float à The price of the vehicle       Behaviors: Vehicle() à default constructor sets age=0, and price=0.0 setAge()   à Takes an integer parameter, returns nothing setPrice() à Takes a float parameter, returns nothing getAge()   à Takes no parameters, returns the vehicle’s age getPrice() à Takes no parameters, returns the vehicle’s price End Class Vehicle...
I need to write a program in C with the following specification * ​​​​​​​Line one of...
I need to write a program in C with the following specification * ​​​​​​​Line one of the standard input will have the total number of people which must not be greater than 10 * Each of the subsequent lines of input will be the first name of a person and their number (ex. "Adam 85") one space between name and number * Each name must be a maximum of 14 characters * Put the first names into an array called...
32. Write a proper decision structure that checks the following conditions and if both conditions are...
32. Write a proper decision structure that checks the following conditions and if both conditions are correct ,sets the value of the “Qualification’’ variable equal to True, if any of the conditions does not hold, set the value equal to False.   Bool Qualification; Condition1: Age Must be Greater than or equal to 21 Condition2: Weight Must be Greater than 190 lbs. USING C#
Given the data below, a lower specification of 78.2, and an upper specification of 98.9, what...
Given the data below, a lower specification of 78.2, and an upper specification of 98.9, what is the long term process performance (Ppk)? Data 101.4791 94.02297 95.41277 106.7218 90.35416 86.9332 94.87044 95.91265 93.98042 108.558 86.17921 85.01441 96.14778 92.59247 92.49536 87.4595 104.3463 90.57274 99.38992 80.61536 82.31772 97.05331 87.64293 103.3648 98.09292 87.72921 110.1765 86.76847 87.22422 94.88148 86.01183 91.43283 104.0907 97.77132 98.69138 95.55565 106.2402 95.96255 88.05735 100.2796 99.00995 86.18458 95.41366 99.32314 95.75733 88.82675 93.39986 98.34077 94.72198 99.14256 92.37767 94.94969 89.63531 87.56148 88.02731 97.57498...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT