0% found this document useful (0 votes)
4 views19 pages

Singly Linked List

The document outlines operations on singly linked lists and stacks, including adding, merging, and manipulating nodes. It provides code snippets for various cases such as creating lists, finding maximum and minimum values, checking if lists are sorted, and performing stack operations like push and pop. Additionally, it includes error handling for empty stacks and methods to traverse and display the contents of the lists and stacks.
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)
4 views19 pages

Singly Linked List

The document outlines operations on singly linked lists and stacks, including adding, merging, and manipulating nodes. It provides code snippets for various cases such as creating lists, finding maximum and minimum values, checking if lists are sorted, and performing stack operations like push and pop. Additionally, it includes error handling for empty stacks and methods to traverse and display the contents of the lists and stacks.
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

Singly linked list

case 14: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("14. create and return array containing info of all nodes in the 7-node list: ");

List1.traverse();

break;

case 15: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

for(int i = 1; i<=x; i++){

List2.addToTail(sc.nextInt());

System.out.print("15. Merge two ordered singly linked lists of integers into one ordered list: 7-
node list = " + List1.printList() +"; 5-node list: " + List2.printList());

List1.mergeList(List2);

List1.traverse();

break;

}
case 16: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

int y = sc.nextInt();

System.out.print("16. add a node with value 14 before the node 15 in the 12-node list: ");

List1.traverse();

List1.addBeforeElement(15, 14);

List1.traverse();

break;

case 17: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

for(int i = 1; i<=x; i++){

List2.addToTail(sc.nextInt());

System.out.println("17. Attach a singly linked list of 6 elements "+ List2.printList()+" to the end
of another singly linked list of 13 nodes: "+ List1.printList());

List1.attachList(List2);

List1.traverse();

break;

case 18: {
for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("18. find and return the maximum value in the 19-node list: ");

List1.traverse();

System.out.println(List1.maxValue());

break;

case 19: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("19. find and return the minimum value in the 19-node list: ");

List1.traverse();

System.out.println(List1.minValue());

break;

case 20: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("20. return the sum of all values in the 19-node list: ");

List1.traverse();

System.out.println(List1.sum());

break;

}
case 21: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("21. return the average of all values in the 19-node list: ");

List1.traverse();

System.out.println(String.format("%.2f", List1.sumdb()/n));

break;

case 22: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.println("22. check and return true if the 19-node list "+ List1.printList()+"is sorted,
return false if the list is not sorted.");

System.out.print(List1.checkSort());

break;

case 23: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.println("23. sort the 19-node list: "+ List1.printList() +"then insert a node with value
40 into the sorted list so that the new list is a sorted list");

List1.sortList();

List1.traverse();
List1.insertSorted(40);

List1.traverse();

break;

case 24: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("24. Reverse the singly linked list of 20 nodes: ");

List1.traverse();

List1.reverse();

List1.traverse();

break;

case 25: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

for(int i = 1; i<=x; i++){

List2.addToTail(sc.nextInt());

System.out.print("25. Check whether two singly linked list have the same contents: 1st list of 5
elements: "+List1.printList()+"; 2nd list of 5 elements: "+List2.printList());

List1.isEqual(List2);

break;

}
}

ArrayList Stack
import java.util.*;

class Node

Object info;

Node next;

public Node(Object x, Node p)

info = x;

next=p;

}
public Node(Object x)

this(x,null);

class LinkedStack

Node head, tail;

LinkedStack()

head=tail=null;

boolean isEmpty()

return(head==null);

void push(Object x)

if(tail==null)

head = tail = new Node(x,null);

else

tail.next = new Node(x,null);

tail=tail.next;

Object top() throws EmptyStackException


{

if(isEmpty()) throw new EmptyStackException();

return(tail.info);

Object pop() throws EmptyStackException

if(isEmpty()) throw new EmptyStackException();

Object x = tail.info;

Node cur = head;

while(cur.next!=tail)

cur=cur.next;

tail=cur; tail.next=null;

return(x);

void traverse()

Node cur = head;

while(cur!=null)

System.out.print(cur.info+" ");

cur=cur.next;

System.out.println();

int getSize ( )

int size =0;


Node cur=head;

while(cur!=null)

size++;

cur=cur.next;

return size;

public class Class

public static void main (String[ ] args)

LinkedStack myStack = new LinkedStack();

Scanner sc = new Scanner(System.in);

int option=sc.nextInt();

int n,x;

switch(option)

case 1 :

n=sc.nextInt();

for(int i=1; i<=n;i++)

myStack.push(sc.nextInt());

x=sc.nextInt();

System.out.print("1. Push "+x+" to the top of "+n+"-element stack: ");

myStack.traverse();
myStack.push(x);

System.out.print("Current stack: "); myStack.traverse();

break;

case 2:

n=sc.nextInt();

for(int i=1; i<=n;i++)

myStack.push(sc.nextInt());

System.out.print("2. Pop the top element of "+n+"-element stack: ");

myStack.traverse();

System.out.println(myStack.pop());

System.out.print("Current stack: "); myStack.traverse();

break;

case 3:

System.out.println("3. Pop empty stack");

try

myStack.pop();

catch (EmptyStackException e)

System.out.print("Error: Stack is empty");

break;
}

case 4:

n=sc.nextInt();

System.out.print("4. Top of "+n+"-element stack: ");

for(int i=1; i<=n;i++)

myStack.push(sc.nextInt());

myStack.traverse();

try

System.out.println(myStack.top());

catch(EmptyStackException e)

System.out.print("Stack is empty");

System.out.print("\nCurrent stack: ");

myStack.traverse();

break;

case 5:

n=sc.nextInt();

System.out.print("5. After 5 operations on the " +n+ "-element stack: ");

for(int i=1; i<=n;i++)

myStack.push(sc.nextInt());
}

myStack.traverse();

for(int i=1;i<=6;i++)

String ope = sc.next();

if(ope.equals("pop"))

try

myStack.pop();

catch (EmptyStackException e)

System.out.print("Stack is empty");

else if(ope.equals("add"))

int num = sc.nextInt();

myStack.push(num);

System.out.print("Current stack size: "+myStack.getSize() +"\nCurrent stack: ");

myStack.traverse();

break;

default:
{

LinkedList Stack
import java.util.*;

class ArrayStack

Object[] a;

int top, max;

public ArrayStack()

this(50);

public ArrayStack(int max1)

max=max1;

a = new Object[max];

top=-1;

boolean grow()

int max1 = max + max/2;

Object[] a1 = new Object[max1];

if(a1==null)

return false;
for(int i=0;i<=top;i++)

a1[i]=a[i];

a=a1;

return(true);

boolean isEmpty()

return (top==-1);

boolean isFull()

return (top==max-1);

void clear()

top=-1;

void push(Object x)

if(isFull()&&!grow())

return;

a[++top]=x;

Object top() throws EmptyStackException

{
if(isEmpty()) throw new EmptyStackException();

return(a[top]);

public Object pop() throws EmptyStackException

if(isEmpty()) throw new EmptyStackException();

Object x = a[top];

top--;

return(x);

void traverse()

for(int i=0;i<=top;i++)

System.out.print(a[i]+" ");

System.out.println();

public class Test

public static void main(String[] args)

ArrayStack myStack = new ArrayStack();

Scanner sc = new Scanner(System.in);

int option=sc.nextInt();

int n,x;

switch(option){

case 1:
{

n= sc.nextInt();

for(int i=1;i<=n;i++)

myStack.push(sc.nextInt());

x=sc.nextInt();

System.out.print("1. Push "+x+" to the top of "+n+"-element stack: ");

myStack.traverse();

myStack.push(x);

System.out.print("Current stack: ");

myStack.traverse();

break;

case 2:

n=sc.nextInt();

for(int i=1;i<=n;i++)

myStack.push(sc.nextInt());

System.out.print("2. Pop the top element of "+n+"-element stack: ");

myStack.traverse();

System.out.println(myStack.pop());

System.out.print("Current stack: ");

myStack.traverse();

break;
}

case 3:

System.out.println("3. Pop empty stack");

try

myStack.pop() ;

catch (EmptyStackException e)

System.out.print("Error: Stack is empty");

break;

case 4:

n=sc.nextInt();

System.out.print(("4. Top of "+n+"-element stack: "));

for(int i=1;i<=n;i++)

myStack.push(sc.nextInt());

myStack.traverse();

try

System.out.println(myStack.top());
}

catch(EmptyStackException e)

System.out.println("Stack is empty ");

System.out.print("Current stack: ");

myStack.traverse();

break;

case 5:

n=sc.nextInt();

System.out.print("5. After 5 operations on the "+n+"-element stack: ");

for(int i=1;i<=n;i++)

myStack.push(sc.nextInt());

myStack.traverse();

for(int i=1;i<=6;i++)

String ope = sc.next();

if(ope.equals("pop"))

try

myStack.pop();

}
catch(EmptyStackException e)

System.out.println("Stack is empty");

else if(ope.equals("add"))

int num = sc.nextInt();

myStack.push(num);

System.out.print("Current stack size: " + (myStack.top+1) +"\nCurrent stack: ");

myStack.traverse();

break;

default:

System.out.print("error") ;

You might also like