0% found this document useful (0 votes)
13 views4 pages

SLL Rev

The document contains a C program that implements a singly linked list with functions to reverse the list both recursively and iteratively. It includes functions to push new nodes, print the list, and a main driver program to test these functionalities. The program prompts the user to input values to create the list and displays the original and reversed lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

SLL Rev

The document contains a C program that implements a singly linked list with functions to reverse the list both recursively and iteratively. It includes functions to push new nodes, print the list, and a main driver program to test these functionalities. The program prompts the user to input values to create the list and displays the original and reversed lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>

#include<stdlib.h>

/* Link list node */

struct Node

int data;

struct Node* next;

};

struct Node *temp,*head=NULL;

/* Function to reverse the linked list using recursion */

void recursiveReverse(struct Node *head)

if(head==NULL)

return;

recursiveReverse(head->next);

printf("%d\t",head->data);

/* Function to reverse the linked list using iteration */

void reverse()

struct Node* prev = NULL;

struct Node* current = head;

struct Node* next;

while (current != NULL)

next = current->next;

current->next = prev;
prev = current;

current = next;

head= prev;

/* Function to push a node */

void push(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 of the new node */

new_node->next =NULL;

if(head==NULL)

/* move the head to point to the new node */

head = temp= new_node;

else

temp->next=new_node;

temp=new_node;

/* Function to print linked list */


void printList()

struct Node *temp = head;

while(temp != NULL)

printf("%d ", temp->data);

temp = temp->next;

/* Driver program to test above function*/

void main()

int i,n,key;

clrscr();

printf("Enter Howmany values you want to insert in a list");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("Enter a number : ");

scanf("%d",&key);

push(key);

printf("Given linked list\n");

printList();

printf("\n Reversed Linked list using recursion \n");

recursiveReverse(head);

reverse();

printf("\nReversed Linked list using non-recursion \n");

printList();

getch();
}

You might also like