Question

In: Computer Science

Part 2- - Create and display a singly Linked List with 5 elements (see below)                 Node*...

Part 2- - Create and display a singly Linked List with 5 elements (see below)

                Node* head

                 Node*second

                 Node*third

                Node*forth

                 Node*fifth

2-Assign the data 5, 6, 8, 10, 12 to each node 3-Display the output

GIVEN CODE:

#include <studio.h>

struct Array { int A[10]; int size; int length; };

void Display(struct Array arr)

{

     int i; printf("\nElements are\n");

     for(i=0;ilengthsize) arr->A[arr->length++]=x;

}

void Insert(struct Array *arr,int index,int x)

{

     int i;

         if(index>=0 && index <=arr->length)

{

     for(i=arr->length;i>index;i--)

     arr->A[i]=arr->A[i-1]; arr->A[index]=x; arr->length++;

}

}

int main()

{

struct Array arr1={{2,3,4,5,6},10,5};

Append(&arr1,10);

Insert(&arr1,0,12);

Display(arr1);

return 0;

}

Solutions

Expert Solution

Linked List Question (Part -2)code:

#include<iostream>
using namespace std;

class node{
    public:
    int data;
    node* next;
    node(int x){
        this->data=x;
        this->next=NULL;
    }
};

int main(){
    node* head=NULL;

    head= new node(5);
    head->next= new node(6);
    head->next->next= new node(8);
    head->next->next->next= new node(10);
    head->next->next->next->next= new node(12);
    
    cout<<"the linked list is: ";
    node* temp=head;

    while(temp){
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    
}

Screenshot and Output:

2)

Array Question (Modified as per need. Kindly comment if you find any mistake. I'll code it for you)

Code:

#include <stdio.h>

struct Array {
     int A[10];
     int size;
     int length;   
};

void Display(struct Array arr){
     int i;
     printf("\nElements are\n");
     for(i=0;i<arr.length;i++)
     printf("%d ", arr.A[i]);

}

void Append(struct Array *arr, int x){
    int i;
    arr->A[arr->length++]=x;
}

void Insert(struct Array *arr,int index,int x){
    int i;
    if(index>=0 && index <=arr->length){
        for(i=arr->length-1;i>index;i--)
            arr->A[i]=arr->A[i-1];
        arr->A[index]=x;
        arr->length++;
    }
}

int main(){
    
struct Array arr1={{2,3,4,5,6},10,5};
Display(arr1);
Append(&arr1,10);
Display(arr1);
Insert(&arr1,0,12);
Display(arr1);
return 0;
}

Code Screenshot and Output:

Please comment in case of doubts or queries.

Kindly upvote if you found this helpful :)


Related Solutions

Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The...
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The school has multiple classes. Each class has a different number of students. class student { Long ID; string Name; string Address; float grades[3]; student *below; }; class Node // the node represents a class in school { int ID; int NoOfStudents; int NoOfQuizes; student *t;// a linked list of students is allocated dynamically Node *Next; }; class school { string Name; Node *Head; int...
Find the last node of a linked list of n elements whose index is a multiple...
Find the last node of a linked list of n elements whose index is a multiple of k (counted from 0). For example, if the list is 12 → 75 → 37 → 99 → 12 → 38 → 99 → 60 ↓ and k = 4, then you should return the second 12 (with index 4). Your algorithm should take O(n) time and use O(1) extra space. Implement the following method in LinkedList.java. public T lastK(int k) LinkedList.java. public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT