You are on page 1of 1

DSA LAB 06 (Linked List) BS F13-Aft, BIT F13

Note 1: Lab is graded activity therefore no discussion allowed between students. Students are not allowed
to talk to anyone during lab except Teacher, TA’s, and lab staff. In case of any communication between
students, both students will be penalized, so take care. Ask TA’s they will be helpful.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Task 1: Implement class Class with concept of header linked list. A new function is to search old name and add
new student after that student. Overload stream insertion operator similar to show function. Lastly write
destructor for this class. The idea of destructor is simple take place temp to start, move start to next & delete
temp, move on till end? (10 Marks)
#define null 0 int main()
struct Student {
{ Class bscs;
string name;
int marks;//out of 1000
bscs.addStudent("Naeem", 910);
Student *next; bscs.addStudent("Amir", 870);
Student(string n, int m) bscs.addStudent("Zeeshan", 915);
{ cout<<bscs<<"------------------------------\n";
name=n; bscs.addStudentAfter("Zaheer", 910,"Amir");
marks=m; bscs.addStudentAfter("Kamran", 870, "Zeeshan");
next=null; bscs.addStudentAfter("Nadeem", 915, "Naeem");
}
};
cout<<bscs<<"------------------------------\n";
class Class bscs.removeStudent("Naeem");
{ cout<<bscs<<"------------------------------\n";
Student *first, *last; bscs.removeStudent("Kamran");
public: cout<<bscs<<"------------------------------\n";
Class(); bscs.removeStudent("Zeeshan");
void addStudent(string n, int m); cout<<bscs;
void addStudentAfter(string n, int m, string oldName); return 0;
void removeStudent(string name); }
~Class();
}; Remaining part of output, read left to write
Output:
Kamran 870
Amir 870 ------------------------------
Zaheer 910 Zeeshan 915
Naeem 910 Amir 870
Nadeem 915 Zaheer 910
------------------------------ Nadeem 915
Zeeshan 915 ------------------------------
Kamran 870 Amir 870
Amir 870 Zaheer 910
Zaheer 910 Nadeem 915
Nadeem 915 ... Allah Hafiz Class ... (message from destructor)

Task 2: Write class DLinkedList and implement following member functions? (5 Marks)
void addBefore(T, T);//add T1 before T2, means first search T1
void deleteNext (T);//search T and delete next node
void reverse();//reverse list, set previous pointers to next and next to previous. Call show function before
& after calling reverse function. If stuck leave it for some other time, after all your head is costly.
Take 2 pointers in reverse function keep 1 to current and 2 to next. Set current pointers and move to 2. After
loop set pointer of end node and swap first & end.
***************** Yahoo! Finished **************

Resource Person: Abdul Mateen PUCIT-NC

You might also like