0% found this document useful (0 votes)
161 views4 pages

Lab 1 - (DS) - Skeleton Code For Singly Linked List - UA

This document provides skeleton code for a singly linked list data structure in Java. It defines Node and LinkedList classes to implement the linked list. The Node class defines functions to set/get data and link fields. The LinkedList class implements functions for basic linked list operations like insertion, deletion, checking emptiness and size. The main method tests the linked list implementation with a menu driven program that allows inserting, deleting and traversing the list.

Uploaded by

Anonymous Yc54u9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views4 pages

Lab 1 - (DS) - Skeleton Code For Singly Linked List - UA

This document provides skeleton code for a singly linked list data structure in Java. It defines Node and LinkedList classes to implement the linked list. The Node class defines functions to set/get data and link fields. The LinkedList class implements functions for basic linked list operations like insertion, deletion, checking emptiness and size. The main method tests the linked list implementation with a menu driven program that allows inserting, deleting and traversing the list.

Uploaded by

Anonymous Yc54u9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Skeleton Code for Singly Linked List

package linked.list;
import java.util.Scanner;
/**
*
* @author Usman
*/
class Node
{
protected int data;
protected Node link;
/* Constructor */
public Node()
{
}
/* Constructor */
public Node(int d,Node n)
{
}
/* Function to set link to next Node */
public void setLink(Node n)
{
}
/* Function to set data to current Node */
public void setData(int d)
{
}
/* Function to get link to next node */
public Node getLink()
{
}
/* Function to get data from current Node */
public int getData()
{
}
}

/* Class linkedList */
class linkedList
{
protected Node start;
public int size ;
/* Constructor */
public linkedList()
{
}
/* Function to check if list is empty */
public boolean isEmpty()
{
}
/* Function to get size of list */
public int getSize()
{
}
/* Function to insert an element at begining */
public void insertAtStart(int val)
{
}
/* Function to insert an element at end */
public void insertAtEnd(int val)
{
}
/* Function to insert an element at position */
public void insertAtPos(int val , int pos)
{
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
}
/* Function to display elements */
public void display()
{
}
}

public class SinglyLinkedList {


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test\n");
char ch;
/* Perform list operations */
do
{
System.out.println("\nSingly Linked List Operations\n");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtStart( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtEnd( scan.nextInt() );
break;
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Invalid position\n");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.println("Enter position");

int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position\n");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" \n");
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* Display List */
list.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}

You might also like