Question

hw3 Data Structure: Please write programs and finish the question in C language: thank you (You can do both 5 & 6 if you know

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

To rotate the Doubly linked list first check whether the given N is greater than the length of the list or not. If N is greater than the size of the list then deduce it in the range of linked list size by taking modulo with the length of the list. After that subtract the value of N from the length of the list. Now the problem reduces to the counter-clockwise rotation of a doubly-linked list by N places.

  • Change the next of the last node to point the Head node.
  • Change the prev of the Head node to point the last node.
  • Change the value of the Head_ref to be the next of the Nth node.
  • Change the value of next of the Nth Node to be NULL.
  • Finally, make the prev of the Head node to point to NULL.
 #include <stdio.h> struct node{ int data; struct node *previous; struct node *next; }; struct node *head, *tail = NULL; int size = 0; void addNode(int data) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = data; if(head == NULL) { head = tail = newNode; head->previous = NULL; tail->next = NULL; } else { tail->next = newNode; newNode->previous = tail; tail = newNode; tail->next = NULL; } size++; } void rotateList(int n) { //Initially, current will point to head struct node *current = head; //n should not be 0 or greater than or equal to number of nodes present in the list if(n == 0 || n >= size) return; else { //Traverse through the list till current point to nth node //after this loop, current will point to nth node for(int i = 1; i < n; i++) current = current->next; //Now to move entire list from head to nth node and add it after tail tail->next = head; //Node next to nth node will be new head head = current->next; //Previous node to head should be NULL head->previous = NULL; //nth node will become new tail of the list tail = current; //tail's next will point to NULL tail->next = NULL; } } void display() { struct node *current = head; if(head == NULL) { printf("List is empty\n"); return; } while(current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { addNode(1); addNode(2); addNode(3); addNode(4); addNode(5); printf("Original List: \n"); display(); rotateList(3); printf("Updated List: \n"); display(); return 0; } 

Original List: 1 2 3 4 5 Updated List: 4 5 1 2 3

If you find the Answer helpful doo, please upvote..

Add a comment
Know the answer?
Add Answer to:
hw3 Data Structure: Please write programs and finish the question in C language: thank you (You...
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
  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • ***Using C++ and also please label "a" and "c" accordingly. Thank you!!**** a) Write the member...

    ***Using C++ and also please label "a" and "c" accordingly. Thank you!!**** a) Write the member function void deleteNode() that will delete the first node from the linked list. b) Modify the LList to create a data member Node *cur that points to the current node. Write the following member functions: • void forward() that moves the cur pointer to the next node. • void backward() that moves the cur pointer to the previous node. • void delete() that removes...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • In this assignment you are to utilize the Node data structure provided on Blackboard. In this...

    In this assignment you are to utilize the Node data structure provided on Blackboard. In this assignmet you are to write a main program that implements two methods and a main method as their driver. So, only main and two methods with it. Note: You may not use any of the LinkedList class provided on Blackboard, you may use the methods in it as a guide (you should actually look at them) but you cannot use any of the methods...

  • Write the pseudo code for function node* list search(node* head ptr, const node::value type& target); where...

    Write the pseudo code for function node* list search(node* head ptr, const node::value type& target); where head ptr is the head pointer of a linked list. The function returns a pointer to the first node containing the specified target in its data field. If there is no such node, the null pointer is returned. You can also use C++ code as you prefer.

  • can you guys please help me with these questions thank you 9.Given a Double Linked-List that...

    can you guys please help me with these questions thank you 9.Given a Double Linked-List that contains the values in order: 1,2,3,4,5; and the following class declarations: class List private: Node* head; public: PrintBackwards (); class Node{ friend class List; private: int value; Node* next; Node* prev; Write the algorithm PrintBackwards that prints the whole list backwards. So your functionshould output: “ 5,4,3,2,1" Extra Credit if you instead implement the algorithm for a single linked-list (i.e. only using the Node*...

  • Enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as a...

    enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as an element of a linked list ** implementation of a stack of tree nodes. */ typedef struct link_t LinkNode; typedef LinkNode *LinkNodePtr; /* The TreeNode type is used as an element of a binary "parse" tree. ** Tree nodes are created while parsing the RPN expression and ** stored on a stack until it's time to place them. */ typedef struct tree_t TreeNode; typedef...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...

    C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...

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