Question

C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists...

  1. C# code Arrays and Linked Lists:
  1. Write C++/Java/C#/Python code to declare an array of linked lists of any primitive type you want. (Array of size 2020) (This could be based on MSDN libraries or the lab) – you do not need to instantiate any of the linked lists to contain any actual values.

Paste your code for that here (this should only be one line)

  1. Based on your code or the lab from 4 or your doubly linked list from assignment 2, write the following methods:
    1. A method to rotate the list by n (where n can be a positive or negative integer), +ve values rotate to the right, -ve values rotate to the left. If this list contains 1 or 0 elements it should do nothing.

Paste your code for that here

  1. A method called “secondSwap” which swaps the second element in the list with the second last. (E.g. {A, B, C, D, E, F} - > {A, E, C, D, B, F}. On a list with less than 3 elements it can do nothing.

Paste your code for that here

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a.) Struct Node {int value; Node* Next }; Node * arptr[2020];

b.) i)

void rotate_by_n(Node** head, int n)
{

Node* curr = *head;
Node* temp = *head;
int length = 1;//store number of nodes
    while (temp->next != NULL) {
        temp = temp->next;
        length++;
    }
if(curr->next==NULL||curr==NULL) //if 0 or 1 node in list
{
      return;
}

    if (n == 0)
        return;
    if(n<0) //for left rotation
    {
    n=-(n);
    if (n > length)
    {
        n = n % length; //if n>count of nodes then we take modulus
    }

    Node* current1 = *head;

    int count1 = 1;
    while (count1 < n && current1 != NULL) {
        current1 = current1->next; // current1 will point to nth node after this loop
        count1++;
    }

    if (current1 == NULL)
        return;


    Node* nthnode = current1;


    while (current1 ->next != NULL)
        current1 = current1->next;

    current1->next = *head; // change to head

    *head = nthnode->next; //change head to (nth+1) node

    nthnode->next = NULL;

    }

    else if(n>0)
    {

    if (n > length)
        n= n%length;

    n= length-n;

    if (n==0||n==length)
        return;

    Node* current1 = *head;
    int count2= 1;
    while (count2 < n && current1 != NULL) {
        current1 = current1->next;
        count2++;
    }

    if (current1 == NULL)
        return ;

    Node* nthnode = current1;
    temp->next = *head;


    *head = nthnode->next;
    nthnode->next = NULL;

    }
}

b) ii)


void secondSwap(Node * head)
{
    Node * temp1=head,*temp2=head->next,*temp3=head,*curr=head;

    while(curr->next->next!=NULL)
    {
        temp3=curr;
        curr=curr->next;

    }
    if (temp1!= NULL)
    temp1->next = curr;
    else
    {
      head = curr;
    }

if (temp3 != NULL)
    temp3->next = temp2;
else
{
    head = temp2;
}
  

Node *temp = curr->next;
curr->next = temp2->next;
temp2->next = temp;

}

void secondSwap(Node ** head)

Add a comment
Know the answer?
Add Answer to:
C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists...
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
  • Array lists and linked lists are both implementations of lists. Write a program in java of...

    Array lists and linked lists are both implementations of lists. Write a program in java of a situation where an array list would be the better choice and one program where a linked list would. Explain the reasons in each case.

  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of...

    Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of first list in Linked List#1, and numbers of second list in Linked List#2. Do not insert both lists in a single Linked List. List#1. 5, 78, 45, 23, 11, 89, 10, 78, 6, 99, 876, 5, 67, 13 List#2. 5, 89, 688, 52, 557, 953, 5, 7, 55, 35, 89, 99, 99, 6, 557, 89, 5, 99, 6, 2, 45, 12, 7, 6, 94,...

  • Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying...

    Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...

  • Code in C only Need to write a function that when passed a linked list swaps...

    Code in C only Need to write a function that when passed a linked list swaps the second and third nodes. if there are only two nodes then swap them, if one or less nothing happens. and include a drawing showing how it is processed.

  • java programming!!! Write the code fragment that will do the following: • Ask the user how...

    java programming!!! Write the code fragment that will do the following: • Ask the user how many numbers they want to enter • Declare and instantiate an array of doubles with as many elements as the number entered by the user • Write one 'for' loop to fill the array with data from the user • Write a second 'for' loop to calculate the sum of all of the numbers in the array • Print the calculated sum Note: Write...

  • 117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class....

    117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a linked list that will hold values of type int. It should use an IntNode class to implement the nodes in the linked list. The linked list must have the following methods: . A constructor with no parameters which creates an empty list. . void add (int data) -adds data to the front of the list. .A constructor IntlinkedList(int[]) which will...

  • How do you insert an array of linked lists(L) as an argument of a function (in...

    How do you insert an array of linked lists(L) as an argument of a function (in pseudocode)? For example I already have an array of linked lists (L) where each element of this array is the head/ or start of a new linked list. I then want to traverse each list in the algorithm e.g. FindMean(how to represent the list here?) { here I want to visit the first node of each list in turn, then all the second nodes...

  • Write Java code (do not use Java libraries) to perform the following on a linked list...

    Write Java code (do not use Java libraries) to perform the following on a linked list of integers: prepend a new value

  • Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using...

    Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using only a constant amount of additional storage and no recursion. Assume the list object has a head pointer _head and consists of Node objects; a Node object has a pointer Node* _next

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