اذهب إلى المحتوى
  • 0

إنشاء، إضافة، حذف على stack list في لغة جافا

محمد الحربي

السؤال

Recommended Posts

  • 1

لا أحب حل التمارين للطلاب عكس أن أتابع معه طرف بطرف حتى الوصول للنتيجة المطلوبة لكن لا عليك سأساعدك في حل التمرين حتى تأخده كمرجع.

ملاحظة: لا أفهم ما علاقة queue الموجودة بالسؤال واحد مع stack هناك فرق شاسع بين كلاهما.

// Java code for stack implementation 

import java.io.*; 
import java.util.*; 

class Main 
{ 
  	// Question1: create stack list 
	static Stack<Integer> stack_create() 
	{ 
		return new Stack<Integer>();
	}
	
  	// Question2: Display the stack 
	static void stack_display(Stack<Integer> stack) 
	{  
		System.out.println("Elements on stack: " + stack); 
	} 
	
  	// Question3: count size of the stack 
	static void stack_count(Stack<Integer> stack) 
	{  
		System.out.println("Size of the stack is: " + stack.size()); 
	} 
	
	// Question4: Insert new nodes on the stack 
	static void stack_push(Stack<Integer> stack) 
	{ 
      	Scanner sc = new Scanner(System.in);
      	System.out.print("Input the number of nodes: ");
      	int N = sc.nextInt();
		for(int i = 0; i < N; i++) 
		{ 
    		System.out.print("Input data for node " + (i+1) + ": ");
			stack.push(sc.nextInt()); 
		} 
	} 
	
	// Question5: remove nodes from the stack 
	static void stack_pop(Stack<Integer> stack) 
	{ 
      	Integer y = (Integer) stack.pop(); 
      	System.out.println("the node which has the value " + y + " is removed"); 
	} 

	public static void main (String[] args) 
	{ 
		Stack<Integer> stack = new Stack<Integer>();
		stack_push(stack); 
		stack_pop(stack); 
		stack_count(stack); 
		stack_display(stack);
	} 
} 

تمت الإجابة على جميع الأسئلة بكل وضوح ولتسهل القراءة تمت إضافة تعليقات على كل method.

الصورة التالية بها عملية test للتمرين:

Screenshot_2020-09-27_18-31-46.png.696480eb10642289adc129d068058494.png

يمكنك الضغط هنا وستجد الكود وبيئة العمل أيضا لتتأكد منه.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

1. create queue list

//declare a Queue   
 Queue<Integer> q = new LinkedList<>(); 

2. display the list

//print the Queue
    System.out.println("The Queue contents:" + q);

3. count the nodes in queue

System.out.println("Size of the Queue: " + q.size());

4. insert new node

q.add("new element");

5. remove node

int removedele = q.remove(); 
System.out.println("removed element-" + removedele); 
System.out.println(q); 

6. last method

Queue q = new Queue(4);
q.enqueue(5);
q.enqueue(6);
q.enqueue(7);	
q.enqueue(9);

 

اتمنى لك التوفيق.

تم التعديل في بواسطة ياسين عناية
رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...