In: Computer Science
a. Create a SLL for N Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of SLL
d. Perform Insertion at the third position.
e. Delete the element at the Front of SLL
f. Perform Deletion at second position of SLL
g. Display the content.
Linked List (SLL) of Student Data with the fields: USN, Name, Branch, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of SLL
d. Perform Insertion and Deletion at Front of SLL
e. Display the content.
a. Create a DLL for N Data by using front insertion.
b. Perform Insertion and Deletion at End of DLL
d. Perform Insertion at the third position.
e. Delete the element in the Front of SLL
f. Perform Deletion at second position of SLL
g. Display the content.
// A complete working Java program on linked list
class LinkedList
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/* Inserts a new node after the given prev_node. */
public void insertAfter(Node prev_node, int new_data)
{
/* 1. Check if the given Node is null */
if (prev_node == null)
{
System.out.println("The given previous node cannot be null");
return;
}
/* 2 & 3: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 4. Make next of new Node as next of prev_node */
new_node.next = prev_node.next;
/* 5. make next of prev_node as new_node */
prev_node.next = new_node;
}
/* Appends a new node at the end. This method is
defined inside LinkedList class shown above */
public void append(int new_data)
{
/* 1. Allocate the Node &
2. Put in the data
3. Set next as null */
Node new_node = new Node(new_data);
/* 4. If the Linked List is empty, then make the
new node as head */
if (head == null)
{
head = new Node(new_data);
return;
}
/* 4. This new node is going to be the last node, so
make next of it as null */
new_node.next = null;
/* 5. Else traverse till the last node */
Node last = head;
while (last.next != null)
last = last.next;
/* 6. Change the next of last node */
last.next = new_node;
return;
}
/* This function prints contents of linked list starting from
the given node */
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
/* Returns count of nodes in linked list */
public int getCount()
{
Node temp = head;
int count = 0;
while (temp != null)
{
count++;
temp = temp.next;
}
return count;
}
// Function to remove the last node
// of the linked list /
public Node removeLastNode()
{
if (head == null)
return null;
if (head.next == null) {
return null;
}
// Find the second last node
Node second_last = head;
while (second_last.next.next != null)
second_last = second_last.next;
// Change next of second last
second_last.next = null;
return head;
}
// Function to remove the first node
// of the linked list /
public Node removeFirstNode()
{
if (head == null)
return null;
// Move the head pointer to the next node
Node temp = head;
head = head.next;
return head;
}
/* Given a reference (pointer to pointer) to the head of a list
and a position, deletes the node at the given position */
public void deleteNode(int position)
{
// If linked list is empty
if (head == null)
return;
// Store head node
Node temp = head;
// If head needs to be removed
if (position == 0)
{
head = temp.next; // Change head
return;
}
// Find previous node of the node to be deleted
for (int i=0; temp!=null && i<position-1; i++)
temp = temp.next;
// If position is more than number of nodes
if (temp == null || temp.next == null)
return;
// Node temp->next is the node to be deleted
// Store pointer to the next of node to be deleted
Node next = temp.next.next;
temp.next = next; // Unlink the deleted node from list
}
}
class Main{
/* Driver program to test above functions. Ideally this function
should be in a separate user class. It is kept here to keep
code compact */
public static void main(String[] args)
{
/* Start with the empty list */
LinkedList llist = new LinkedList();
// Insert 6. So linked list becomes 6->NUllist
llist.append(6);
// Insert 7 at the beginning. So linked list becomes
// 7->6->NUllist
llist.push(7);
// Insert 1 at the beginning. So linked list becomes
// 1->7->6->NUllist
llist.push(1);
System.out.println("Created Linked list is: ");
llist.printList();
System.out.println("");
System.out.println("Total No of Nodes: " + llist.getCount());
// Insert 4 at the end. So linked list becomes
// 1->7->6->4->NUllist
llist.append(4);
// Delete 4 at the end. So linked list becomes
// 1->7->6->NUllist
llist.removeLastNode();
System.out.println("List After Insertion And Removing At Last: ");
llist.printList();
// Insert 8, after 7. So linked list becomes
// 1->7->8->6->NUllist
llist.insertAfter(llist.head.next, 8);
System.out.println("");
System.out.println("List After Insertion At 3rd Position: ");
llist.printList();
// Delete first node. So linked list becomes
// 7->8->6->NUllist
llist.removeFirstNode();
System.out.println("");
System.out.println("List After Deleting first Node: ");
llist.printList();
// Delete node At Position 2. So linked list becomes
// 7->8->NUllist
llist.deleteNode(2);
System.out.println("");
System.out.println("List After Deleting Node At Position 2: ");
llist.printList();
}
}
Output for first program:
Second Program Linked List For Student details:
// A complete working Java program on linked list
class LinkedList
{
Node head; // head of list
/* Linked list Node*/
class Node
{
String USN;
String Name;
String Branch;
int sem;
int phoneNo;
Node next;
Node(String USN,String Name,String Branch,int sem,int phoneNo) {
this.USN = USN;
this.Name = Name;
this.Branch = Branch;
this.sem = sem;
this.phoneNo = phoneNo;
next = null;
}
}
/* Inserts a new Node at front of the list. */
public void push(String USN,String Name,String Branch,int sem,int phoneNo)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(USN,Name,Branch,sem,phoneNo);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/* Appends a new node at the end. This method is
defined inside LinkedList class shown above */
public void append(String USN,String Name,String Branch,int sem,int phoneNo)
{
/* 1. Allocate the Node &
2. Put in the data
3. Set next as null */
Node new_node = new Node(USN,Name,Branch,sem,phoneNo);
/* 4. If the Linked List is empty, then make the
new node as head */
if (head == null)
{
head = new Node(USN,Name,Branch,sem,phoneNo);
return;
}
/* 4. This new node is going to be the last node, so
make next of it as null */
new_node.next = null;
/* 5. Else traverse till the last node */
Node last = head;
while (last.next != null)
last = last.next;
/* 6. Change the next of last node */
last.next = new_node;
return;
}
/* This function prints contents of linked list starting from
the given node */
public void printList()
{
Node tnode = head;
while (tnode != null)
{
String USN;String Name;String Branch;int sem;int phoneNo;
System.out.print("USN: "+ tnode.USN+" Name: "+tnode.Name+" Branch: "+tnode.Branch+" Sem: "+tnode.sem+" Phone No: "+tnode.phoneNo);
System.out.println("");
tnode = tnode.next;
}
}
/* Returns count of nodes in linked list */
public int getCount()
{
Node temp = head;
int count = 0;
while (temp != null)
{
count++;
temp = temp.next;
}
return count;
}
// Function to remove the last node
// of the linked list /
public Node removeLastNode()
{
if (head == null)
return null;
if (head.next == null) {
return null;
}
// Find the second last node
Node second_last = head;
while (second_last.next.next != null)
second_last = second_last.next;
// Change next of second last
second_last.next = null;
return head;
}
// Function to remove the first node
// of the linked list /
public Node removeFirstNode()
{
if (head == null)
return null;
// Move the head pointer to the next node
Node temp = head;
head = head.next;
return head;
}
}
class Main{
/* Driver program to test above functions. Ideally this function
should be in a separate user class. It is kept here to keep
code compact */
public static void main(String[] args)
{
/* Start with the empty list */
LinkedList llist = new LinkedList();
// Insert s1. So linked list becomes s1->NUllist
llist.append("A","Test","B1",1,123);
// Insert s2 at the beginning. So linked list becomes
// s2->s1->NUllist
llist.push("B","Test2","B2",2,1234);
// Insert s3 at the beginning. So linked list becomes
// s3->s2->s1->NUllist
llist.push("C","Test3","B3",3,12345);
System.out.println("Created Linked list is: ");
llist.printList();
System.out.println("");
System.out.println("Total No of Nodes: " + llist.getCount());
// Insert s4 at the end. So linked list becomes
// s3->s2->s1->s4->NUllist
llist.append("D","Test4","B4",4,123456);
// Delete s4 at the end. So linked list becomes
// s3->s2->s1->NUllist
llist.removeLastNode();
System.out.println("List After Insertion And Removing At Last: ");
llist.printList();
// Delete first node. So linked list becomes
// s2->s1->NUllist
llist.removeFirstNode();
System.out.println("");
System.out.println("List After Deleting first Node: ");
llist.printList();
// Insert node at start. So linked list becomes
// s3->s2->s1->NUllist
llist.push("E","Test5","B5",5,1234567);
System.out.println("");
System.out.println("List After Adding Node At Front: ");
llist.printList();
}
}
Output for second program:
Third Porgram to store string :
// A complete working Java program on linked list
class LinkedList
{
Node head; // head of list
/* Linked list Node*/
class Node
{
String Name;
Node next;
Node(String Name) {
this.Name = Name;
next = null;
}
}
/* Inserts a new Node at front of the list. */
public void push(String Name)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(Name);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/* Appends a new node at the end. This method is
defined inside LinkedList class shown above */
public void append(String Name)
{
/* 1. Allocate the Node &
2. Put in the data
3. Set next as null */
Node new_node = new Node(Name);
/* 4. If the Linked List is empty, then make the
new node as head */
if (head == null)
{
head = new Node(Name);
return;
}
/* 4. This new node is going to be the last node, so
make next of it as null */
new_node.next = null;
/* 5. Else traverse till the last node */
Node last = head;
while (last.next != null)
last = last.next;
/* 6. Change the next of last node */
last.next = new_node;
return;
}
/* This function prints contents of linked list starting from
the given node */
public void printList()
{
Node tnode = head;
while (tnode != null)
{
String Name;
System.out.print(tnode.Name + " ");
tnode = tnode.next;
}
}
/* Returns count of nodes in linked list */
public int getCount()
{
Node temp = head;
int count = 0;
while (temp != null)
{
count++;
temp = temp.next;
}
return count;
}
// Function to remove the last node
// of the linked list /
public Node removeLastNode()
{
if (head == null)
return null;
if (head.next == null) {
return null;
}
// Find the second last node
Node second_last = head;
while (second_last.next.next != null)
second_last = second_last.next;
// Change next of second last
second_last.next = null;
return head;
}
// Function to remove the first node
// of the linked list /
public Node removeFirstNode()
{
if (head == null)
return null;
// Move the head pointer to the next node
Node temp = head;
head = head.next;
return head;
}
// function to create and return a Node
public Node GetNode(String data) {
return new Node(data);
}
// function to insert a Node at required position
public Node insertAtPos(int position, String data) {
if (position < 1)
System.out.print("Invalid position");
// if position is 1 then new node is
// set infornt of head node
// head node is changing.
if (position == 1) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
} else {
while (position-- != 0) {
if (position == 1) {
// adding Node at required position
Node newNode = GetNode(data);
// Making the new Node to point to
// the old Node at the same position
newNode.next = head.next;
// Replacing current with new Node
// to the old Node to point to the new Node
head.next = newNode;
break;
}
head = head.next;
}
if (position != 1)
System.out.print("Position out of range");
}
return head;
}
/* Given a reference (pointer to pointer) to the head of a list
and a position, deletes the node at the given position */
public void deleteNode(int position)
{
// If linked list is empty
if (head == null)
return;
// Store head node
Node temp = head;
// If head needs to be removed
if (position == 0)
{
head = temp.next; // Change head
return;
}
// Find previous node of the node to be deleted
for (int i=0; temp!=null && i<position-1; i++)
temp = temp.next;
// If position is more than number of nodes
if (temp == null || temp.next == null)
return;
// Node temp->next is the node to be deleted
// Store pointer to the next of node to be deleted
Node next = temp.next.next;
temp.next = next; // Unlink the deleted node from list
}
}
class Main{
/* Driver program to test above functions. Ideally this function
should be in a separate user class. It is kept here to keep
code compact */
public static void main(String[] args)
{
/* Start with the empty list */
LinkedList llist = new LinkedList();
// Insert A. So linked list becomes s1->NUllist
llist.append("A");
// Insert B at the beginning. So linked list becomes
// B->A->NUllist
llist.push("B");
// Insert C at the beginning. So linked list becomes
// C->B->A->NUllist
llist.push("C");
System.out.println("Created Linked list is: ");
llist.printList();
System.out.println("");
System.out.println("Total No of Nodes: " + llist.getCount());
// Insert D at the position first. So linked list becomes
// D->C->B->A->NUllist
llist.push("D");
// Insert E at the position third. So linked list becomes
// E->D->C->B->A->NUllist
llist.insertAtPos(1,"E");
// Insert F at the last. So linked list becomes
// E->D->C->B->A->F->NUllist
llist.append("F");
System.out.println("List After Insertion At First,Third And Last: ");
llist.printList();
// Delete first node. So linked list becomes
// D->C->B->NUllist
llist.removeFirstNode();
llist.deleteNode(3);
llist.removeLastNode();
System.out.println("");
System.out.println("List After Deleting First,Third And Last Node: ");
llist.printList();
}
}
3rd Output:
4th Program (CLL):
class Main
{
static class Node
{
int data;
Node next;
};
static Node addToEmpty(Node last, int data)
{
// This function is only for empty list
if (last != null)
return last;
// Creating a node dynamically.
Node temp = new Node();
// Assigning the data.
temp.data = data;
last = temp;
// Creating the link.
last.next = last;
return last;
}
static Node addBegin(Node last, int data)
{
if (last == null)
return addToEmpty(last, data);
Node temp = new Node();
temp.data = data;
temp.next = last.next;
last.next = temp;
return last;
}
static void traverse(Node last)
{
Node p;
// If list is empty, return.
if (last == null)
{
System.out.println("List is empty.");
return;
}
// Pointing to first Node of the list.
p = last.next;
// Traversing the list.
do
{
System.out.print(p.data + " ");
p = p.next;
}
while(p != last.next);
}
// Driven code
public static void main(String[] args)
{
Node last = null;
last = addBegin(last, 4);
last = addBegin(last, 2);
last = addBegin(last, 8);
last = addBegin(last, 12);
last = addBegin(last, 10);
System.out.println("Circular Linked List: ");
traverse(last);
}
}
Output:
Program 5 (DLL):
// Class for Doubly Linked List
class DLL {
Node head; // head of list
/* Doubly Linked list Node*/
class Node {
int data;
Node prev;
Node next;
// Constructor to create a new node
// next and prev is by default initialized as null
Node(int d) { data = d; }
}
// Function to delete a node in a Doubly Linked List.
// head_ref --> pointer to head node pointer.
// del --> data of node to be deleted.
void deleteNode(Node del)
{
// Base case
if (head == null || del == null) {
return;
}
// If node to be deleted is head node
if (head == del) {
head = del.next;
}
// Change next only if node to be deleted
// is NOT the last node
if (del.next != null) {
del.next.prev = del.prev;
}
// Change prev only if node to be deleted
// is NOT the first node
if (del.prev != null) {
del.prev.next = del.next;
}
// Finally, free the memory occupied by del
return;
}
//Deleting The last Node
void deleteNodeAtEnd(){
if(head == null)
{
return;
}
Node ptr = head;
Node prevPtr = null;
while( ptr.next != null ){
prevPtr = ptr;
ptr = ptr.next;
}
prevPtr.next = null;
}
// Adding a node at the front of the list
public void push(int new_data)
{
/* 1. allocate node
* 2. put in the data */
Node new_Node = new Node(new_data);
/* 3. Make next of new node as head and previous as NULL */
new_Node.next = head;
new_Node.prev = null;
/* 4. change prev of head node to new node */
if (head != null)
head.prev = new_Node;
/* 5. move the head to point to the new node */
head = new_Node;
}
/* Given a node as prev_node, insert a new node after the given node */
public void InsertAfter(Node prev_Node, int new_data)
{
/*1. check if the given prev_node is NULL */
if (prev_Node == null) {
System.out.println("The given previous node cannot be NULL ");
return;
}
/* 2. allocate node
* 3. put in the data */
Node new_node = new Node(new_data);
/* 4. Make next of new node as next of prev_node */
new_node.next = prev_Node.next;
/* 5. Make the next of prev_node as new_node */
prev_Node.next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node.prev = prev_Node;
/* 7. Change previous of new_node's next node */
if (new_node.next != null)
new_node.next.prev = new_node;
}
// Add a node at the end of the list
void append(int new_data)
{
/* 1. allocate node
* 2. put in the data */
Node new_node = new Node(new_data);
Node last = head; /* used in step 5*/
/* 3. This new node is going to be the last node, so
* make next of it as NULL*/
new_node.next = null;
/* 4. If the Linked List is empty, then make the new
* node as head */
if (head == null) {
new_node.prev = null;
head = new_node;
return;
}
/* 5. Else traverse till the last node */
while (last.next != null)
last = last.next;
/* 6. Change the next of last node */
last.next = new_node;
/* 7. Make last node as previous of new node */
new_node.prev = last;
}
// This function prints contents of linked list starting from the given node
public void printlist(Node node)
{
Node last = null;
System.out.println("Traversal in forward Direction");
while (node != null) {
System.out.print(node.data + " ");
last = node;
node = node.next;
}
System.out.println();
System.out.println("Traversal in reverse direction");
while (last != null) {
System.out.print(last.data + " ");
last = last.prev;
}
}
// Function to delete a node in a Doubly Linked List.
// head_ref --> pointer to head node pointer.
// del --> pointer to node to be deleted.
Node deleteNodeAtGivenPosUtil(Node head, Node del)
{
// base case
if (head == null || del == null)
return null;
// If node to be deleted is head node
if (head == del)
head = del.next;
// Change next only if node to be
// deleted is NOT the last node
if (del.next != null)
del.next.prev = del.prev;
// Change prev only if node to be
// deleted is NOT the first node
if (del.prev != null)
del.prev.next = del.next;
del = null;
return head;
}
// Function to delete the node at the
// given position in the doubly linked list
public void deleteNodeAtGivenPos(Node head, int n)
{
/* if list in NULL or invalid position is given */
if (head == null || n <= 0)
return;
Node current = head;
int i;
/*
* traverse up to the node at position 'n' from the beginning
*/
for (i = 1; current != null && i < n; i++)
{
current = current.next;
}
// if 'n' is greater than the number of nodes
// in the doubly linked list
if (current == null)
return;
// delete the node pointed to by 'current'
deleteNodeAtGivenPosUtil(head, current);
}
}
class Main{
/* Driver program to test above functions*/
public static void main(String[] args)
{
/* Start with the empty list */
DLL dll = new DLL();
// Insert 6. So linked list becomes 6->NULL
dll.push(6);
// Insert 7 at the beginning. So linked list becomes 7->6->NULL
dll.push(7);
// Insert 1 at the beginning. So linked list becomes 1->7->6->NULL
dll.push(1);
System.out.println("Created DLL is: ");
dll.printlist(dll.head);
System.out.println("");
// Insert 4 at the end. So linked list becomes 1->7->6->4->NULL
dll.append(4);
//Delete Node At End
dll.deleteNodeAtEnd();
System.out.println("List After Inserting And Deletion At End: ");
dll.printlist(dll.head);
System.out.println("");
dll.InsertAfter(dll.head.next, 8);
// Insert 8, at 3rd position. So linked list becomes 1->7->8->6->NULL
System.out.println("List After Inserting At Third Position: ");
dll.printlist(dll.head);
System.out.println("");
//Delete First Node 7->8->6->NULL
dll.deleteNode(dll.head);
System.out.println("List After Deleting In Front: ");
dll.printlist(dll.head);
System.out.println("");
dll.deleteNodeAtGivenPos(dll.head,2);
//Delete First Node 7->6->NULL
System.out.println("List After Deleting Node At Second Position: ");
dll.printlist(dll.head);
System.out.println("");
}
}
Output: