Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Structure and Algorithms #include #include struct node; typedef struct node

ID: 3700738 • Letter: D

Question

Data Structure and Algorithms

#include #include struct node; typedef struct node Node; struct node int data; Node* next; //declare your function here. int main(int argc, char* argv[]) Node* head = NULL; int i; Node* temp; //set up a test list with values 9->8->7->...->0 for (i 0; i data - i; temp->next - head; head - temp; //call your function to reverse the list (should work for any list given the head node) //print the reversed list. temp = head; while (temp ?= NULL) printf("%d ", temp->data); temp temp->next; return 0; Define your recursive function here (1) Identify and write the base case or cases (2) Recursive condition. (3) write out the code

Explanation / Answer

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

struct node;
typedef struct node Node;

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

Node* reverseLL(Node* head)
{
if(head == NULL)
return;
if(head->next == NULL)
return head;
Node* temp = reverseLL(head->next);
head->next->next = head;
head->next = NULL;
return temp;
}

int main(int argc, char* argv[])
{
Node* head = NULL;
int i;
Node* temp;
for(i = 0; i < 10; i++)
{
temp = (Node*)malloc(sizeof(Node));
if(temp == NULL)
{
printf("out of memory? ");
exit(1);
}
temp->data = i;
temp->next = head;
head = temp;
}
head = reverseLL(head);
temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
return 0;
}