In: Computer Science
PLEASE INCLUDE THE SOURCE CODE AND OUTPUT PLEASE AND
THANKS!!This assignment covers recursion and linked list which
include the following tasks:
2. a. Using C/C++, construct a single linked list of 8 nodes and assign random numbers as the nodes’ values. Then print the list from the first node to the last. Finally, free all memories of the linked list.
b. Using C/C++, construct a single linked list of 8 nodes and assign random numbers as the nodes’ values. Then create a new node and assign its value 100; insert this node at the sixth position of the list, and define a recursive function to print the list to verify the result. After that, delete the eighth node of the list to keep the linked list having 8 nodes, and define another recursive function to reprint the linked list backwards (from the last to the first).
//File libsll.h
#ifndef sll_lib_h
#define sll_lib_h
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int value;
struct node * next;
}node;
node * newnode(int );
node * create(int );
node * add_beg(node*, int);
node * add_end(node*, int);
node * del_first(node*);
node * del_last(node*);
int size(node*);
int loc_check(node*,int);
node * add_loc(node*,int,int);
node * add_locx(node*,int,int);
node * del_loc(node*,int);
node * del_locx(node*,int);
void display(node *);
node * newnode(int val){
node * new = (node *)malloc(sizeof (node));
new->value=val;
new->next=NULL;
return new;
}
node * create (int val){
node * start = newnode(val);
return start;
}
node * add_beg(node * head, int val){
node * new = newnode(val);
new->next = head;
head = new;
return head;
}
node * add_end(node * head, int val){
if(head->next==NULL){
node * new = newnode(val);
head->next=new;
}
else
head->next=add_end(head->next,val);
return head;
}
node * del_first(node * head){
if(head==NULL)
printf("Empty List\n");
else
head=head->next;
return head;
}
node * del_last(node * head){
if(head==NULL)
printf("Empty List\n");
else if(head->next==NULL)
head=NULL;
else
head->next = del_last(head->next);
return head;
}
int size(node * head){
int sz=0;
if(head==NULL)
return sz;
else
sz = 1 + (size(head->next));
return sz;
}
int loc_check(node * head, int loc){
if(loc>1||loc<=size(head))
return 1;
else
return 0;
}
node * add_locx(node * head, int loc, int val){
if(loc==1)
head=add_beg(head,val);
else
head->next=add_locx(head->next, loc-1, val);
return head;
}
node * add_loc(node * head,int loc,int val){
if (loc_check(head,loc)==0){
printf("Invalid Location\n");
return head;
}
else
return (add_locx(head, loc, val));
}
node * del_locx(node * head, int loc){
if(loc==1)
head=del_first(head);
else
head->next=del_locx(head->next, loc-1);
return head;
}
node * del_loc(node * head, int loc){
if(loc_check(head,loc)==1)
return del_locx(head, loc);
else{
printf("Invalid Location\n");
return head;
}
}
void display(node * head){
if(head==NULL)
printf("Empty List\n");
else if(head->next!=NULL){
printf("|%d| -> ",head->value);
display(head->next);
}
else
printf("|%d|\n",head->value);
}
void display_reverse(node * head){
if(head==NULL)
printf("Empty List\n");
else if(head->next!=NULL){
display_reverse(head->next);
printf(" <- |%d|",head->value);
}
else
printf("|%d|",head->value);
}
void free_list(node * head){
if(head==NULL)
return;
free_list(head->next);
free(head);
return;
}
#endif
2.a.
#include<stdio.h>
#include "libsll.h"
int main(){
node * n;
n=create(51);//first node
n=add_beg(n,6);//adding nodes at beginning
n=add_beg(n,18);
n=add_end(n,40);//adding nodes at end
n=add_end(n,24);
n=add_loc(n,2,8);//adding nodes at index
n=add_loc(n,4,37);
n=add_loc(n,8,83);
display(n);
free_list(n);
return 0;
}
//Output: |18| -> |8| -> |6| -> |37| -> |51| -> |40| -> |24| -> |83|
//2.b.
#include<stdio.h>
#include "libsll.h"
int main(){
node * n;
n=create(51);
n=add_beg(n,6);
n=add_beg(n,18);
n=add_end(n,40);
n=add_end(n,24);
n=add_loc(n,2,8);
n=add_loc(n,4,37);
n=add_loc(n,8,83);
n=add_loc(n,6,100); // adding 100 at sixth index
display_reverse(n);
return 0;
}
//Output: |83| <- |24| <- |40| <- |100| <- |51| <- |37| <- |6| <- |8| <- |18|
Thanks and please upvote if it helped