473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

building linklist in java

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

Jan 20 '07 #1
5 2027
Asembereng wrote:
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!
Jan 20 '07 #2
Asembereng wrote:
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.

Jan 20 '07 #3
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 20 '07 #4
Asembereng wrote:
>
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

Jan 20 '07 #5
CBFalconer <cb********@yahoo.comwrote:
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.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jan 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jova | last post by:
I'm stuck I have to convert an array to a linklist the array is initialized as public Organization{ Crew members = new Member etc....
7
by: vjay | last post by:
I want to just create a linklist.The program below goes into an endless loop.The srange behaviour is that i can exit from the program if i create only two nodes.After two goes into infinite loop. ...
6
by: jwvai316 | last post by:
I don't really know how to say it so I just say it a nested linklist. How do you make LinkLists inside LinkList? Can anyone help me for this? I think an example program will help me a lot. ...
7
by: Hal Vaughan | last post by:
I have a problem with port forwarding and I have been working on it for over 2 weeks with no luck. I have found C programs that almost work and Java programs that almost work, but nothing that...
0
by: prashant0903 | last post by:
how i 'll made the linklist program in java both included insertion & deletion in program
2
by: zubia | last post by:
hi how 2 deal with the rptr n lptr in doubly linklist
1
by: smoothkriminal1 | last post by:
Write a Code that will pick timetable of 40 students from file using linklist n than find a slot where all the students dont have any class. file can be of any format Student can maximum take 6...
2
Parul Bagadia
by: Parul Bagadia | last post by:
I have written a code for deleting certain value from linklist; it's not working; where as i have written one for deleting a no., after given no. which works fine! I even debugged it; but invain;...
2
by: dynamo | last post by:
this is a basic linklist,there seems to be a runtime error when i run the program(the main function) i suspect it has something to do with my del function but i see nothing wrong can you help.Thanx...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.