السلام عليكم اخواني واخواتي .. كيف حالكم , ان شاء الله تكونوا في خير و عافيه
هذا اول موضوع لي في المنتدى وانا عاوز مساعده وان شاء الله تفيدوني وافيدكم
هذا السوؤال عبارة عن LinkedList وانا عندي حل للسؤال هذا بس مش بالطرقية هذي لانه فيه الكثير من import وانا عاوز اقلل منه
و عاوزة يتفرع لاربع كلاسات مثل : Node , Data, Object and LinkedList
هذا السؤال :
You are required to implement a linked list ADT that contains only one reference to the front of the list. Each node should be able to keep a generic Object type. Your implementation should at least include the following operations:
Add a new item to the list
Remove a specified item from the list
Retrieve a specified item from the list
Make the list empty
Check whether the list is empty
Other operations deemed necessary
You are also required to implement an Iterator for your linked list.
وهذا الجواب اللي عاوزة يتفرع الى اربع كلاسات
[CODE]
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class LinkedListExample extends JFrame{
private JButton addNewItem = null;
private JButton getSpecificItem = null;
private JButton getAllItems = null;
private JButton removeOneItem = null;
private JButton removeAllItems = null;
private JButton checkList = null;
private JPanel buttonsPanel = null;
private JPanel statusPanel = null;
private JTextArea statusMessage = null;
private JScrollPane scrollPane = null;
private List exampleLinkedList = null;
public LinkedListExample(){
super("Linked List Example");
//create new linked List
exampleLinkedList = new LinkedList();
exampleLinkedList.add("Linked List Example");
//Initialize GUI elements
addNewItem = new JButton("Add an Item");
getSpecificItem = new JButton("Get an Item");
getAllItems = new JButton("Get All Items");
removeOneItem = new JButton("Remove an Item");
removeAllItems = new JButton("Remove All Items");
checkList = new JButton("Check List");
buttonsPanel = new JPanel();
statusPanel = new JPanel();
statusMessage = new JTextArea("Linked List Example");
statusMessage.setEditable(false);
statusMessage.setLineWrap(true);
scrollPane = new JScrollPane(statusMessage, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(300, 300));
//Add Them to a panel
buttonsPanel.add(addNewItem);
buttonsPanel.add(getSpecificItem);
buttonsPanel.add(getAllItems);
buttonsPanel.add(removeOneItem);
buttonsPanel.add(removeAllItems);
buttonsPanel.add(checkList);
statusPanel.add(scrollPane);
//Add elements to the frame
add(buttonsPanel,BorderLayout.NORTH);
add(statusPanel,BorderLayout.SOUTH);
/*------------- Buttons Actions--------------------------*/
addNewItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addNew();
}
});
getSpecificItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getSpecificItem();
}
});
getAllItems.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getAllItems();
}
});
removeOneItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeOneItem();
}
});
removeAllItems.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeAllItems();
}
});
checkList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkList();
}
});
}
private void addNew(){
addLogMessage("\nAdd New Item Example");
String result = "" ;
result = JOptionPane.showInputDialog("Add New");
exampleLinkedList.add(result);
addLogMessage("Item {" + result + "} added to the list");
}
private void getSpecificItem(){
addLogMessage("\nGet Specific Item Example");
String result = "";
int max = exampleLinkedList.size() - 1;
result = JOptionPane.showInputDialog("Enter number between 0 and " + max);
addLogMessage("Item at index {" + result + "} is : {" + exampleLinkedList.get(Integer.parseInt(result)) + "}");
}
private void getAllItems(){
addLogMessage("\nGet All Items Example");
int count = 0;
ListIterator listIterator = exampleLinkedList.listIterator();
while(listIterator.hasNext()){
addLogMessage("Object " + count + " : " + listIterator.next());
count++;
}
}
private void removeOneItem(){
addLogMessage("\nRemove One Item Example");
String result = "";
int max = exampleLinkedList.size() - 1;
result = JOptionPane.showInputDialog("Enter number between 0 and " + max);
addLogMessage("\nItem {" + exampleLinkedList.get(Integer.parseInt(result)) + "} at index {" + result + "} has been removed");
exampleLinkedList.remove(Integer.parseInt(result));
}
private void removeAllItems(){
addLogMessage("\nRemove All Items Example");
exampleLinkedList.clear();
addLogMessage("List has been cleared");
}
private void checkList(){
addLogMessage("\nCheck List Example");
if(exampleLinkedList.isEmpty()){
addLogMessage("List is empty");
}else{
addLogMessage("List is not empty");
}
}
private void addLogMessage(String Message){
statusMessage.append("\n" + Message);
statusMessage.setCaretPosition(statusMessage.getDocument().getLength());
}
public static void main(String[] args) {
LinkedListExample theApplication = new LinkedListExample();
theApplication.setDefaultCloseOperation(EXIT_ON_CLOSE);
theApplication.setSize(500, 500);
theApplication.pack();
theApplication.setVisible(true);
}
}
[CODE/]
وهذا أول كلاس سويته وهو عبارة عن Node
كود:
public class Node
{
int num;
Node next;
Node()
{
num = 0;
next = null;
}
Node(int num, Node next)
{
this.num=num;
this.next=next;
}
int getNum()
{
return num;
}
Node getNext()
{
return next;
}
void setNext(Node next)
{
this.next = next;
}
}