In: Computer Science
Linked List:
Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively.
Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
}*first=NULL;
void create(int A[], int n)
{
for(i=1; i<n; i++)
{
}
}
void Display(struct Node*p)
{
while(p!=NULL)
{
}
}
void RDisplayTailRecursion (struct Node*p)
{
if(p!=NULL)
{
}
}
void RDisplayHeadRecursion (struct Node*p)
{
if(p!=NULL)
{
}
}
int main()
{
struct Node *temp;
int A[] = {3,5,7,10,25,8,32,2};
create(A,8);
Display(first);
printf("\n");
RDisplayTailRecursion(first);
RDisplayTailRecursion(first);
return 0;
}
If you have any doubts, please give me comment...
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
} *first = NULL;
void create(int A[], int n)
{
int i;
for (i = 1; i < n; i++)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = A[i];
newNode->next = NULL;
if(first==NULL){
first = newNode;
}
else{
struct Node *temp = first;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next = newNode;
}
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void RDisplayTailRecursion(struct Node *p)
{
if (p != NULL)
{
printf("%d ", p->data);
RDisplayTailRecursion(p->next);
}
}
void RDisplayHeadRecursion(struct Node *p)
{
if (p != NULL)
{
RDisplayHeadRecursion(p->next);
printf("%d ", p->data);
}
}
int main()
{
struct Node *temp;
int A[] = {3, 5, 7, 10, 25, 8, 32, 2};
create(A, 8);
Display(first);
printf("\n");
RDisplayHeadRecursion(first);
printf("\n");
RDisplayTailRecursion(first);
printf("\n");
return 0;
}