Question

C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...

C++

Goals

 Build single linked lists using pointers

 Learn how to manipulate linked lists

In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when the list is empty.

All of the code manipulating the linked list should be in the main function. For this lab, you will not implement the linked list operations using functions/methods. The idea is to focus on the low-level manipulation of the links that connect the nodes in the list.

1. Create the linked list: store integer values entered by the user in the linked list. Each node in the list should store one value. Use a menu to get user input values. After the user has entered the sequence of values, the program should print the values in order. Think about how to traverse the list in order to print the values. Meanwhile, you need to set the head and tail pointers to point to the first and last node in the list.

2. Get the head or tail node value: print the values of head or tail node in screen. If the list is empty, print a note as “List is empty!!”

3. Delete the head or tail value: ask the user whether to delete the node in the head or tail of the list. When user chooses one, delete that node by manipulate pointers and print the new head/tail node value. If the list is already empty, print a note like “Deleting failed, list is empty!!” Remember to free the memory after deleting.

Finally, give users options to exit the program or do it again.

Example running:

Welcome to my linked list!

Enter a number: 100

Do you want another num(y or n): y

Enter a number: 30

Do you want another num(y or n): y

Enter a number: 50

Do you want another num(y or n): y

Enter a number: 10

Do you want another num(y or n): n

Your linked list is: 100 30 50 10

Do you want to do get head or tail node value (h or t): t

Tail node is: 10

Do you want to delete head or tail node (h or t): h

The new head node is 30!

Do you want to do this again (y or n)? n

Homework Answers

Answer #1

#define SLIST_H;

#include <stdlib.h>

#include <slist.h>

struct snode {

    void * data;

    struct snode * next;

};

typedef struct snode snode;

struct slist {

    snode * head;

    snode * tail;

    unsigned int count;

};

typedef struct slist slist;

typedef void (*slist_forfn)(void *);

slist * slist_create(void);

void slist_empty(slist * list);

void slist_delete(slist * list);

void slist_add_tail(slist * list, void * data);

void slist_add_head(slist * list, void * data);

void * slist_remove_head(slist * list);

void * slist_delete_tail(slist * list);

void * slist_delete(slist *list, snode *node, snode *previous);

static snode * snode_create(void * data)

{

    snode * node = malloc(sizeof(snode));

    if (node) {

        node->data = data;

        node->next = NULL;

    }

return node;

}

slist * slist_create(void)

{

    slist * list = malloc(sizeof(slist));

    if (list) {

        list->head = NULL;

        list->tail = NULL;

        list->count = 0;

    }

return list;

}

void slist_empty(slist * list)

{

    snode * node, * temp;

    node = list->head;

    while (node != NULL) {

        temp = node->next;

        free(node);

        node = temp;

    }

}

void slist_delete(slist * list)

{

    if (list) {

        slist_empty(list);

        free(list);

    }

}

void slist_add_tail(slist * list, void * data)

{

    snode * node = snode_create(data);

    if (list->head == NULL) {

        /* Adding the first node */

        list->head = node;

        list->tail = node;

    }

    else {

        list->tail->next = node;

        list->tail = node;

    }

    list->count++;

}

void slist_add_head(slist * list, void * data)

{

    snode * node = snode_create(data);

    if (list->tail == NULL) {

        /* Adding the first node */

        list->head = node;

        list->tail = node;

    }

    else {

        node->next = list->head;

        list->head = node;

    }

    list->count++;

}

void * slist_remove_head(slist * list)

{

    void *data = NULL;

    if (list->head) {

        snode *temp = list->head;

        if (list->head->next) {

            list->head = list->head->next;

        }

        else {

            /* List is now empty */

            list->head = NULL;

            list->tail = NULL;

        }

        data = temp->data;

        free(temp);

        list->count--;

        if (list->count == 1) {

            list->tail = list->head;

        }

    }

     

    return data;

}

void * slist_remove_tail(slist * list)

{

    void *data = NULL;

    if (list->tail) {

        snode *current, *previous = NULL;

        current = list->head;

        while (current->next) {

            previous = current;

            current = current->next;

        }

        data = current->data;

        free(current);

        if (previous) {

            previous->next = NULL;

        }

        else {

            /* List is now empty */

            list->head = NULL;

            list->tail = NULL;

        }

        list->count--;

        if (list->count == 1) {

            list->head = list->tail;

        }

    }

return data;

}

void * slist_remove(slist *list, snode *node, snode *previous)

{

    void *data;

    if (node == list->head) {

        data = slist_remove_head(list);

    }

    else {

        previous->next = node->next;

        data = node->data;

        list->count--;

        if (list->count == 1) {

            list->tail = list->head;

        }

        else if (node == list->tail) {

            list->tail = previous;

        }

        free(node);

    }

    return data;

int main(void)

{

    slist * list = slist_create();

    char * elements[] = {"A", "B", "C", "D", "E", "F"};

    const unsigned int n = sizeof(elements) / sizeof(const char*);

    unsigned int i;

    snode * node, * previous = NULL;

    unsigned int found = 0;

    /* Populate the list with A, B, ..., F */

    for (i = 0; i < n; i++) {

        slist_add_tail(list, elements[i]);

    }

    /* Insert X and Y either side of C */

    for (node = list->head; node != NULL && !found; node = node->next) {

        if (strcmp((const char*)node->data, "C") == 0) {

            slist_insert_before(list, node, previous, "X");

            slist_insert_after(list, node, "Y");

            found = 1;

        }

        previous = node;

    }

/* Forward iteration */

    for (node = list->head; node != NULL; node = node->next) {

        printf("%s\n", (const char*)node->data);

    }

slist_delete(list);

return 0;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first...
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first node, and each node contains a pointer to the next node, and the pointer in the last node points back to the first node? A. Circular, singly-linked list. B. Circular, doubly-linked list. C. Singly-linked list. D. Doubly-linked list. E. None of the above.    QUESTION 2 _________ is not an advantage of linked lists when compared to arrays. A. Dynamic memory allocation. B. Efficient...
1. Create a linked list using the Node class that contains the first million prime numbers...
1. Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options: 1 = Search for a Number (let the user enter a number to search for) 2 = Add a new Number (let the user enter a number and add it to the head of the list) 3 = Delete a Number (let the user enter a number and delete it if found) 4 = Exit...
in c++ Each of the member functions in WrongCode.cpp has errors in the way it performs...
in c++ Each of the member functions in WrongCode.cpp has errors in the way it performs a linked list operation. Find as many mistakes as you can //--------------------one void NumberList::appendNode(double num) { ListNode *newNode, *nodePtr; // Allocate a new node & store num newNode = new listNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node. if (!head) head = newNode; else // Otherwise, insert newNode. { // Find the last...
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does not have tail? Write the code. How much time does it take to Do it?
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does no have tail? Write the code. How much time does it take to do it? (java)
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT