Link Queue ISC Computer Science 2012 Theory

Link is an entity which can hold a maximum of 100 integers. Link enables the user to add elements from the rear end and remove integers from the front end of the entity.

Define a class Link with the following details:

Class name: Link

Data members/instance variables:
lnk[]: entity to hold the integer elements
max: stores the maximum capacity of the entity
begin: to point to the index of the front end
end: to point to the index of the rear end

Member functions:
Link(int m): constructor to initialize max = m, begin = 0, end = 0
void addLink(int v): to add an element from the rear index if possible otherwise display the message "OUT OF SIZE"
int delLink(): to remove and return an element from the front index, if possible otherwise display the message "EMPTY" and return -99
void display(): displays the elements of the entity

(a) Specify the class Link giving details of the constructor, void addLink(int), int delLink() and void display(). The main() function and algorithm need not be written.

import java.util.Scanner;
class Link{
    int lnk[];
    int max;
    int begin;
    int end;
    public Link(int m){
        max = m;
        lnk = new int[max];
        begin = 0;
        end = 0;
    }
    public void addLink(int v){
        if(end == max)
            System.out.println("OUT OF SIZE");
        else
            lnk[end++] = v;
    }
    public int delLink(){
        if(end == 0){
            System.out.println("EMPTY");
            return -99;
        }
        int v = lnk[begin++];
        if(begin >= end){
            begin = 0;
            end = 0;
        }
        return v;
    }
    public void display(){
        if(end == 0)
            System.out.println("EMPTY");
        else{
            for(int i = begin; i < end; i++)
                System.out.print(lnk[i] + " ");
            System.out.println();
        }
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Link size: ");
        int size = Integer.parseInt(in.nextLine());
        Link obj = new Link(size);
        while(true){
            System.out.println("1. Add element");
            System.out.println("2. Remove element");
            System.out.println("3. Display elements");
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(in.nextLine());
            switch(choice){
            case 1:
                System.out.print("Element to be added: ");
                int v = Integer.parseInt(in.nextLine());
                obj.addLink(v);
                break;
            case 2:
                v = obj.delLink();
                if(v != -99)
                    System.out.println(v + " removed");
                break;
            case 3:
                obj.display();
                break;
            default:
                System.out.println("Bye!");
                return;
            }
        }
    }
}

(b) What type of data structure is the above entity?

Queue is the type of data structure in the above entity.

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory