0 Replies - 4039 Views - Last Post: 01 June 2008 - 08:39 AM Rate Topic: -----

#1 ranfx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-June 08

Help with fairly simple menu-driven program using linked lists

Posted 01 June 2008 - 08:39 AM

So basically, I'm retarded and have just started this program and can't quite figure it out. the idea is to make a airline database with 3 sll's. As you can see it's not anywhere close to being done, but i'm getting frustrated that i can't even get the first part of the menu done. it compiles... i tried printing the lists at the end of the program just to test it, and it doesn't print anything. also, if you're wondering why i made some of the stuff static, i did it to appease the compiler, even though it didn't seem necessary to me at all.

i'll appreciate any help, i'm pretty sure it's something pretty basic so yea. Also, the linked list code is totally fine, i know that for sure but put it in for reference. thanks!

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

public class airline
{
	public static Scanner keyboard = new Scanner(System.in);

	public static int menuNum, flightNum;
	public static String name, style;

	public static SLL flight1 = new SLL();
	public static SLL flight2 = new SLL();
	public static SLL flight3 = new SLL();

	public void addTo(String N, int F, String S)
	{
		if (S == "front")
		{
			if(F == 1)
			{
				flight1.addToHead(N);
			}
			else if(F == 2)
			{
				flight2.addToHead(N);
			}
			else if(F == 3)
			{
				flight3.addToHead(N);
			}
		}
		else if (S == "back")
		{
			if(F == 1)
			{
				flight1.addToTail(N);
			}
			else if(F == 2)
			{
				flight2.addToTail(N);
			}
			else if(F == 3)
			{
				flight3.addToTail(N);
			}
		}
	}



	public static void main(String[] arg)
	{
		airline obj = new airline();

		System.out.println("What would you like to do? Enter the corresponding number, your name, and flight number. \n\n"
							+ "1. Add a name to a certain flight \n"
							+ "2. Delete a name from a certain flight \n"
							+ "3. Search a name\n"
							+ "4. See an entire flight list \n\n");
		menuNum = keyboard.nextInt();
		name = keyboard.next();
		flightNum = keyboard.nextInt();

		if (menuNum == 1)
		{
			System.out.println("Add to front or back?");

			style = keyboard.next();

			if (style == "front")
			{
				obj.addTo(name, flightNum, "front");
			}
			else if (style == "back")
			{
				obj.addTo(name, flightNum, "back");
			}

		}

			flight1.printAll();
			flight2.printAll();
			flight3.printAll();



	}
}




SLL

public class SLL<T>
{
	protected SLLNode<T> head, tail;
	public SLL() // condtructor that shows list is empty
	{ head = tail = null; }


	public boolean isEmpty() // returns if empty or not T/F
	{ return head == null; }


	public void addToHead(T el)
	{
		head = new SLLNode<T>(el,head); // can include SLLNode first like creating object for pointer
		if (tail == null)
			tail = head;
	}


	public void addToTail(T el)
	{
		if (!isEmpty()) {
			tail.next = new SLLNode<T>(el);
			tail = tail.next; // add node and move tail to last
		}
		else head = tail = new SLLNode<T>(el); // 1 node
	}

	public T deleteFromHead()
	{ // delete the head and return its info;
		if (isEmpty())
			 return null;
		T el = head.info;
		if (head == tail)	   // if only one node on the list;
			 head = tail = null;
		else head = head.next;
		return el;
	}


	public T deleteFromTail()
	{ // delete the tail and return its info;
		if (isEmpty())
			 return null;
		T el = tail.info;
		if (head == tail)	   // if only one node in the list;
			 head = tail = null;
		else {				  // if more than one node in the list,
			 SLLNode<T> tmp;	// find the predecessor of tail;
			 for (tmp = head; tmp.next != tail; tmp = tmp.next);
			 tail = tmp;		// the predecessor of tail becomes tail;
			 tail.next = null;
		}
		return el;
	}


	public void delete(T el)
	{  // delete the node with an element el;
		if (!isEmpty())
			if (head == tail && el.equals(head.info)) 		// if only one
				 head = tail = null;	   					// node on the list;
			else if (el.equals(head.info))					// if more than one node on the list;
				 head = head.next;							// and el is in the head node;
			else {											// if more than one node in the list
				 SLLNode<T> pred, tmp;						// and el is in a nonhead node;
				 for (pred = head, tmp = head.next;
					  tmp != null && !tmp.info.equals(el);
					  pred = pred.next, tmp = tmp.next);
				 if (tmp != null)
				 {											// if el was found;
					 pred.next = tmp.next;
					 if (tmp == tail) 						// if el is in the last node;
						tail = pred;
				 }
			}
	}


	public void printAll()
	{
		for (SLLNode<T> tmp = head; tmp != null; tmp = tmp.next)
			System.out.print(tmp.info + " ");
	}


	public boolean isInList(T el) {
		SLLNode<T> tmp;
		for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);
		return tmp != null;
	}
}




node

import java.io.*;
import java.util.Scanner;
import java.util.LinkedList;

public class SLLNode<T>
{
	public T info;

	public SLLNode<T> next;

	public SLLNode()
	{
		next = null;
	}

	public SLLNode(T el)
	{
		info = el;
		next = null;
	}

	public SLLNode(T el, SLLNode<T> ptr)
	{
		info = el;
		next = ptr;
	}

}




Is This A Good Question/Topic? 0
  • +

Page 1 of 1