Hello guys im working on a project for my class and im stuck. i dont have much of experience with linked list and my book isn't helpful with this project. Here is what the project is about: Develop an interactive program to implement basic linked list operations including:

1. Add A Node
• To the beginning of a list
• To the end of a list
• After the Nth node
2. Delete A Node
• First node
• Last node
• Given its content
• The Nth node
3. Locate a node
4. Traverse a list

Display the content of the list after every operation. Add an item to the list of menu items to allow the user to terminate the program. Use the random number generator class to generate random numbers between 100 and 1000 to place in the data field of the list.

And Here is the code that i currently have. Please help. This is a gui program, but if you could help with creating the list and adding and deleting the nodes i could go from there. Thanks

// GUI-related imports
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.util.Random;
import java.util.*;
public class project6 extends Frame implements ActionListener
{
 String command = "";
 Random RN = new Random(0,100,1000);
 LinkedList            LL = new LinkedList();
 int                   position, value;
 String                inputValue;
 Node                  location = new Node();
 public static void main(String[] args )
 {
  Frame frame = new project6(); 
   
  frame.setResizable(false);
  frame.setSize(900,500);
  frame.setVisible(true);
 }
 
 public  project6()
 {
  setTitle("Linked List Operations");
  
  // Create Menu Bar
        
  MenuBar mb = new MenuBar();
  setMenuBar(mb);
  
  // Create Menu Group Labeled "File"
  Menu UtilitiesMenu = new Menu("Utilities");
  mb.add(UtilitiesMenu);
  // **** add menu items
  MenuItem create=new MenuItem ("Create List");
  create.addActionListener(this);
  UtilitiesMenu.add(create);
  MenuItem locate=new MenuItem ("Locate Node");
  locate.addActionListener(this);
  UtilitiesMenu.add(locate);
  MenuItem traverse=new MenuItem ("Traverse List");
  traverse.addActionListener(this);
  UtilitiesMenu.add(traverse);
 MenuItem exit=new MenuItem ("Exit");
  exit.addActionListener(this);
  UtilitiesMenu.add(exit);
  
  Menu AddMenu = new Menu("Add");
  mb.add(AddMenu);
  
 // **** add menu items
  MenuItem add1=new MenuItem ("Add Beginning");
  add1.addActionListener(this);
  AddMenu.add(add1);
  MenuItem addlast=new MenuItem ("Add to the End");
  addlast.addActionListener(this);
  AddMenu.add(addlast);
  MenuItem addn=new MenuItem ("Add after N");
  addn.addActionListener(this);
  AddMenu.add(addn);
  
  
  
  //////////////////////
  Menu DeleteMenu = new Menu("Delete");
  mb.add(DeleteMenu);
 // **** add menu items
  MenuItem first=new MenuItem ("Delete 1st Node");
  first.addActionListener(this);
  DeleteMenu.add(first);
  MenuItem dlast=new MenuItem ("Delete Last");
  dlast.addActionListener(this);
  DeleteMenu.add(dlast);
  MenuItem dselect=new MenuItem ("Delete by Content");
  dselect.addActionListener(this);
  DeleteMenu.add(dselect);
  MenuItem deleteN=new MenuItem ("Delete N node");
  deleteN.addActionListener(this);
  DeleteMenu.add(deleteN);
  
  
  
  WindowListener l = new WindowAdapter()
  {
      
   public void windowClosing(WindowEvent ev)
   {
    System.exit(0);
   }
   
   public void windowActivated(WindowEvent ev)
   {
    repaint();
   }
   
   public void windowStateChanged(WindowEvent ev)
   {
    repaint();
   }
  
  };
  
  ComponentListener k = new ComponentAdapter()
  {
   public void componentResized(ComponentEvent e) 
   {
          repaint();           
      }
  };
  
  // register listeners
   
  this.addWindowListener(l);
  this.addComponentListener(k);

 }
 
//******************************************************************************
//  called by windows manager whenever the application window performs an action
//  (select a menu item, close, resize, ....
//******************************************************************************

 public void actionPerformed (ActionEvent ev)
  {
   // figure out which command was issued
   
   command = ev.getActionCommand();
   
   // take action accordingly
      
   if("Create List".equals(command))
   {
    LL.AddToBegining(RN.getRandomNumber());
    repaint();  
   }
   
   else
    if ("Locate Node".equals(command)){
     //
    }
    else
     if ("Traverse List".equals(command)){
      //
     }
     else
      if("Add Beginning".equals(command)){
       //
      }
      else
       if("Add to the End".equals(command)){
        //
       }
       else
        if("Add after N".equals(command)){
         //
        }
        else
         if("Delete 1st Node".equals(command)){
          //
         }
         else
          if("Delete Last".equals(command)){
           //
          }
          else 
           if("Delete N node".equals(command)){
            //
           }
           else 
            if("Delete by Content".equals(command)){
             //
            }
          
  //*** add all other options

  else
    if("Exit".equals(command))
    {
       
     System.exit(0);
    }
  }
//********************************************************
// called by repaint() to redraw the screen
//********************************************************
  
  public void paint(Graphics g)
  {
   if (LL.head == null)
   {
    g.drawString(" Empty List", 400, 250);
    return;
   }
   else
   {
    int xs = 100;
    int ys = 100;
    Node t= LL.head;
    g.drawString("Head", 50, 60);
    g.drawLine(60, 65, 100, 110);
    while (t != null)
    {
   //**** display the rectangle and node content


   //**** check if the right end of window is reached, move to next line
     t = t.next;
    }
    g.drawString("End Of List", xs+5, ys+15);
   }
   
   
  }
  /////////////////////////////////////////
  public class Node
  {
      int data;
      Node next;
  }
  
  public class LinkedList 
  {
      Node head = null;
      Node tail = null;
      long  length;
      
      public LinkedList()
      {
          head = null; 
          tail = null;
          length = 0;
      }
      public void AddToBegining(int number)
      {
       head = null;
       while (number)
       {
        new  = new data;
        number = new number(Node);
        number.data = new data;
        number.next = head;
        head   = t;
       }

          
      }
      public void AddToEnd(int number)
      {
        
         
      }
      public void AddAfterNthNode(int number, int position)
      {
       temp=head;
       position=n;
       if(head !=null){
        while(temp !=null && position < n){
         temp=temp.next;
         position++;
        }
        if(position==n){
         number.next=temp.next;
         temp.next=number;
        }
        else {
         System.out.print("node n doesnt exist");
        }
          
      }
   //  public Node DeleteFromBegining()
      {
          if (head==null){
           System.out.print("nothing to delete");
          }
          else{
           head=head.next;
          }
          }
        
      }
     // public Node DeleteFromEnd()
      {
        
      }
     // public Node DeleteSpecificNode(int temp,count,n)
      { 
       
       temp=head;
       count=n;
       while (temp!=null && count < n){
        temp=temp.next;
        count++;
       }
       if (count==n){
       }
       else (temp==null){
        system.outprint("specified node doesnt exist");
       }
       
        
      }
     // public Node LocateNode(int arg)
      {
       
      }
      public void Traverse()
      {
       temp = head;                    
       While (temp != null)
       {
        System.out.print("node found"+ temp.data;);
        temp = temp.next;     
       }

         
      
      }
      
  }


}
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;


public class LinkedList implements Serializable {

		public void add(int index, Object element) {
			// TODO Auto-generated method stub
		}
		
		public boolean add(Object o) {
			// TODO Auto-generated method stub
		}
		
		public boolean addAll(Collection c) {
			// TODO Auto-generated method stub
		}
		
		public boolean addAll(int index, Collection c) {
			// TODO Auto-generated method stub
		}

		public void addFirst(Object o) {
			// TODO Auto-generated method stub
		}
		
		public void addLast(Object o) {
			// TODO Auto-generated method stub
		}
		
		public void clear() {
			// TODO Auto-generated method stub
		}
		
		public Object clone() {
			// TODO Auto-generated method stub
		}
		
		public boolean contains(Object o) {
			// TODO Auto-generated method stub
		}
		
		public Object get(int index) {
			// TODO Auto-generated method stub
		}
  
		public Object getFirst() {
			// TODO Auto-generated method stub
		}

		public Object getLast() {
			// TODO Auto-generated method stub
		}
		
		public int indexof(Object o) {
			// TODO Auto-generated method stub
		}

		public int lastIndexOf(Object o) {
			// TODO Auto-generated method stub
		}
		
		public ListIterator listIterator(int index) {
			// TODO Auto-generated method stub
		}

		public Object remove(int index) {
			// TODO Auto-generated method stub
		}

		public boolean remove(Object o) {
			// TODO Auto-generated method stub
		}
    
		public Object removeFirst() {
			// TODO Auto-generated method stub
		}
		
		public Object removeFirst() {
			// TODO Auto-generated method stub
		}
    
		public Object removeLast() {
			// TODO Auto-generated method stub
		}

		public Object set(int index, Object element) {
			// TODO Auto-generated method stub
		}
     
		public int size() {
			// TODO Auto-generated method stub
		}
   
		public Object[] toArray() {
			// TODO Auto-generated method stub
		}
		
		public Object[] toArray(Object[] a) {
			// TODO Auto-generated method stub
		}
}

Here's the framework; that should get you on the right track.

I suggest starting with some of the easy methods like size(), clear(), add() first. Implement the more difficult ones last (iterator, toArray, etc).

What behavior should these methods exhibit? I'm confident that any young CS student is capable of navigating their way to the Java API to discover what LinkedList does.

Happy coding :D

Edited 5 Years Ago by ztini: n/a

Im still unclear on how to add the new nodes to the list.. and the only imports and util we are allowed to use is what i have in the first piece of code.

This article has been dead for over six months. Start a new discussion instead.