Answer to Question #245265 in C for harry

Question #245265

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.




1
Expert's answer
2021-10-01T16:52:41-0400
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();
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS