Skip to content

Commit

Permalink
Merge pull request #166 from K-Saniya/K-Saniya-Singly-Linked-List-Cod…
Browse files Browse the repository at this point in the history
…e-branch

Added code for Singly Linked List
  • Loading branch information
sujana-kamasany authored Oct 11, 2023
2 parents 7d0c15e + b9ffd41 commit ac0c096
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions singlyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.util.*;

class Node{
int data;
Node next;

Node()
{
next=null;
}
Node(int data)
{
this.data=data;
next=null;
}

}
class LinkedList
{int count=1;
int data;
Node head;
Node ptr;
Node temp;
LinkedList()
{
head=null;
}
void insert(int data)
{
temp=new Node(data);
if(head==null)
{
head=temp;
}
else
{
ptr=head;
while(ptr.next!=null)
{
ptr = ptr.next;
}
ptr.next = temp;
}
}

void display()
{
Node ptr=head;
while(ptr!=null)
{
System.out.println("data is= "+ptr.data);
ptr=ptr.next;
}

}


}



public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int y,d;
//creating object of LinkedList class
LinkedList l =new LinkedList();
//user input for linked list data
do{
System.out.println("enter data =");
d=sc.nextInt();
l.insert(d);
System.out.println("if you want to add more enter 1 else enter 0");
y=sc.nextInt();

}while(y==1);

//display method for the stored data
l.display();

}
}


0 comments on commit ac0c096

Please sign in to comment.