Question

In: Computer Science

1. An array has an index of [5] at the starting address of 200. It has...

1. An array has an index of [5] at the starting address of 200. It has 3 words per memory cell, determine loc[3],loc[4] and NE. (3 Marks: 1 mark for each)

2. A 2-D array defined as A[10 , 5] requires 4 words of storage space for each element. Calculate the address of A[4,3] given the base address as 250 • If the array is stored in Row-major form • If the array is stored in Column-major form

3. Write a method for the following using the data structure Linked List. void append(Node list1, Node list2) If the list1 is {22, 33, 44, 55} and list2 is {66, 77, 88, 99} then append(list1, list2) will change list1 to {22, 33, 44, 55, 66, 77, 88, 99}. (5 Marks: 1mark for each correct step)

4. Write a method for the following using the data structure Linked List. int sum(Node list) If the list is {25, 45, 65, 85} then sum(list) will return 220. (5 Marks: 1mark for each correct step)

5. Trace the following code showing the contents of the Linked List L after each call L.add(50); L.add(60); L.addFirst(10); L.addLast(100); L.set(1, 20); L.remove(1); L.remove(2); L.removeFirst(); L.removeLast(); (10 Marks: 1 mark for each correct answer) L.addFirst(10);

6. Compare and contrast the following data structures. • Array and Linked List

Solutions

Expert Solution

#include <iostream>

using namespace std;

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

// only for the 1st Node
void initNode(struct Node *head,int n){
head->data = n;
head->next =NULL;
}

// apending
void addNode(struct Node *head, int n) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;

Node *cur = head;
while(cur) {
if(cur->next == NULL) {
cur->next = newNode;
return;
}
cur = cur->next;
}
}

void insertFront(struct Node **head, int n) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = *head;
*head = newNode;
}

struct Node *searchNode(struct Node *head, int n) {
Node *cur = head;
while(cur) {
if(cur->data == n) return cur;
cur = cur->next;
}
cout << "No Node " << n << " in list.\n";
}

bool deleteNode(struct Node **head, Node *ptrDel) {
Node *cur = *head;
if(ptrDel == *head) {
*head = cur->next;
delete ptrDel;
return true;
}
  
while(cur) {
if(cur->next == ptrDel) {
cur->next = ptrDel->next;
delete ptrDel;
return true;
}
cur = cur->next;
}
return false;
}

/* reverse the list */
struct Node* reverse(struct Node** head)
{
Node *parent = *head;
Node *me = parent->next;
Node *child = me->next;

/* make parent as tail */
parent->next = NULL;
while(child) {
me->next = parent;
parent = me;
me = child;
child = child->next;
}
me->next = parent;
*head = me;
return *head;
}

/* Creating a copy of a linked list */
void copyLinkedList(struct Node *node, struct Node **pNew)
{
if(node != NULL) {
*pNew = new Node;
(*pNew)->data = node->data;
(*pNew)->next = NULL;
copyLinkedList(node->next, &((*pNew)->next));
}
}

/* Compare two linked list */
/* return value: same(1), different(0) */
int compareLinkedList(struct Node *node1, struct Node *node2)
{
static int flag;

/* both lists are NULL */
if(node1 == NULL && node2 == NULL) {
flag = 1;
}
else {
if(node1 == NULL || node2 == NULL)
flag = 0;
else if(node1->data != node2->data)
flag = 0;
else
compareLinkedList(node1->next, node2->next);
}

return flag;
}

void deleteLinkedList(struct Node **node)
{
struct Node *tmpNode;
while(*node) {
tmpNode = *node;
*node = tmpNode->next;
delete tmpNode;
}
}

void display(struct Node *head) {
Node *list = head;
while(list) {
cout << list->data << " ";
list = list->next;
}
cout << endl;
cout << endl;
}

int main()
{
struct Node *newHead;
struct Node *head = new Node;
  
initNode(head,10);
display(head);

addNode(head,20);
display(head);

addNode(head,30);
display(head);

addNode(head,35);
display(head);

addNode(head,40);
display(head);

insertFront(&head;,5);
display(head);

int numDel = 5;
Node *ptrDelete = searchNode(head,numDel);
if(deleteNode(&head;,ptrDelete))
cout << "Node "<< numDel << " deleted!\n";
display(head);

cout << "The list is reversed\n";
reverse(&head;);
display(head);

cout << "The list is copied\n";
copyLinkedList(head,&newHead;);
display(newHead);

cout << "Comparing the two lists...\n";
cout << "Are the two lists same?\n";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;

numDel = 35;
ptrDelete = searchNode(newHead,numDel);
if(deleteNode(&newHead;,ptrDelete)) {
cout << "Node "<< numDel << " deleted!\n";
cout << "The new list after the delete is\n";
display(newHead);
}
cout << "Comparing the two lists again...\n";
cout << "Are the two lists same?\n";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
  
cout << endl;
cout << "Deleting the copied list\n";
deleteLinkedList(&newHead;);
display(newHead);
return 0;
}


Related Solutions

An array has an index of [5] at the starting address of 200. It has 3...
An array has an index of [5] at the starting address of 200. It has 3 words per memory cell, determine loc[3],loc[4] and NE. (3 Marks: 1 mark for each) A 2-D array defined as A[10 , 5] requires 4 words of storage space for each element. Calculate the address of A[4,3] given the base address as 250 If the array is stored in Row-major form If the array is stored in Column-major form Write a method for the following...
in java, starting with an empty list, show conents of an array (including their index), size...
in java, starting with an empty list, show conents of an array (including their index), size 4, implimenting a circular queue after the following operations are performed: insert(2) insert(7) insert(9) delete() delete() insert(5) detete() delete() insert(15)
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting from 1. Your element [0][0] should be equal to 1; element[4][4] should be equal 25. Print the array. The output should have 5 rows and 5 columns. Specify the width for each output to demonstrate the table in a formatted view. Change the value of the elements: 2nd row 4th column to 24, 1st row 3rd column to 13. Print the array again. Find...
Use the 74LS138 , to design an address decoder that will place 8K by 16bit RAM at a starting address of C000h and 8K RAM at a starting address of 6000h.
Use the 74LS138 , to design an address decoder that will place 8K by 16bit RAM at a starting address of C000h and 8K RAM at a starting address of 6000h.
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
What is an array data structure? What is an array index? What are the benefits of...
What is an array data structure? What is an array index? What are the benefits of array structures? What are the drawbacks of array structures? What is a grid structure? Give examples of when an array could be used. Give examples of when a grid could be used.
5. Suppose the index model for stocks 1 and 2 has the following results ?1 =...
5. Suppose the index model for stocks 1 and 2 has the following results ?1 = 0.5% + 0.95?? + ?1, ?2 = −1.2% + 1.25?? + ?2, ?? = 25%. The standard deviations of ?1 and ?2 are 20% and 22% respectively. ( 1) Calculate the standard deviation of stock 1 and stock 2. (2) What is the covariance between each stock and the market index. (3) What are the covariance and correlation coefficient between these two stocks? (4)...
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
Write a procedure to reverse an array. Assume staring address has been passed in ESI, the...
Write a procedure to reverse an array. Assume staring address has been passed in ESI, the number of units in ECX, the unit size in EBX
Topic: Equality and Inequality in Health In approx. 200 words, address one of these questions: 1)...
Topic: Equality and Inequality in Health In approx. 200 words, address one of these questions: 1) What is the link between income and health in different settings? 2) How does health status vary with income in different settings? 3) How do health services vary with income in different settings? 4) What are the health consequences of inequality? 5) What are the social and political forces that are associated with inequality in health services and health status?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT