[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.0

How do I implement a stack from my already existing linked list?

Asked by datopdogg7 in Java Programming Language

Tags: JAVA list

Hi experts, the below code is of a single linked list.
I need to implement a stack from this very code.
Can you please tell me what I need to add and change in this code to make it a stack class?
Please no references to other websites, I have already looked, and I would like to make use of my existing code.
Thanks,
Dennis
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
package stacks_queues;
 
 
public class LinkedList {
    
    // Make variables private
 
    private Node head;
    private int listCount;
 
        // Constructor for the class
    
        public LinkedList()
        {
                // this is an empty list, so the reference to the head node
                // is set to a new node with no data
                head = new Node(null);
                listCount = 0;
        }
 
        // This method appends the word to the end of this list or in between two words in doing so making the list alphabetic
        
        public void add(Word data)
 
        {
                Node temp = new Node(data);
                Node current = head;
                
                // Crawl to the end of the list, starting from the head node
 
                while(current.getNext() != null && current.getNext().getData().getWord().compareTo(data.getWord())<0)
                {
                        current = current.getNext();
                }
                
                // Make the new node's reference equal to the last node's "next" reference 
 
                temp.setNext(current.getNext());
                current.setNext(temp);
                
                // Increment the number of word objects in the list
                
                listCount++;
        }
 
        // Inserts the word at the specified position in this list.        
        
        public void add(Word data, int index)
 
        {
                Node temp = new Node(data);
                Node current = head;
                // crawl to the requested index or the last element in the list,
                // whichever comes first
                for(int i = 1; i < index && current.getNext() != null; i++)
                {
                        current = current.getNext();
                }
                // set the new node's next-node reference to this node's next-node reference
                temp.setNext(current.getNext());
                // now set this node's next-node reference to the new node
                current.setNext(temp);
                listCount++;// increment the number of elements variable
        }
 
        public Word get(int index)
        // post: returns the element at the specified position in this list.
        {
                // index must be 1 or higher
                if(index <= 0)
                        return null;
 
                Node current = head.getNext();
                for(int i = 1; i < index; i++)
                {
                        if(current.getNext() == null)
                                return null;
 
                        current = current.getNext();
                }
                return current.getData();
        }
 
        public boolean remove(int index)
        // post: removes the element at the specified position in this list.
        {
                // if the index is out of range, exit
                if(index < 1 || index > size())
                        return false;
 
                Node current = head;
                for(int i = 1; i < index; i++)
                {
                        if(current.getNext() == null)
                                return false;
 
                        current = current.getNext();
                }
                current.setNext(current.getNext().getNext());
                listCount--; // decrement the number of elements variable
                return true;
        }
 
        public int size()
        // post: returns the number of elements in this list.
        {
                return listCount;
        }
 
        public Word returnWord(String user_word){
 
            Word word = new Word();
            Node current = head.getNext();
 
            for(int i = 1; i < listCount; i++)
            {
                    current = current.getNext();
 
                    if(current.getData().getWord().compareTo(user_word) == 0){
 
                    break;
 
                    }
 
            }
 
            return current.getData();
 
        }
 
        public boolean checkWord(String user_word){
 
            Word word = new Word();
            Node current = head.getNext();
            boolean answer = false;
            int i = 1;
 
            if(listCount != 0){
 
                current = current.getNext();
 
                do
                {
                        current = current.getNext();
 
                        if(current.getData().getWord().compareTo(user_word) == 0){
 
                            answer = true;
 
                        }
 
                        else{
 
                            answer = false;
 
                        }
 
                }while(i < listCount);
            }
 
            return answer;
 
        }
 
        private class Node
        {
                // reference to the next node in the chain,
                // or null if there isn't one.
                Node next;
                // data carried by this node.
                // could be of any type you need.
                Word data;
 
 
                // Node constructor
                public Node(Word _data)
                {
                        next = null;
                        data = _data;
                }
 
                // another Node constructor if we want to
                // specify the node to point to.
                public Node(Word _data, Node _next)
                {
                        next = _next;
                        data = _data;
                }
 
                // these methods should be self-explanatory
                public Word getData()
                {
                        return data;
                }
 
                public void setData(Word _data)
                {
                        data = _data;
                }
 
                public Node getNext()
                {
                        return next;
                }
 
                public void setNext(Node _next)
                {
                        next = _next;
                }
        }
 
 
}
 
Loading Advertisement...
 
[+][-]02/04/09 06:43 PM, ID: 23556005Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/04/09 06:45 PM, ID: 23556022Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/04/09 10:28 PM, ID: 23556989Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/04/09 10:30 PM, ID: 23556994Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/04/09 10:39 PM, ID: 23557022Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/04/09 10:40 PM, ID: 23557024Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/04/09 10:43 PM, ID: 23557036Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Java Programming Language
Tags: JAVA list
Sign Up Now!
Solution Provided By: objects
Participating Experts: 1
Solution Grade: A
 
[+][-]02/04/09 10:44 PM, ID: 23557041Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625