a. Write a C program to implement a singly linked list. The list should store student records i.e. -- name, rollno. , year of joining and enrolled academic program.
b. Reimplement (a) but with a doubly linked list.
c. Implement stack and queue using linked list.
struct student {
string name;
int year;
string rollno;
}
class NodeA {
struct student student;
NodeA *next;
}
class LinkedList {
private:
NodeA *head;
size_t size;
public:
friend class NodeA;
LinkedList() : size(0), head(nullptr) {};
bool insertAtHead(struct student);
bool insertAtTail(struct student);
void remove(struct student);
struct student getLast();
struct student getFirst();
// other methods
}
class NodeB {
struct student student;
NodeB *next;
NodeB *prev;
}
class DoubleLinkedList {
private:
NodeB *head;
NodeB *tail;
size_t size;
public:
friend class NodeB;
DoubleLinkedList() : size(0), head(nullptr), tail(nullptr) {}
bool insertAtHead(struct student);
bool insertAtTail(struct student);
void remove(struct student);
struct student getLast();
struct student getFirst();
// other methods
}
class Stack {
private:
LinkedList list;
public:
bool push(struct student s) {
list.insertAtHead(s);
}
struct student pop() {
return list.getFirst();
}
}
class Queue {
private;
DoubleLinkedList list;
public:
bool push(struct student s) {
list.insertAtHead(s);
}
struct student pop() {
return list.getLast();
}
}
Comments
Leave a comment