网站首页 > 文章中心 > 其它

java数据结构实现代码

作者:小编 更新时间:2023-10-17 10:38:46 浏览量:452人看过

用java实现一个数据结构!

import java.io.IOException;

import java.util.Scanner;

public class LinkList {

private static Scanner san = new Scanner(System.in);

public static void main(String[] args) throws IOException {

List list = new List();

java数据结构实现代码-图1

for (int i = 1; i = 10; i++) {

System.out.print("请输入第" + i + "个数: ");

list.add(san.nextInt());

list.print();

}

System.out.println("输入的数据如下: ");

class node {

java数据结构实现代码-图2

int data;

node next = this; // 指向自己

class List {

private node header = new node();

// 循环链表的尾部添加数据

public node add(int data) {

node current = new node();

node temp = header;

while (temp.next != header)

temp = temp.next;

java数据结构实现代码-图3

current.data = data;

current.next = temp.next;

temp.next = current;

return current;

// 查询某个数字的位置 如果不在 返回-1;

public int search(int data) {

int n = 0;

while (temp.next != header) {

n++;

if (temp.data == data)

break;

return n;

else

return -1;

// 打印出整个链表

public void print() {

System.out.print(temp.data + " ");

System.out.println();

// 插入数据

public node Insert(int pos, int data) {

for (int i = 0; i pos - 1; i++) {

if (temp.next != header) {

} else

return null;

// 删除某个数据

public node del(int data) {

node oldtemp = null;

node current = null;

oldtemp = temp;

if (temp.data == data) {

current = temp;

if (current == header)

oldtemp.next = current.next;

关于数据结构(java)的一个代码

描述栈抽象数据类型的SStack接口的声明

public interfaceSStackE //栈接口

{

boolean isEmpty(); //判断是否空栈,若空栈返回true

boolean push(E element); //元素element入栈,若操作成功返回true

E pop(); //出栈,返回当前栈顶元素,若栈空返回null

E get(); //取栈顶元素值,未出栈,若栈空返回null

顺序栈类具体操作方法的声明:

importdataStructure.linearList.SStack;

public classSeqStackE implements SStackE

//顺序栈类

private Object value[]; //存储栈的数据元素

private int top; //top为栈顶元素下标

public SeqStack(int capacity) //构造指定容量的空栈

this.value = newObject[Math.abs(capacity)];

this.top=-1;

public SeqStack() //构造默认容量的空栈

this(10);

public boolean isEmpty() //判断是否空栈,若空栈返回true

return this.top==-1;

public boolean push(E element) //元素element入栈,若操作成功返回true

if (element==null)

return false; //空对象(null)不能入栈

if (this.top==value.length-1) //若栈满,则扩充容量

Object[] temp = this.value;

for (int i=0; itemp.length;i++)

this.value[i] = temp[i];

this.top++;

this.value[this.top] = element;

return true;

public E pop() //出栈,返回当前栈顶元素,若栈空返回null

if (!isEmpty())

return (E)this.value[this.top--];

public E get() //取栈顶元素值,未出栈,栈顶元素未改变

return (E)this.value[this.top];

public String toString() //返回栈中各元素的字符串描述

String str="{";

if (this.top!=-1)

str +=this.value[this.top].toString();

for (int i=this.top-1; i=0; i--)

str += ","+this.value[i].toString();

return str+"} ";

实例引用public static void main(String args[])

System.out.print("Push: ");

char ch='a';

String str =(char)(ch+i)+"";

stack.push(str);

System.out.print(str+" ");

System.out.println("\n"+stack.toString());

System.out.print("Pop : ");

while(!stack.isEmpty()) //全部出栈

System.out.print(stack.pop().toString()+" ");

用Java语言编写数据结构中顺序表的插入删除查找代码并实现

public class Test {

public static void main(String[] args) {

int ai = 1;

String data = "data";

String[] array = insertArrar(data, ai, length);

data = delArray(array, ai, length);

System.out.println(data);

public static String[] insertArrar(String data,int ai,int length){

String[] array = new String[length];

array[ai] = data;

return array;

public static String delArray(String[] array,int ai,int length){

String data = "";

data=array[ai];

array[ai]=null;

for(int i = 0; iarray.length;i++){

System.out.println(array[i]);

return data;

以上就是土嘎嘎小编为大家整理的java数据结构实现代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章