Question

Below are the node_struct, link, list_struct and list as defined in class and in hw3: typedef...

Below are the node_struct, link, list_struct and list as defined in class and in hw3: typedef struct node_struct * link;

struct node_struct {

int item;

link next;

};

typedef struct list_struct * list;

struct list_struct {

link first;

int length;

};

a) (15 pts) Write a function that swaps the first and last node in a list. If there are not enough nodes, it does not do anything to the list. You have to update the links yourself. DO NOT call any insert or remove functions to do any of the work for you.
void swapFirstLast(list L) {
b) (8 pts) Make a drawing of a list with 5 (five) nodes and show how the links are updated as done in class. Label each arrow update with the line number that did it.
c) (3 pts) What is the Θ complexity of your function? Justify your answer. (You can show to the side of the code)
d) (4 pts) If your function fails or crashes for certain inputs, give those inputs and clearly indicate the line with the problem (use line numbers or write the test cases as a comment on that line of code).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
void swapFirstLast(list L) {
    // No swaps required on less than length 2.
    if(L == NULL || L->length <= 1) {
        return;
    }

    // special case for length of 2 nodes.
    if(L->length == 2) {
        link ourfirst = L->first;
        link oursecond = ourfirst->next;

        ourfirst->next = NULL;
        oursecond->next = ourfirst;

        // make the last node, as first
        L->first = oursecond;
    } else {
        link ourfirst = L->first;
        link ourlastPrev = L->first;
        link ourlast = L->first->next;

        while(ourlast->next != NULL) {
            ourlastPrev = ourlast;
            ourlast = ourlast->next;
        }
        L->first = ourlastPrev->next;
        L->first->next = ourfirst->next;
        ourlastPrev->next = ourfirst;       
        ourfirst->next = NULL;
    }
}

Taking directly case 3, with length more than 2 nodes, as other cases seem trivial compared to this..

So on start of line 18, the list looks like below:

From line 18 to line 25, we are trying to find the first node, last and seconlast node.. After line25, the markers will be adjusted as below:

After line 27:

After line 28:


After line 29:

After line 30:

The complexity is O(N) for N nodes, as we need to find the last node to swap. Also, code does not crash for any input.

Add a comment
Know the answer?
Add Answer to:
Below are the node_struct, link, list_struct and list as defined in class and in hw3: typedef...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

  • 2) (10 pts) Write a function that takes in a pointer to a linked list of...

    2) (10 pts) Write a function that takes in a pointer to a linked list of nodes storing integers and a variable named value, and returns the number of nodes in the list storing that value. For example, if a list pointed to by listPtr stores 2, 6, 2, 3, 4, 2, 6, and 6 and value = 6, your function should return 3, since 6 appears in the list 3 times. Please use the struct and function prototype provided...

  • 16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below,...

    16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below, write expressions that do the following, assuming that currPtr is somewhere in the middle of the list: a. Access the component member of the first list element. b. Advance currPtr to point to the next element. c. Access the component member of the next element (the one that follows the current element). d. Access the component member of the element that follows the next...

  • You are now going to create a LinkedList class, that will work very similarly to the...

    You are now going to create a LinkedList class, that will work very similarly to the Stack class seen in the book (and used in the previous exercise). Then write new methods as follows: add ( LinkedList::Link* l, int n ): will insert in the linked list, after link l, a chain of n new links. Each link will store an integer that will be set to receive, in order, a value starting from 0 until n-1. In the last...

  • C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct...

    C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct nodet *makeAnode(int val) { struct nodet *box; box = malloc(sizeof(struct nodet) ); box->data = val; box->link = NULL; return box; } void printList(struct nodet *L) { struct nodet = *mov; mov = L; while(mov != NULL) { printf("%d ", mov->data); mov = mov->link; } printf("\n"); } // THIS SHOULD COUNT HOW MANY ITEMS (NODES) ARE IN THE LIST. int listLen(struct nodet **L) { int...

  • The program defined in insertionSort.cpp gets a list of numbers as input from the user and...

    The program defined in insertionSort.cpp gets a list of numbers as input from the user and calls the insertion sort algorithm to sort them. It also displays the list before and after the sorting. The insertion sort algorithm should be defined in the InsertionSort.h file, but this definition has been omitted and it is your task to provide it. The appropriate insertion sort function is to be defined in the InsertionSort.h file. The InsertionSort.h file performs an include of Swap.h,...

  • Please help ASAP! C ++, linked list, etc. everything for the project is in the link...

    Please help ASAP! C ++, linked list, etc. everything for the project is in the link below. https://www.zipshare.com/download/eyJhcmNoaXZlSWQiOiIzZDIyN2UzYi1kNGFhLTRlMzEtYjMzZi00MDhmYzNiMjk3ZGMiLCJlbWFpbCI6Im1pMTQzNEBnbWFpbC5jb20ifQ== You should turn it in by blackboard. Each task should be in a separate unzipped folder named YourLastName_YourFirstInitial_taskN, where N is the task number inside a zipped file named: YourLastName_YourFirstInitial.zip. You may use code you have written previously and you may ask other members of the class questions about the assignment. However, you must do your own assignment; you may not use...

  • python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item,...

    python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34,1, 23, 7, and 10. Display it. Then...

  • Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...

    Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...

  • Using Python. 3) Create a single-linked list (StudentList) where the data in the link is an object of type pointer to Student as defined below. Note that you need to store and pass a pointer of type...

    Using Python. 3) Create a single-linked list (StudentList) where the data in the link is an object of type pointer to Student as defined below. Note that you need to store and pass a pointer of type Student so that you do not create duplicate objects. Test your list with the provided program. You will find example code for a single-linked list of integers in Moodle that you can base your solution on. For your find methods, you can either...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT