0% found this document useful (0 votes)
8 views12 pages

Lab 7

Uploaded by

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

Lab 7

Uploaded by

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

Lab Task 7

Linked List
----------------------------------------
Name: Muhammad Hassam
Roll no: 037
Section: BSSE-3A

----------------------------------------

Ex 1: Write a function to get Nth node in a Linked List.


Code:
//Node Class
public class Node {
public int Data;
public Node next;
public Node(int value){
Data = value;
next = null;
}
public void display(){
System.out.print("{"+Data+"} ");
}
}
// Linked List Class
public class LinkedList {
public Node head;
public LinkedList(){
head = null;
}

public void insertAtFirst(int value){


Node newNode = new Node(value);
newNode.next = head;
head = newNode;
}

public void insertAtLast(int value){


Node newNode = new Node(value);
if(isEmpty()){
head = newNode;
return;
}
Node temp = head;
while (temp.next != null){
temp = temp.next;
}
temp.next = newNode;
}

public int deleteAtFirst(){


if(isEmpty()){
System.out.println("List is Empty. Nothing to delete");
return -1;
}
Node temp = head;
head.next = head;
temp.next = null;
return temp.Data;
}

public int deleteAtLast(){


if(isEmpty()){
System.out.println("List is Empty. Nothing to delete");
return -1;
}
if(head.next == null){
int data = head.Data;
head = null;
return data;
}
Node temp = head;
while (temp.next.next != null){
temp = head.next;
}
int data = temp.next.Data;
temp.next = null;
return data;
}

public void displayData(){


Node current = head;
System.out.println("List - Head ---> Tail");
while (current != null){
current.display();
current = current.next;
}
System.out.println();
}

public boolean isEmpty(){


return (head == null);
}
}
//Main Class
public class Main {
public static void main(String[] args) {
System.out.println("---------------------------------------");
System.out.println("Program To Find Nth Term in Linked List ");
LinkedList list = new LinkedList();
list.insertAtLast(1);
list.insertAtLast(2);
list.insertAtLast(3);
list.insertAtLast(4);
list.displayData();
findNthTerm(list,4);
System.out.println("\n---------------------------------------");
}

public static void findNthTerm(LinkedList list, int term){


Node temp = list.head;
int counter = 1;
while (temp != null){
if(counter == term){
System.out.print(term+" term is :");
temp.display();
return;
}
temp = temp.next;
counter++;
}
System.out.println("Not found in the list.");
}
}

Output:
Ex 2: Write a function that counts the number of times
a given int occurs in a Linked List.
Code:

//Main class
public class Main {
public static void main(String[] args) {
System.out.println("---------------------------------------");
System.out.println("Program To Find Occurance of each integer in
Linked List ");
LinkedList list = new LinkedList();
list.insertAtLast(1);
list.insertAtLast(2);
list.insertAtLast(2);
list.insertAtLast(4);
list.displayData();
findOccurance(list);
System.out.println("\n---------------------------------------");

}
public static void findOccurance(LinkedList list){
Node out = list.head;

while ( out != null ){


int count = 0;
int occ =0;
Node in =list.head;

while (in != null){


if(in.Data == out.Data){
count++;
}
in =in.next;
}

// this is just to make display better //


boolean found = false; //
Node temp = list.head; //
while(temp != out){ //
if(temp.Data == out.Data){ //
found = true; //
break; //
} //
temp =temp.next; //
}; //
if(found){ //
out = out.next; //
continue; //
} //
///////////////////////////////////////

System.out.println(out.Data + " occurred " + count + " time(s)");

out= out.next;
}
}
}

Output:

Ex 3: Write a function that Swap Nodes in a Linked List.


Code:
// Main
public class Main {
public static void main(String[] args) {
System.out.println("---------------------------------------");
System.out.println("Program To SwapNodes in Linked List ");
System.out.println("Before:");
LinkedList list = new LinkedList();
list.insertAtLast(1);
list.insertAtLast(2);
list.insertAtLast(3);
list.insertAtLast(4);
list.displayData();
System.out.println("After:");
swapNodes(list);
list.displayData();
System.out.println("---------------------------------------");
}

public static void swapNodes(LinkedList list){


if (list.head == null || list.head.next == null) {
System.out.println("List has less than two nodes; cannot swap.");
return;
}
Node node1 = list.head;
Node node2 = list.head.next;

node1.next = node2.next;
node2.next = node1;
list.head = node2;
}
}

Output:

Stack and Queue Implementation using Linked List.


Code:
// Stack
public class Node {
public int Data;
public Node next;
public Node(int value){
Data = value;
next = null;
}
public void displayNode(){
System.out.println("{"+Data+"}");
}
}
// Linked list class
public class LinkedList {
public Node head;

public LinkedList(){
head = null;
}
public boolean isEmpty(){
return(head == null);
}

public void insertAtFirst(int value){


Node newNode = new Node(value);
newNode.next=head;
head = newNode;
}

public void insertAtLast(int value){


Node newNode = new Node(value);
if(isEmpty()){
head = newNode;
return;
}
Node temp = head;
while (temp.next != null){
temp = temp.next;
}
temp.next = newNode;
}

public int deleteAtFirst(){


if (isEmpty()) {
System.out.println("List is empty. No node to delete.");
return -1;
}
Node temp = head;
head = head.next;
temp.next= null;
return temp.Data;
}
public int deleteAtLast(){
if (isEmpty()) {
System.out.println("List is empty. No node to delete.");
return -1;
}
if (head.next == null) {
int data = head.Data;
head = null;
return data;
}
Node temp = head;
while(temp.next.next != null){
temp = temp.next;
}
int data = temp.next.Data;
temp.next =null;
return data;
}

}
// Stack Class
public class Stack {
private int top;
private LinkedList list;
public int size;
public Stack(){
top = -1;
list = new LinkedList();
size = 0;
}

public boolean isEmpty(){


return (top == -1);
}

public void push(int x){


list.insertAtLast(x);
top = x;
size++;
}

public int pop(){


if(isEmpty()){
System.out.println("Stack is Empty");
return -1;
}
else{
int x = list.deleteAtLast();
size--;
if(size > 0){
Node temp = list.head;
while (temp.next != null){
temp = temp.next;
}
top= temp.Data;

}else{
top=-1;
}
return x;
}
}

public int peek(){


if(isEmpty()){
System.out.println("Stack is Empty");
return -1;
}
else{
int x = top;
return x;
}
}
}
// Main Class
public class Main {
public static void main(String[] args){
Stack stack = new Stack();
stack.push(45);
stack.push(15);
stack.push(25);
stack.push(35);
while (!stack.isEmpty()){
System.out.println("popped :"+stack.pop());
}

stack.push(55);
System.out.println("peek :"+stack.peek());

}
}

Stack Output:

Code:
//Queue
public class Queue {
int size ;
LinkedList list;

public Queue(){
size = 0;
list = new LinkedList();

}
public void insert(int x){
size++;
list.insertAtLast(x);
}
public int remove(){
if(isEmpty()){
System.out.println("Queue is Empty");
return -1;
}
else{
int data = list.deleteAtFirst();
size--;
return data;
}
}

public boolean isEmpty() {


return (size == 0);
}
}

//Main Class
public class Main {
public static void main(String[] args){
Queue queue = new Queue();
queue.insert(10);
queue.insert(20);
queue.insert(30);
queue.insert(40);
while (!queue.isEmpty()){
System.out.println("dequeued : "+ queue.remove());
}
}
}

Queue Output:

Linked List Class Complete Operations


public class LinkedList {
public Node head;
public LinkedList(){
head = null;
}
public boolean isEmpty(){
return(head == null);
}

public void insertAtFirst(int value){


Node newNode = new Node(value);
newNode.next=head;
head = newNode;
}

public void insertAtLast(int value){


Node newNode = new Node(value);
if(isEmpty()){
head = newNode;
return;
}
Node temp = head;
while (temp.next != null){
temp = temp.next;
}
temp.next = newNode;
}

public int deleteAtFirst(){


if (isEmpty()) {
System.out.println("List is empty. No node to delete.");
return -1;
}
Node temp = head;
head = head.next;
temp.next= null;
return temp.Data;
}
public int deleteAtLast(){
if (isEmpty()) {
System.out.println("List is empty. No node to delete.");
return -1;
}
if (head.next == null) {
int data = head.Data;
head = null;
return data;
}
Node temp = head;
while(temp.next.next != null){
temp = temp.next;
}
int data = temp.next.Data;
temp.next =null;
return data;
}

public void displayList(){


System.out.println("List(head --> tail) :");
Node current = head;
while(current !=null){
current.displayNode();
current = current.next;
}
System.out.println();
}

public boolean search(int key){


Node temp = head;
while(temp!= null){
if(temp.Data == key){
System.out.println("Key Found in this List");
return true;
}
temp = temp.next;
}
System.out.println("Key not Found!");
return false;
}
}

You might also like