Question
Pull out question 8 on Exercises.
See also: Program 4.16 invert (), Program 4.4 printList ()
Configuring -main ()
1. Create a linked list
2. Function call of 8 (a) and (b)
3. Check the accuracy of the output of r list and l list
4. Repeat steps 1-3 above

void printlist(listPointer first) printf (The list contains: ) for (; first; first = first→link) printf (%4d, first-data)
Write a function to move r to the left n nodes from any given position (l, (b)
as listPointer invert (listPointer lead) /invert the list pointed to by lead / listPointer middle,trail; middle-NULL while (l

Please use c language
void printlist(listPointer first) printf ("The list contains: ) for (; first; first = first→link) printf ("%4d", first-"data); printf("In") Program 4.4: Printing a list the nodes are also in this order. Following the merge, x and y do not exist as indi- vidual lists. Each node initially in x or y is now in z. No additional nodes may be used. What is the time complexity of your algorithm? Let list,-(XI, X2, . . . , xn) and list 2-01, уг, . . . , ym). Write a function to merge the two lists together to obtain the linked list, list 7. ,y22 Xm) İfrn > n. 8. S It is possible to traverse a linked list in both directions (i.e., left to right and res- tricted right-to-left) by reversing the links during the left-to-right traversal. A pos- sible configuration for the list under this scheme is given in Figure 4.10. The vari- able r points to the node currently being examined and I to the node on its left. Note that all nodes to the left of r have their links reversed. (a) Write a function to move r to the right n nodes from a given position (1, r)
Write a function to move r to the left n nodes from any given position (l, (b)
as listPointer invert (listPointer lead) /invert the list pointed to by lead / listPointer middle,trail; middle-NULL while (lead) t resent trail = middle; middle lead; lead lead-ilink; middle-link trail; return middle; poit Program 4.16: Inverting a singly linked list
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>
  
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
  
// This function rotates a linked list counter-clockwise and
// updates the head. The function assumes that k is smaller
// than size of linked list. It doesn't modify the list if
// k is greater than or equal to size
void rotate(struct Node **head_ref, int k)
{
if (k == 0)
return;
  
// Let us understand the below code for example k = 4 and
// list = 10->20->30->40->50->60.
struct Node* current = *head_ref;
  
// current will either point to kth or NULL after this loop.
// current will point to node 40 in the above example
int count = 1;
while (count < k && current != NULL)
{
current = current->next;
count++;
}
  
// If current is NULL, k is greater than or equal to count
// of nodes in linked list. Don't change the list in this case
if (current == NULL)
return;
  
// current points to kth node. Store it in a variable.
// kthNode points to node 40 in the above example
struct Node *kthNode = current;
  
// current will point to last node after this loop
// current will point to node 60 in the above example
while (current->next != NULL)
current = current->next;
  
// Change next of last node to previous head
// Next of 60 is now changed to node 10
current->next = *head_ref;
  
// Change head to (k+1)th node
// head is now changed to node 50
*head_ref = kthNode->next;
  
// change next of kth node to NULL
// next of 40 is now NULL
kthNode->next = NULL;
}
  
/* UTILITY FUNCTIONS */
/* Function to push a node */
void push (struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
  
/* put in the data */
new_node->data = new_data;
  
/* link the old list off the new node */
new_node->next = (*head_ref);
  
/* move the head to point to the new node */
(*head_ref) = new_node;
}
  
/* Function to print linked list */
void printList(struct Node *node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
  
/* Drier program to test above function*/
int main(void)
{
/* Start with the empty list */
struct Node* head = NULL;
  
// create a list 10->20->30->40->50->60
for (int i = 60; i > 0; i -= 10)
push(&head, i);
  
printf("Given linked list \n");
printList(head);
rotate(&head, 4);
  
printf("\nRotated Linked list \n");
printList(head);
  
return (0);
}

Add a comment
Know the answer?
Add Answer to:
Pull out question 8 on Exercises. See also: Program 4.16 invert (), Program 4.4 printList () Configuring -main () 1. Create a linked list 2. Function call of 8 (a) and (b) 3. Check the accuracy of th...
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
  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

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