|
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).
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.
Below are the node_struct, link, list_struct and list as defined in class and in hw3: typedef...
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 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, 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 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 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 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 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...
You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...
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...
C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...