0% found this document useful (0 votes)
78 views5 pages

Class Linkedlist (Node Head Class Node (Int Node Node Next

The document defines a LinkedList class that implements basic linked list functionality including adding nodes to the front and end of the list, adding a node after a given node, and reversing the list. The LinkedList class contains a Node inner class to represent individual nodes, and methods like push(), append(), addAfter(), printList(), and reverse() to manipulate the list. It also includes a main method that demonstrates creating and modifying a linked list.

Uploaded by

gautam7777
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)
78 views5 pages

Class Linkedlist (Node Head Class Node (Int Node Node Next

The document defines a LinkedList class that implements basic linked list functionality including adding nodes to the front and end of the list, adding a node after a given node, and reversing the list. The LinkedList class contains a Node inner class to represent individual nodes, and methods like push(), append(), addAfter(), printList(), and reverse() to manipulate the list. It also includes a main method that demonstrates creating and modifying a linked list.

Uploaded by

gautam7777
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/ 5

class LinkedList

Node head;

class Node

int node;

Node next;

Node(int d)

node = d;

next= null;

public void push(int new_data)

Node new_node = new Node(new_data);

new_node.next = head;

head= new_node;

}
public void addAfter(Node prev_node , int data)

if(prev_node == null)

System.out.println("the given node is null");

return;

Node new_node = new Node(data);

new_node.next = prev_node.next;

prev_node.next = new_node;

public void append(int data)

Node new_node = new Node(data);

if(head == null)

head = new Node(data);

return;

new_node.next= null;
Node last = null;

try

while(last.next != null)

last = last.next;

last.next = new_node;

return;

catch (Exception e)

public void printList()

int a = 0;

Node tnode = head;

while(tnode!=null)

System.out.println("----->"+tnode.node );

tnode = tnode.next;
a++;

System.out.println("the count is" + a);

public Node reverse(Node node)

Node Next = null;

Node prev = null;

Node current=head ;

while(current!=null)

Next= current.next;

current.next = prev;

prev = current;

current =Next;

node = prev;

return node;

}
public static void main(String args[])

LinkedList list = new LinkedList();

list.append(6);

list.push(7);

list.push(1);

list.append(4);

list.addAfter(list.head.next,8);

list.printList();

System.out.println("reverse likedlist is:");

list.reverse(list.head);

list.printList();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy