「数据结构java代码」JAVA基本数据结构

博主:adminadmin 2022-11-30 00:12:10 44

本篇文章给大家谈谈数据结构java代码,以及JAVA基本数据结构对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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

public class Test {

public static void main(String[] args) {

int length = 5;

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)的一个代码

描述栈抽象数据类型的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;

this.value = newObject[temp.length*2];

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--];

else

return null;

}

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

{

if (!isEmpty())

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

else

return null;

}

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[])

{

SeqStackString stack = newSeqStackString(20);

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

char ch='a';

for(int i=0;i5;i++)

{

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()+" ");

System.out.println();

}

用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();

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

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

list.add(san.nextInt());

list.print();

}

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

list.print();

}

}

class node {

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;

current.data = data;

current.next = temp.next;

temp.next = current;

return current;

}

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

public int search(int data) {

node temp = header;

int n = 0;

while (temp.next != header) {

temp = temp.next;

n++;

if (temp.data == data)

break;

}

if (temp.data == data)

return n;

else

return -1;

}

// 打印出整个链表

public void print() {

node temp = header;

while (temp.next != header) {

temp = temp.next;

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

}

System.out.println();

}

// 插入数据

public node Insert(int pos, int data) {

node temp = header;

node current = new node();

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

if (temp.next != header) {

temp = temp.next;

} else

return null;

}

current.data = data;

if (temp.next != header) {

current.next = temp.next;

}

temp.next = current;

return current;

}

// 删除某个数据

public node del(int data) {

node temp = header;

node oldtemp = null;

node current = null;

while (temp.next != header) {

oldtemp = temp;

temp = temp.next;

if (temp.data == data) {

current = temp;

break;

}

}

if (current == header)

return null;

oldtemp.next = current.next;

return current;

}

}

用java实现数据结构“栈

Java栈的实现

public class MyStack { //定义一个堆栈类

int[] array; //用int数组来保存数据,根据需要可以换类型

int s_size; //定义堆栈的宽度

public MyStack(int i){ //定义一个带参数构造器

array=new int[i]; //动态定义数组的长度

s_size=0; //堆栈的默认宽度为0

}

public MyStack(){ //默认构造器

this(50); //默认构造器可容纳50个元素

}

public void push(int i){ //压栈

array[this.s_size]=i;

this.s_size++;

}

public int pop(){ //从堆栈中取元素,从栈顶开始取

if(this.s_size!=0){

int t=array[s_size-1]; //用中间变量保存栈顶的元素

array[s_size-1]=0; //取完元素该位置设为0

s_size--; //栈的大小减1

return t; //返回栈顶元素

}else{

System.out.println("This stack is empty"); //当栈为空时显示提示信息,返回0

return 0;

}

}

public boolean isEmpty(){ //判断栈是否为空

return this.s_size==0;

}

public int top(){ //从栈顶取值,功能和 pop() 方法一样

if(!this.isEmpty()){

int t=array[this.s_size-1];

array[this.s_size-1]=0;

this.s_size--;

return t;

}else{

System.out.println("This stack is empty!");

return 0;

}

}

public void printAll(){ //打印出堆栈中的所有元素的值,不是取出,元素依然在堆栈里

if(!this.isEmpty()){

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

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

}

}

}

//下面是测试代码

public static void main(String[] args){

MyStack stack=new MyStack();

stack.push(4);

stack.push(5);

stack.push(6);

stack.push(7);

//System.out.println(stack.isEmpty());

stack.printAll();

System.out.println("===========");

System.out.println(stack.top());

System.out.println(stack.top());

System.out.println(stack.top());

System.out.println(stack.top());

System.out.println(stack.top());

}

}

数据结构(java版)

package game24.datastructure.list;

/**

 * 链表的结点

 * @author luoweifu

 *

 */

class Node{

Object data; //数据元素

Node next; //后驱结点

public Node() {

this(null);

}

public Node(Object data) {

this.data = data;

this.next = null;

}

}

/**

 * 带头结点的链式链表,下标从0开始; 

 * @author Administrator

 *

 */

public class SinglyLinkedListE{

Node head; //头结点

int size; //链表的大小

public SinglyLinkedList() {

head = new Node();

size = 0;

}

public SinglyLinkedList(E[] datas) {

int n = datas.length;

head = new Node();

Node p = head;

for(int i=0; in; i++) {

p.next = new Node(datas[i]);

p = p.next;

}

size = n;

}

public SinglyLinkedList(SinglyLinkedList list) {

head = list.head;

size = list.size();

}

public void add(Object e) {

Node p;

if(0 == size) {

p = head;

} else {

p = index(size-1);

}

p.next = new Node(e);

size ++;

}

public void concat(SinglyLinkedList list) {

Node lastNode = this.index(size - 1);

lastNode.next = list.index(0);

size += list.size();

}

public void clear() {

head.next = null;

size = 0;

}

public Object get(int i) {

Node p = index(i);

return p.data;

}

private Node index(int i) {

Node p = null;

if(i=0  isize){

p = head;

for(int j=0; j=i; j++) {

p = p.next;

}

return p;

}

public int indexOf(Object e) {

Node p = head.next;

int i = 0;

while(!p.data.equals(e)) {

p = p.next;

i++;

}

if(isize)

return i;

else 

return -1;

}

public void insert(int i, Object e) {

Node p = index(i);

Node p2 = new Node(e);

p2.next = p.next;

p.next = p2;

size ++;

}

public boolean isEmpty() {

if(size ==0)

return true;

else

return false;

}

public int lastIndexOf(Object e) {

int i = size-1;

while(!get(i).equals(e)) {

i--;

}

if(i=0)

return i;

else 

return -1;

}

public void remove(int i) {

if(i=0  isize) {

Node p = null;

if(i == 0)

p = head;

else {

p = index(i-1);

}

p.next = index(i).next;

}

size --;

}

public void set(int i, Object e) {

Node p = index(i);

p.data = e;

}

public int size() {

return size; 

}

@Override

public boolean equals(Object obj) {

SinglyLinkedList list = (SinglyLinkedList)obj;

if(this == obj  size == list.size) {

return true;

}

return false;

}

/**

 * 测试线性表

 * @param args

 */

public static void main(String args[]) {

//List list = new LinkList();

//List list = new DoubleLinkList();

SinglyLinkedList list1 = new SinglyLinkedList();

for(int i=0; i10; i++) {

list1.add(new Integer(i));

}

Integer [] a = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110};

SinglyLinkedList list = new SinglyLinkedList(a);

list.remove(9);

System.out.print("size:" + list.size() + "\n");

System.out.println("isEmpty:" + list.isEmpty());

System.out.print("第7个位置的元素:" + list.get(7) + "\n");

list.concat(list1);

for(int i=0; ilist.size(); i++) {

System.out.print(list.get(i) + "    ");

}

list.add(21);

list.add(22);

list.insert(3, new Integer(5));

System.out.print("size:" + list.size() + "\n");

System.out.print("第一次出现5的索引:" + list.indexOf(5) + "\n");

System.out.print("最后一次出现5的索引:" + list.lastIndexOf(5) + "\n");

list.set(0, new Integer(30));

for(int i=0; ilist.size(); i++) {

System.out.print(list.get(i) + "    ");

}

SinglyLinkedList list2 = list;

System.out.println("\n is equels?  " + list2.equals(list));

}

}

数据结构java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于JAVA基本数据结构、数据结构java代码的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-30,除非注明,否则均为首码项目网原创文章,转载请注明出处。