Question

In: Computer Science

PLEASE INCLUDE THE SOURCE CODE AND OUTPUT PLEASE AND THANKS!!This assignment covers recursion and linked list...

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).

Solutions

Expert Solution

//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


Related Solutions

Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that integer squared, ending with newline. (Note: This assignment is configured to have students programming directly in the zyBook. Instructors may instead require students to upload a file). Below is a program that's been nearly completed for you. Click "Run program". The output is wrong. Sometimes a program lacking input will produce wrong output (as in this case), or no output. Remember to always pre-enter...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h>...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h> using namespace std; struct ListNode { int data; ListNode *next; ListNode(int x) : data(x), next(nullptr) {} }; class LinkedList { private: ListNode *head = nullptr; public: void addNode(int x) { ListNode *p = new ListNode(x); if (head == nullptr) head = p; else { ListNode *q = head; while (q->next != nullptr) q = q->next; q->next = p; } } void display() { ListNode...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as...
please use linux or unix to complete, and include pictures of the output. Modify the code...
please use linux or unix to complete, and include pictures of the output. Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the...
Linked List: Complete the following code to create a linked list from an Array. After creating...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT