码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构(DataStructure)

时间:2020-02-16 14:55:48      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:after   string   --   bin   swa   中缀表达式   后缀   vector   nal   

数据结构相关基础~~~

(1)数字求和运算

(2)在链表的开头和结尾添加元素

(3)获取链表的第一个和最后一个元素

(4)删除链表中的元素

(5)获取链表的元素

(6)获取向量元素的索引值

(7)栈的实现

(8)链表元素查找

(9)压栈出栈的方法实现字符串反转

(10)队列的用法

(11)获取向量的最大元素

(12)链表修改

(13)旋转向量

  1 import java.io.IOException;
  2 import java.util.Collections;
  3 import java.util.LinkedList;
  4 import java.util.Vector;
  5 import java.util.Queue;
  6 
  7 public class DataControl {
  8     // 数字求和运算
  9     private static void NumberGetAdd() {
 10         int sum = 0;
 11         int limit = 100;
 12         int i = 1;
 13         do {
 14             sum += i;
 15             i++;
 16         } while (i <= limit);
 17         System.out.println("sum:" + sum);
 18     }
 19 
 20 
 21     // 利用堆栈将中缀表达式转化为后缀
 22     private final void UseStackMiddleToAfter() throws IOException {
 23         String input = "1+2*4/5-7+3/6";
 24         String output;
 25         InToPost theTrans = new InToPost(input);
 26         output = theTrans.doTrans();
 27         System.out.println("Postfix is " + output + ‘\n‘);
 28     }
 29 
 30     class InToPost {
 31         private Stack theStack;
 32         private String input;
 33         private String output = "";
 34 
 35         private InToPost(String in) {
 36             input = in;
 37             int stackSize = input.length();
 38             theStack = new Stack(stackSize);
 39         }
 40 
 41         private String doTrans() {
 42             for (int j = 0; j < input.length(); j++) {
 43                 char ch = input.charAt(j);
 44                 switch (ch) {
 45                     case ‘+‘:
 46                     case ‘-‘:
 47                         gotOper(ch, 1);
 48                         break;
 49                     case ‘*‘:
 50                     case ‘/‘:
 51                         gotOper(ch, 2);
 52                         break;
 53                     case ‘(‘:
 54                         theStack.push(ch);
 55                         break;
 56                     case ‘)‘:
 57                         gotParen(ch);
 58                         break;
 59                     default:
 60                         output += ch;
 61                         break;
 62                 }
 63             }
 64             while (!theStack.isEmpty()) {
 65                 output += theStack.pop();
 66             }
 67             System.out.println(output);
 68             return output;
 69         }
 70 
 71         private void gotOper(char opThis, int prec1) {
 72             while (!theStack.isEmpty()) {
 73                 char opTop = theStack.pop();
 74                 if (opTop == ‘(‘) {
 75                     theStack.push(opTop);
 76                     break;
 77                 } else {
 78                     int prec2;
 79                     if (opTop == ‘+‘ || opTop == ‘-‘) prec2 = 1;
 80                     else prec2 = 2;
 81                     if (prec2 < prec1) {
 82                         theStack.push(opTop);
 83                         break;
 84                     } else output += opTop;
 85                 }
 86             }
 87             theStack.push(opThis);
 88         }
 89 
 90         private void gotParen(char ch) {
 91             while (!theStack.isEmpty()) {
 92                 char chx = theStack.pop();
 93                 if (chx == ‘(‘) break;
 94                 else output += chx;
 95             }
 96         }
 97     }
 98 
 99     class Stack {
100         private int maxSize;
101         private char[] stackArray;
102         private int top;
103 
104         private Stack(int max) {
105             maxSize = max;
106             stackArray = new char[maxSize];
107             top = -1;
108         }
109 
110         private void push(char j) {
111             stackArray[++top] = j;
112         }
113 
114         private char pop() {
115             return stackArray[top--];
116         }
117 
118         public char peek() {
119             return stackArray[top];
120         }
121 
122         private boolean isEmpty() {
123             return (top == -1);
124         }
125     }
126 
127 
128     // 在链表的开头和结尾添加元素
129     private static void AddDataInLinkedListStartEnd() {
130         LinkedList lList = new LinkedList();
131         lList.add("1");
132         lList.add("2");
133         lList.add("3");
134         lList.add("4");
135         lList.add("5");
136         System.out.println(lList);
137         lList.addFirst("0");
138         System.out.println(lList);
139         lList.addLast("6");
140         System.out.println(lList);
141     }
142 
143 
144     // 获取链表的第一个和最后一个元素
145     private static void GetFirstLastDataOfLinkedList() {
146         LinkedList l = new LinkedList();
147         l.add("100");
148         l.add("200");
149         l.add("300");
150         l.add("400");
151         l.add("500");
152         System.out.println("链表第一个元素是:" + l.getFirst());
153         System.out.println("链表最后一个元素是:" + l.getLast());
154     }
155 
156 
157     // 删除链表中的元素
158     private static void DeleteDataInLinkedList() {
159         LinkedList<String> lList = new LinkedList<String>();
160         lList.add("1");
161         lList.add("8");
162         lList.add("6");
163         lList.add("4");
164         lList.add("5");
165         System.out.println(lList);
166         lList.subList(2, 4).clear();
167         System.out.println(lList);
168     }
169 
170 
171     // 获取链表的元素
172     private static void GetLinkedListFactor() {
173         Test1 stack = new Test1();
174         for (int i = 30; i < 40; i++)
175             stack.push(new Integer(i));
176         System.out.println(stack.top());
177         System.out.println(stack.pop());
178         System.out.println(stack.pop());
179         System.out.println(stack.pop());
180     }
181 
182     public static class Test1 {
183         private LinkedList list = new LinkedList();
184 
185         public void push(Object v) {
186             list.addFirst(v);
187         }
188 
189         public Object top() {
190             return list.getFirst();
191         }
192 
193         public Object pop() {
194             return list.removeFirst();
195         }
196     }
197 
198 
199     // 获取向量元素的索引值
200     private static void GetVectorIndex() {
201         Vector v = new Vector();
202         v.add("X");
203         v.add("M");
204         v.add("D");
205         v.add("A");
206         v.add("O");
207         Collections.sort(v);
208         System.out.println(v);
209         int index = Collections.binarySearch(v, "D");
210         System.out.println("元素索引值为:" + index);
211     }
212 
213 
214     // 栈的实现
215     private static void AchievedStack() {
216         MyStack theStack = new MyStack(10);
217         theStack.push(10);
218         theStack.push(20);
219         theStack.push(30);
220         theStack.push(40);
221         theStack.push(50);
222         while (!theStack.isEmpty()) {
223             long value = theStack.pop();
224             System.out.print(value);
225             System.out.print(" ");
226         }
227         System.out.println("");
228     }
229 
230     static class MyStack {
231         private int maxSize;
232         private long[] stackArray;
233         private int top;
234 
235         public MyStack(int s) {
236             maxSize = s;
237             stackArray = new long[maxSize];
238             top = -1;
239         }
240 
241         public void push(long j) {
242             stackArray[++top] = j;
243         }
244 
245         public long pop() {
246             return stackArray[top--];
247         }
248 
249         public long peek() {
250             return stackArray[top];
251         }
252 
253         public boolean isEmpty() {
254             return (top == -1);
255         }
256 
257         public boolean isFull() {
258             return (top == maxSize - 1);
259         }
260     }
261 
262 
263     // 链表元素查找
264     private static void SearchLinkedListFactor() {
265         LinkedList lList = new LinkedList();
266         lList.add("1");
267         lList.add("2");
268         lList.add("3");
269         lList.add("4");
270         lList.add("5");
271         lList.add("2");
272         System.out.println("元素 2 第一次出现的位置:" + lList.indexOf("2"));
273         System.out.println("元素 2 最后一次出现的位置:" + lList.lastIndexOf("2"));
274     }
275 
276 
277     // 压栈出栈的方法实现字符串反转
278     private static void UseStackAchieveStringReverse() throws IOException {
279         String input = "www.w3cschool.cc";
280         String output;
281         StringReverserThroughStack theReverser =
282                 new StringReverserThroughStack(input);
283         output = theReverser.doRev();
284         System.out.println("反转前: " + input);
285         System.out.println("反转后: " + output);
286     }
287 
288     static class StringReverserThroughStack {
289         private String input;
290         private String output;
291 
292         public StringReverserThroughStack(String in) {
293             input = in;
294         }
295 
296         public String doRev() {
297             int stackSize = input.length();
298             Stack theStack = new Stack(stackSize);
299             for (int i = 0; i < input.length(); i++) {
300                 char ch = input.charAt(i);
301                 theStack.push(ch);
302             }
303             output = "";
304             while (!theStack.isEmpty()) {
305                 char ch = theStack.pop();
306                 output = output + ch;
307             }
308             return output;
309         }
310 
311         class Stack {
312             private int maxSize;
313             private char[] stackArray;
314             private int top;
315 
316             public Stack(int max) {
317                 maxSize = max;
318                 stackArray = new char[maxSize];
319                 top = -1;
320             }
321 
322             public void push(char j) {
323                 stackArray[++top] = j;
324             }
325 
326             public char pop() {
327                 return stackArray[top--];
328             }
329 
330             public char peek() {
331                 return stackArray[top];
332             }
333 
334             public boolean isEmpty() {
335                 return (top == -1);
336             }
337         }
338     }
339 
340     // 队列的用法
341     private static void QueueFunction() {
342         Queue<String> queue = new LinkedList<String>();
343         //添加元素
344         queue.offer("a");
345         queue.offer("b");
346         queue.offer("c");
347         queue.offer("d");
348         queue.offer("e");
349         for (String q : queue) {
350             System.out.println(q);
351         }
352         System.out.println("===");
353         System.out.println("poll=" + queue.poll()); //返回第一个元素,并在队列中删除
354         for (String q : queue) {
355             System.out.println(q);
356         }
357         System.out.println("===");
358         System.out.println("element=" + queue.element()); //返回第一个元素
359         for (String q : queue) {
360             System.out.println(q);
361         }
362         System.out.println("===");
363         System.out.println("peek=" + queue.peek());
364         for (String s : queue) {
365             System.out.println(s);
366         }
367     }
368 
369 
370     // 获取向量最大元素
371     private static void GetVectorMax() {
372         Vector v = new Vector();
373         v.add(new Double("3.4324"));
374         v.add(new Double("3.3532"));
375         v.add(new Double("3.342"));
376         v.add(new Double("3.349"));
377         v.add(new Double("2.3"));
378         Object obj = Collections.max(v);
379         System.out.println("最大元素是:" + obj);
380     }
381 
382 
383     // 链表修改
384     private static void LinkedListChanged() {
385         LinkedList officers = new LinkedList();
386         officers.add("B");
387         officers.add("B");
388         officers.add("T");
389         officers.add("H");
390         officers.add("P");
391         System.out.println(officers);
392         officers.set(2, "M");
393         System.out.println(officers);
394     }
395 
396 
397     // 旋转向量
398     private static void RotateVector() {
399         Vector<String> v = new Vector();
400         v.add("1");
401         v.add("2");
402         v.add("3");
403         v.add("4");
404         v.add("5");
405         System.out.println(v);
406         Collections.swap(v, 0, 4);
407         System.out.println("旋转后");
408         System.out.println(v);
409     }
410 
411     public static void main(String[] args) throws IOException {
412         NumberGetAdd();
413         AddDataInLinkedListStartEnd();
414         GetFirstLastDataOfLinkedList();
415         DeleteDataInLinkedList();
416         GetLinkedListFactor();
417         GetVectorIndex();
418         AchievedStack();
419         SearchLinkedListFactor();
420         UseStackAchieveStringReverse();
421         QueueFunction();
422         GetVectorMax();
423         LinkedListChanged();
424         RotateVector();
425     }
426 
427 }

 

数据结构(DataStructure)

标签:after   string   --   bin   swa   中缀表达式   后缀   vector   nal   

原文地址:https://www.cnblogs.com/skygrass0531/p/12316654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!