Question
I need this in C programing
And please give me complete code with screenshot of your output.



onded reverze. e le defnes α node stricture identical to the list nodes for the cycle detection probem in Lab4 3. Cycle le re
0 0
Add a comment Improve this question Transcribed image text
Answer #1
The main idea to detect a cycle is to take two pointer one running fast and other is slow so if there is a cycle then fastpointer and slowpointer will to same node at some point if yes then cycle is present remove it otherwise no cycle present
#include<stdio.h>

//struct for linked list
struct node
{
    int data;
    struct node* next;
};


struct node * break_cycle(struct node *head)
{
    struct node *slowpointer = head, *fastpointer = head;
    
    while (slowpointer && fastpointer && fastpointer->next)
    {
        slowpointer = slowpointer->next; //move one pointer one step further 
        fastpointer = fastpointer->next->next; // second pointer two step
                
        //if both comes to same node means there is a cycle then call remove cycle and break the loop and return the head        
        if (slowpointer == fastpointer)
        {
            removecycle(slowpointer, head);
            break;
            
        }
        return head;
    }
}

void removecycle(struct node *loop_node, struct node *head) {
    struct node *ptr1;
    struct node *ptr2;

/* Set a pointer to the begining of the Linked List and move it one by one to find the first node which is part of the Linked List */
    ptr1 = head;
    while (1) {
        /* Now start a pointer from loop_node and check if it ever reaches ptr2 */
        ptr2 = loop_node;
        while (ptr2->next != loop_node && ptr2->next != ptr1)
            ptr2 = ptr2->next;

        /* If ptr2 reached ptr1 then there is a loop. So break the loop */
        if (ptr2->next == ptr1)
            break;

        /* If ptr2 did't reach ptr1 then try the next node after ptr1 */
        ptr1 = ptr1->next;
    }
}

Reverse linkedlist

#include<stdio.h>

//struct for linked list
struct node
{
    int data;
    struct node* next;
};


struct node * reverse(struct node * head){
    //we take two pointers and store one with the previous value and the one with next value
    struct node * previous = NULL, *next = NULL;

    while (head != NULL){
        //loop untill we reach last node we save next node of current node in next and point the current node next
        // to previous node value and then previous point to current node and current node becomes next node
        next = head->next;
        head->next = previous;
        previous = head;
        head = next;
    }

    return previous;
}

Add a comment
Know the answer?
Add Answer to:
I need this in C programing And please give me complete code with screenshot of your...
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
  • Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up...

    Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up for helping. True/False (13) Chapter 14 - A List Implementation that Links Data Adding a node to an empty chain is the same as adding a node to the beginning of a chain. Adding a node at the end of a chain of n nodes is the same as adding a node at position n. You need a temporary variable to reference nodes as...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or...

    RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or a fail. Its very imortant that this and the other questions posted are answered 100% Thank you. 13 - Template C++ Advance Please Only answer assignment if your code is 100%. When pasteing your code use text so I may copy and paste into visual studio. The code and questions must be answered 100% correct and works. Thank you. Programming Assignment Convert the int...

  • need c++ format need SkipListNode.h & .cpp and main.cpp ********************************************************** 1.3 The Skip List's Node Class...

    need c++ format need SkipListNode.h & .cpp and main.cpp ********************************************************** 1.3 The Skip List's Node Class Each node of a skip list must be capable of pointing to multiple sucessors in the skip list structure. This could be accomplished with an array of pointers of fixed size, but this is wasteful in terms of storage since the size would have to be large enough to accommodate the biggest possible skip list. It is more efficient to the skip list itself...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • Please use C programming to write the code to solve the following problem. Also, please use the i...

    Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...

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