0% found this document useful (0 votes)
25 views2 pages

reversing A Linked List:: Homework 3 Name: Buya MD Habib Hasan Shakil ID: 201932130119

The document contains C code to reverse a linked list. It defines a Node struct with data and next pointer fields. The reverse function takes the head pointer as a parameter, iterates through the list changing next pointers and prev pointers, and returns the new head. The push function adds nodes to the front of the list. The printList function prints the list data. Main gets input, pushes to the initial empty list, prints it, then calls reverse and prints again.

Uploaded by

Md Ashiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

reversing A Linked List:: Homework 3 Name: Buya MD Habib Hasan Shakil ID: 201932130119

The document contains C code to reverse a linked list. It defines a Node struct with data and next pointer fields. The reverse function takes the head pointer as a parameter, iterates through the list changing next pointers and prev pointers, and returns the new head. The push function adds nodes to the front of the list. The printList function prints the list data. Main gets input, pushes to the initial empty list, prints it, then calls reverse and prints again.

Uploaded by

Md Ashiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Homework 3

Name: Buya Md Habib Hasan Shakil


ID: 201932130119

//Reversing a linked list:

#include<stdio.h>
#include<stdlib.h>

/* Link list node */


struct Node
{
int data;
struct Node* next;
};

/* Function to reverse the linked list */


static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}

/* 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 *head)
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}

/* Driver program to test above function*/


int main()
{
/* Start with the empty list */
struct Node* head = NULL;

int n, temp;

scanf("%d", &n);

for(int i=0; i<n; i++){


scanf("%d", &temp);
push(&head, temp);
}

printList(head);
reverse(&head);
getchar();
}

for better view of code: https://paste.ubuntu.com/p/RpyjXQ79xJ/

You might also like