Question

In: Computer Science

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). Do not use linked lists for this assignment, we are using dynamic memory allocation to hold records, not using linked lists. The structures will act as an in memory database for your records (structures), and you will need to create enough memory to hold all of your records, but also when you remove records, you will need to allocate new memory, move the data over to that memory space, and free the old memory (instead of using a static size array). No use of arrays is allowed to hold your structures, but you can use arrays in other places in your program. Your program will prompt for information that will be added to a new structure that will be added when you call the add function. Delete is as simple as just taking the last record out of your database (i.e. no need to search for a “record” to delete - this assignment is about pointers and memory allocation / free, so no need to make the algorithm more complicated). In addition to your memory to hold your data / structures, your program will also need to hold a static duration memory type that will serve as the counter for how many times the database has changed. Along with the amount of times the database has changed, you will also need to have a variable to hold the number of records in your database, functions to calculate size (records multiplied by sizeof struct), and functions to add, print, and delete records. You will not be required to use lookup functions, print is just the entire database. To manage your in-memory database, you will need to use some pointers to hold locations of your data. Please take some time to write down examples of where you will need to have pointers. You will need to have at least a pointer to the beginning of your database memory, another to show which record you are on, but think about the need for other pointers when you think about functions that will delete a record, add a record, etc. One of the major points of this assignment is not just the ability to manage records in memory, it is also about how to manage the memory itself. There is a huge inherent danger in how memory is allocated and subsequently not released properly which will create a “memory leak” in your program which will consume memory over time and crash your system (due to all the memory being used, or your process hitting a max limit of memory). This will mean that you will need to manage how pointers are pointing at data very carefully as it is very easy to create a memory leak and your program will not crash, it will just not release memory properly. You will need a menu to prompt users for the above requirements that may look like: MENU ======= 1. Print all records 2. Print number of records 3. Print size of database 4. Add record 5. Delete record 6. Print number of accesses to database 7. Exit Once you have gathered the appropriate information you will need to manipulate the data to hold the data correctly, but we are not using File I/O to maintain state on the data (would require too much time). Your database is a memory only database, so once your program ends, your database is gone. Being this is the case, it will be important to create a header file that has some data in it (5-7 records), so you will not need to enter all the records every time.

Solutions

Expert Solution

#include<stdio.h>

#include<stdlib.h>

struct Node

{

    int data;

    struct Node* next;

};

void sortedInsert(struct Node** head_ref, struct Node* new_node)

{

    struct Node* current;

    if (*head_ref == NULL || (*head_ref)->data >= new_node->data)

    {

        new_node->next = *head_ref;

        *head_ref = new_node;

    }

    else

    {

        current = *head_ref;

        while (current->next!=NULL &&

               current->next->data < new_node->data)

        {

            current = current->next;

        }

        new_node->next = current->next;

        current->next = new_node;

    }

}

struct Node *newNode(int new_data)

{

    struct Node* new_node =

        (struct Node*) malloc(sizeof(struct Node));

    new_node->data = new_data;

    new_node->next = NULL;

  

    return new_node;

}

void printList(struct Node *head)

{

    struct Node *temp = head;

    while(temp != NULL)

    {

        printf("%d ", temp->data);

        temp = temp->next;

    }

}

int main()

{

    struct Node* head = NULL;

    struct Node *new_node = newNode(5);

    sortedInsert(&head, new_node);

    new_node = newNode(10);

    sortedInsert(&head, new_node);

    new_node = newNode(7);

    sortedInsert(&head, new_node);

    new_node = newNode(3);

    sortedInsert(&head, new_node);

    new_node = newNode(1);

    sortedInsert(&head, new_node);

    new_node = newNode(9);

    sortedInsert(&head, new_node);

    printf("\n Created Linked List\n");

    printList(head);

  

    return 0;

}


Related Solutions

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....
In this assignment you will use pointers and structures to create Single and double link list....
In this assignment you will use pointers and structures to create Single and double link list. Write a menu driven program with the menu options: 1. Insert in Linked List       1. Insert in Single linked list              1. Insert at front(head)              2. Insert at Index              3. Insert at end(tail)       2. Insert in double linked list              1. Insert at front(head)              2. Insert at Index              3. Insert at end(tail) 2. Print       1. Print linked...
4.4 Lab: Arrays of Pointers to Structures (Sort) C++ programming This assignment is based on Homework...
4.4 Lab: Arrays of Pointers to Structures (Sort) C++ programming This assignment is based on Homework 3. The program will create an array of Airport structures. On the first line in the input file airports.txt there is an integer n, representing the number of lines in the input file. On each of the following n lines there is an airport code (a unique identifier) followed by the airport’s number of enplanements (commercial passenger boardings), and city served. You may assume...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
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...
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...
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT