Uploaded by suhail khan

cs223 2023f project assignment (3)

advertisement
201 Work for Midterm
You need to satisfy at leats 60 over 100 to pass the course. So get these
fucking questions be solved.
1- Question is about singly circular linked list with no dumy node.
class myList{
public:
myList(); //tail pointer assigning to NULL
void appendList(myList &other);
private:
struct Node
{
int item;
Node* next;
};
Node* tail;
};
Write appendList function that appands the input list given as parameter at the
end of the list object calling this member fuction. For example if your first list
contains [1 2 3 4] form start to end and your appending list contains [5 6 7 8] ,
your final list will be [1 2 3 4 5 6 7 8]. Your method should be working on O(1)
time. You cannot change or add any thing into myList class.
2AIn this problem a sorted linked list is given to you as in ascending order.
struct Node
{
int item;
Node* next;
};
class SortedList{
public:
SortedList(){
head = NULL;
}
Node *head;
};
Write a global function which takes head
pointer of a list and an integer value as
input and returns the adress of Node after
which the given value should be inserted.
This function wont do and insertian at all.
You dont do an insertion. It just returns the
adress. For example,fort he linked list
below, your function should return the
adress of the node that contains an integer
value of 7 if the input value is 9. Your
function should return NULL if the input
value is 1.
3 à 7 à 11 à 13
Fill the prototype of the function below and solve it.
_________ findAdress( ________ head, const int value){
}
Bstruct DoublyList
{
int item;
DoublyList *next;
DoublyList *prev;
};
2
Consider that doubly linked list implementation.
You need to write code segment which inserts new node
pointed by newPtr after the node pointed by cur.
(Assume cur pointer will not hold NULL value.)
3- Express the output of these functions seperately.
class Test{
public:
Test(int no = 0){
id = no;
cout<<"Constructor for "<< id<<endl;
}
Test(const Test& T){
id = T.id;
cout<<"Copy Constructor for "<<
id<<endl;
}
~Test(){
cout<<"Destructor for "<< id<<endl;
}
void operator=(const Test &right){
id = right.id;
cout<<"Assigment operator for "<<
id<<endl;
}
private:
int id;
};
//part A
void A(Test B, Test & C){
cout << "inside A"<< endl;
}
int main(){
Test X(1) , Y(2);
A(X,Y);
return 0;
}
3
//part B
void B(Test *A){
delete []A;
}
int main(){
Test Y(3), *X = new Test(6);
X = new Test[2];
cout<<"Before function B"<<endl;
B(X);
cout<<"After function B"<<endl;
return 0;
}
//part C
void C(Test *A){
Test B(4) , D(B);
cout<<"Inside function C"<<endl;
*A = D;
}
int main(){
Test A(5);
C(&A);
cout<<"After function C"<<endl;
return 0;
}
4Display the output of this program properly and with in order of program call.
void displayList(Node *head){
if (head == NULL)
cout<<"NULL"<<endl;
else{
for(Node * X = head;x !=NULL;
x = x->next){
cout<< x->data<<endl;
}
}
}
Node *myFunc1(int val1, int val2 , int val3){
Node *head = new Node;
head->data = val1;
head->next = new Node;
nead->next->data = val2;
head->next->next = new Node;
nead->next->next->data = val3;
nead->next->next->next = NULL;
return head;
}
void myFunc2(Node *head1, Node *head2,
Node *head3){
head1->next = head3;
head1->next->data = 3;
head1 = head2;
head1-> data = 5;
head1->next->data = 7;
head2 = head3;
head3 = head1;
}
4
void myFunc3(Node *head){
for(; head != NULL ; head = head->next){
head->data /= 2;
}
}
int main(){
Node *head1 = myFunc1(10,20,30);
Node *head2 = myFunc1(2,4,6);
Node *head3 = myFunc1(11,22,33);
myFunc2(head1, head2, head3);
displayList(head1);
displayList(head2);
displayList(head3);
head1 = myFunct1(1,2,3);
myFunc3(head1);
displayList(head1);
return 0 ;
}
Answers
void List::appendList(List *other){
if (other.tail == NULL)
return;
else if(tail == NULL){
tail = other.tail;
other.tail = NULL;
}
else{
Node * first = tail -> next;
tail -> next = other.tail -> next;
other.tail -> next = first;
tail = other.tail;
other.tail = NULL;
}
}
Node *findAddress(Node *head, const int value){
if (head == NULL)
return NULL;
if (head -> item > value)
return NULL;
Node *cur = head;
for(;cur->next != NULL; cur= cur->next){
if(cur -> next -> item > value)
break;
}
return cur;
}
newPtr ->next = cur -> next;
newPtr->prev = cur;
cur -> next = newPtr;
if(newPtr -> next != NULL)
newPtr -> next -> prev = cur;
C1
C2
CC1
Inside A
D1
D2
D1
10 3 22 33
576
3 22 33
011
5
C3
C6
C0
C0
Before B
D0
D0
After B
D3
C5
C4
CC4
Inside C
Assignment 4
D4
D4
After C
D4
Download