03 - | Java - Data Structures For Beginners | - | Single Linked Lists - Part 01 |

preview_player
Показать описание
by : Dr. Mohamed El Desouki
شرح تراكيب البيانات
Single Linked List Operations - Part 01
مرفق الكود الخاص بكل فديو فى التعليقات
Рекомендации по теме
Комментарии
Автор

package singlelinkedlist;

import java.util.LinkedList;

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

class Slinkedlist
{
Node head;

public void Append(int item)
{
Node n =new Node(item);
if (head==null)
head=n;
else
{
Node current =head;
while(current.next!=null)
current=current.next;

current.next=n;

}

}
public void InsertFirst(int item)
{
Node newNode = new Node(item); // Create a new node
newNode.next = head; // Link the new node to the current head
head = newNode; // Update the head to the new node
}

public void InsertatPosition(int item, int position)
{
Node newNode = new Node(item);
// If inserting at the head (position 0)
if (position == 0) {
InsertFirst(item);
return;
}

Node current = head;
int index = 0;

// Traverse to the position just before the specified position
while (current != null && index < position - 1) {
current = current.next;
index++;
}

// If current is null, position is beyond the end of the list
if (current == null) {
System.out.println("Position is out of bounds.");
return;
}

// Insert the new node
newNode.next = current.next;
current.next = newNode;
}

public void traverse()
{
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}

public void delete(int item)
{
if (head == null) return; // empty list

if (head.data == item)
{
head = head.next;
return;
}

Node current = head;
while (current.next != null)
{
if (current.next.data == item)
{
current.next = current.next.next;
return;
}
current = current.next;
}
}
public int length() {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
// Search for a value
public boolean search(int item) {
Node current = head;
while (current != null) {
if (current.data == item) {
return true;
}
current = current.next;
}
return false;
}
public void merge(Slinkedlist list2) {
if (head == null) {
head = list2.head;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = list2.head;
}
// Sort the linked list using Bubble Sort
public void sort() {
if (head == null) return;

boolean swapped;
do {
swapped = false;
Node current = head;
while (current.next != null) {
if (current.data > current.next.data) {
int temp = current.data;
current.data = current.next.data;
current.next.data = temp;
swapped = true;
}
current = current.next;
}
} while (swapped);
}
// Reverse the linked list
public void reverse() {
Node prev = null;
Node current = head;
Node next = null;

while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}

}


public class SingleLinkedListDemo {

public static void main(String[] args) {
Slinkedlist slist = new Slinkedlist();
slist.Append(10);
slist.Append(20);
slist.Append(30);
slist.traverse();

Slinkedlist l2 = new Slinkedlist();
l2.Append(40);
l2.Append(50);
l2.Append(60);
l2.traverse();
slist.merge(l2);
slist.traverse();
// // slist.InsertFirst(5);
// //slist.traverse();
// //slist.InsertFirst(1);
// //slist.traverse();
// slist.InsertatPosition(5, 0);
// slist.traverse();
// slist.InsertatPosition(25, 3);
// slist.traverse();
// slist.InsertatPosition(100, 8);
// slist.traverse();
// slist.delete(20);
// slist.traverse();
// if (slist.search(130))
// System.out.println("The Item Found");
// else
// System.out.println("The item not found");
//
// System.out.println("The Number of nodes in list =" + slist.length());
}

}

DesoukiEgypt
Автор

نفسي اقابلك واشكرك بجد علي ضميرك العالي جدا وعلي طريقه شرحك المبسطة والمفصله تفصيل ممل وعالي جدا بجد انتا رقم واحد ليا والوحيد الي حببني في البرمجه وفهمهاني بحبك يا دكتور ابانوب من مصر ❤❤❤❤❤❤

بوببوبي-صث
Автор

سبحان الله وبحمده سبحان الله العظيم
جزاكم الله خيرا

User-pv
Автор

والله انت أسطورتي و يا ليت تكمل الكورس

Memaoooo
Автор

دكتورنا الحبيب بارك الله فيك وجزاك الله خير الجزاء على مجهوداتك، دكتورنا الحبيب أنا أريد البدء في عالم البرمجة من الصفر وأريد أبدأ معك بدءاً من لغة JAVA، هل هناك أي مشكلة إذا بدأت البرمجة عن طريق لغة JAVA؟ لأن الغالبية الساحقة ينصحون بتعلم البرمجة بدءاً بلغة ++C وأنا أريد أن أبدءها بلغة JAVA هل هناك مشكلة في ذلك؟ مع جزيل الشكر والتقدير والإحترام.

rabbitlover
visit shbcf.ru