building linklist in java


A

Asembereng

Hi i want to do insertion sort, merge sort and quick sort on my
linkList of ints.. do anyone have an idea how to do it??
these are the codes. And i also need to define a lifo fifo class..




class UTGLLElement {
public int myData; // data item

public UTGLLElement next; // next link in list
// ------------------------------------------------------------

public UTGLLElement(int data) // constructor
{
myData = data;
}

// ------------------------------------------------------------

public void displayLink() // display this link
{
System.out.print(myData + " ");
}
// ------------------------------------------------------------


} // end class UTGLLElement
// //////////////////////////////////////////////////////////////

class LinkedList {
private UTGLLElement head; // ref to head link

private UTGLLElement tail; // ref to tail link
// ------------------------------------------------------------

public LinkedList() // constructor
{
head = null; // no links on list yet
tail = null;
}

// ------------------------------------------------------------


public LinkedList(UTGLLElement[] linkArr) // constructor (array as
{ // argument)
head = null;; // initialize list
for(int j=0; j<linkArr.length; j++) // copy array
addElement( linkArr[j] ); // to list
}
// ------------------------------------------------------------

public boolean isEmpty() // true if no links
{
return head == null;
}

// ------------------------------------------------------------



public void addElement(UTGLLElement k) { // make new link
UTGLLElement previous = null; // start at first
UTGLLElement current = head;
// until end of list,
while(current != null && k.myData > current.myData)
{ // or key > current,
previous = current;
current = current.next; // go to next item
}
if(previous==null) // at beginning of list
head = k; // first --> k
else // not at beginning
previous.next = k; // old prev --> k
k.next = current; // k --> old current
}

// ------------------------------------------------------------


public void insertFirst(int myInt) // insert at front of list

{
UTGLLElement newElement = new UTGLLElement(myInt); // make new link
if (isEmpty()) // if empty list,
tail = newElement; // newLink <-- tail
newElement.next = head; // newLink --> old head
head = newElement; // head --> newLink
}

// ------------------------------------------------------------

public void insertLast(int myInt) // insert at end of list
{
UTGLLElement newLink = new UTGLLElement(myInt); // make new link
if (isEmpty()) // if empty list,
head = newLink; // head --> newLink
else
tail.next = newLink; // old tail --> newLink

tail = newLink; // newLink <-- tail
}

// ------------------------------------------------------------

public UTGLLElement del(int key){
UTGLLElement current=head;
UTGLLElement prev=head;
while(current.myData != key)
{
if(current.next == null)
return null; // didn't find it
else
{
prev = current; // go to next link
current = current.next;
}
} // found it
if(current == head) // if first link,
head = head.next; // change first
else // otherwise,
prev.next = current.next; // bypass it
return current;
}
//--------------------------------------------------------------

// public void LinkedList(UTGLLElement[] myArray){
// head = null;; // initialize list
// for(int j=0; j<myArray.length; j++) // copy array
// addElement( myArray[j] ); // to list
//
// }
//--------------------------------------------------------------

public void insertionSort(){
for(UTGLLElement current=head; current !=null;current=current.next){
System.out.print(" here "+current);
}
}



public void displayList() {
System.out.print("Linked list elements : ");
UTGLLElement current = head; // start at beginning
while (current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
// ------------------------------------------------------------


public UTGLLElement remove() // return & delete first link
{ // (assumes non-empty list)
UTGLLElement temp = head; // save first
head = head.next; // delete first
return temp; // return value
}
// ------------------------------------------------------------

//



} // end class LinkedList
// //////////////////////////////////////////////////////////////

class TheMain {
public static void main(String[] args) { // make a new list
LinkedList theList = new LinkedList();
theList.insertFirst(78); // insert at front
theList.insertFirst(12);
theList.insertFirst(88);
theList.insertLast(20); // insert at the back
theList.insertLast(33);
theList.insertLast(55);
theList.displayList(); // display the list
theList.del(20);
theList.insertionSort();
//theList.insertionSort();


theList.displayList(); // display again
} // end main()
} // end class Fir

// //////////////////////////////////////////////////////////////
class LIFO {
private LinkedList theList;

// -------------------------------------------------------------

public LIFO() // constructor
{
theList = new LinkedList();
}
// -------------------------------------------------------------

// -------------------------------------------------------------

}// ENDS the LIFO class
 
Ad

Advertisements

T

Tarique

Asembereng said:
Hi i want to do insertion sort, merge sort and quick sort on my
linkList of ints.. do anyone have an idea how to do it??
these are the codes. And i also need to define a lifo fifo class..
That's a java code that you have posted,so wrong choice of newsgroup,try
c.l.j.programmer!
 
S

santosh

Asembereng said:
Hi i want to do insertion sort, merge sort and quick sort on my
linkList of ints.. do anyone have an idea how to do it??
these are the codes. And i also need to define a lifo fifo class..
<snip>

Post to a group dealing with your language. This group discusses ISO C.
 
R

Richard Heathfield

Asembereng said:
Hi i want to do insertion sort, merge sort and quick sort on my
linkList of ints.. do anyone have an idea how to do it??

Thank you for providing source code. Unfortunately, it doesn't compile. The
error I am getting from my C compiler is:

foo.c:106: unterminated character constant

Here is the line in question.

return null; // didn't find it

When we satisfy the preprocessor by changing 't to 't', some more errors
emerge:

gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
foo.c:1: parse error before `UTGLLElement'
foo.c:1: syntax error before `{'
foo.c:41: warning: ANSI C does not allow extra `;' outside of a function
foo.c:63: warning: type defaults to `int' in declaration of `current'
foo.c:63: request for member `next' in something not a structure or union
foo.c:63: ANSI C forbids data definition with no type or storage class
foo.c:63: parse error before `/'
foo.c:102: warning: type defaults to `int' in declaration of `prev'
foo.c:102: `head' undeclared here (not in a function)
foo.c:102: ANSI C forbids data definition with no type or storage class
foo.c:103: parse error before `while'
foo.c:122: warning: ANSI C does not allow extra `;' outside of a function
foo.c:130: warning: type defaults to `int' in declaration of `current'
foo.c:130: redefinition of `current'
foo.c:63: `current' previously defined here
foo.c:130: request for member `next' in something not a structure or union
foo.c:130: parse error before `)'
foo.c:137: syntax error before `void'
foo.c:137: warning: function declaration isn't a prototype
foo.c: In function `displayList':
foo.c:138: `System' undeclared (first use in this function)
foo.c:138: (Each undeclared identifier is reported only once
foo.c:138: for each function it appears in.)
foo.c:139: parse error before `current'
make: *** [foo.o] Error 1

I suggest you fix these before continuing.

Incidentally, I compiled this in the UK, but C is defined by an
international standard, so the rules of the language are precisely the same
in Java as they are in the UK.
 
C

CBFalconer

Asembereng said:
Hi i want to do insertion sort, merge sort and quick sort on my
linkList of ints.. do anyone have an idea how to do it??
these are the codes. And i also need to define a lifo fifo class..

class UTGLLElement {
public int myData; // data item

If you closely examine the name of this newsgroup, and polish up
your reading skills, you might just happen to notice the words
"lang.c". That language does not have classes, or publics. Try
comp.lang.c++. They probably also won't answer a homework
question.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
Ad

Advertisements

C

Christopher Benson-Manica

CBFalconer said:
If you closely examine the name of this newsgroup, and polish up
your reading skills, you might just happen to notice the words
"lang.c". That language does not have classes, or publics. Try
comp.lang.c++. They probably also won't answer a homework
question.

A quick look at the OP's subject suggests that comp.lang.java.help or
comp.lang.java.programmer would be more appropriate, although your
last point still applies of course.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top