您的位置:首页 > 理论基础 > 数据结构算法

Java中链表、堆栈、队列、二叉树、散列表等数据结构的实现

2014-07-02 10:08 856 查看
转自:http://blog.csdn.net/meraker/article/details/6116191

温习数据结构:Java中链表、堆栈、队列、二叉树、散列表等数据结构的实现

1.Java链表类List的源代码如下:

import java.io.*;

public class List

{

 /*用变量来实现表头*/

 private Node Head=null;

 private Node Tail=null;

 private Node Pointer=null;

 private int Length=0;

 public void deleteAll()

 /*清空整个链表*/

 {

  Head=null;

  Tail=null;

  Pointer=null;

  Length=0;

 }

 public void reset()

 /*链表复位,使第一个结点成为当前结点*/

 {

  Pointer=null;

 }

 public boolean isEmpty()

 /*判断链表是否为空*/

 {

  return(Length==0);

 }

 public boolean isEnd()

 /*判断当前结点是否为最后一个结点*/

 {

  if(Length==0)

   throw new java.lang.NullPointerException();

  else if(Length==1)

   return true;

  else

   return(cursor()==Tail);

 }

 public Object nextNode()

 /*返回当前结点的下一个结点的值,并使其成为当前结点*/

 {

  if(Length==1)

   throw new java.util.NoSuchElementException();

  else if(Length==0)

   throw new java.lang.NullPointerException();

  else

  {

   Node temp=cursor();

   Pointer=temp;

   if(temp!=Tail)

    return(temp.next.data);

   else

    throw new java.util.NoSuchElementException();

  }

 }

 public Object currentNode()

 /*返回当前结点的值*/

 {

  Node temp=cursor();

  return temp.data;

 }

  

 public void insert(Object d)

 /*在当前结点前插入一个结点,并使其成为当前结点*/

 {

  Node e=new Node(d);

  if(Length==0)

  {

   Tail=e;

   Head=e;

  }

  else

  {

   Node temp=cursor();

   e.next=temp;

   if(Pointer==null)

    Head=e;

   else

    Pointer.next=e;

  }

  Length++;

 }

 public int size()

 /*返回链表的大小*/

 {

  return (Length);

 }

 public Object remove()

 /*将当前结点移出链表,下一个结点成为当前结点,如果移出的结点是最后一个结点,则第一个结点成为当前结点*/

 {

  Object temp;

  if(Length==0)

   throw new java.util.NoSuchElementException();

  else if(Length==1)

  {

   temp=Head.data;

   deleteAll();

  }

  else

  {

   Node cur=cursor();

   temp=cur.data;

   if(cur==Head)

    Head=cur.next;

   else if(cur==Tail)

   {

    Pointer.next=null;

    Tail=Pointer;

    reset();

   }

   else

    Pointer.next=cur.next;

    Length--;

  }

  return temp;

 }

 private Node cursor()

 /*返回当前结点的指针*/

 {

  if(Head==null)

   throw new java.lang.NullPointerException();

  else if(Pointer==null)

   return Head;

  else

   return Pointer.next;

 }

 public static void main(String[] args)

 /*链表的简单应用举例*/

 {

  List a=new List ();

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

   a.insert(new Integer(i));

   System.out.println(a.currentNode());

   while(!a.isEnd())

    System.out.println(a.nextNode());

    a.reset();

    while(!a.isEnd())

    {

     a.remove();

    }

    a.remove();

    a.reset();

    if(a.isEmpty())

     System.out.println("There is no Node in List /n");

     System.in.println("You can press return to quit/n");

    try

    {

     System.in.read();

     //确保用户看清程序运行结果

    }

    catch(IOException e)

    {}

   }

  }

  class Node

  /*构成链表的结点定义*/

  {

   Object data;

   Node next;

   Node(Object d)

   {

    data=d;

    next=null;

   }

  }

  读者还可以根据实际需要定义新的方法来对链表进行操作。双向链表可以用类似的方法实现只是结点的类增加了一个指向前趋结点的指针。

  可以用这样的代码来实现:

class Node

{

 Object data;

 Node next;

 Node previous;

 Node(Object d)

 {

  data=d;

  next=null;

  previous=null;

 }

}

  当然,双向链表基本操作的实现略有不同。链表和双向链表的实现方法,也可以用在堆栈和队列的实现中,这里就不再多写了,有兴趣的读者可以将List类的代码稍加改动即可。

2..Java堆栈算法的实现

import java.io.*;

public class Stacki implements Serializable

{ int index=-1,size;

Object object[],o;

boolean isEmpty;

public &nsp; Stacki()

{ this.size=10;

}

public Stacki(int size)

{ if(size<=0)

{ System.out.println("堆栈初始大小错误");

return;

}

this.size=size;

object=new Object[size];

}

public void push(Object obj)

{ if(++index!=size)

{ object[index]=obj;

}

else

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

{ object[i]=object[i+1];

}

if(index--==0) index=0;

object[index]=obj;

}

}

public Object pop()

{ if(index!=0)

{ o=object[index];

object[index--]=null;

}

else

{ o=object[0];

object[0]=null;

}

return o;

}

public boolean isEmpty()

{ isEmpty=false;

if(object[0]==null)

isEmpty=true;

return isEmpty;

}

public Object peek()

{ return object[index];

}

}

3.Java队列算法的实现

class QueueApp

{

public static void main(String[] args)

{

Queue theQueue = new Queue(5); // queue holds 5 items

theQueue.insert(10); // insert 4 items

theQueue.insert(20);

theQueue.insert(30);

theQueue.insert(40);

theQueue.remove(); // remove 3 items

theQueue.remove(); // (10, 20, 30)

theQueue.remove();

theQueue.insert(50); // insert 4 more items

theQueue.insert(60); // (wraps around)

theQueue.insert(70);

theQueue.insert(80);

while( !theQueue.isEmpty() ) // remove and display

{ // all items

long n = theQueue.remove(); // (40, 50, 60, 70, 80)

System.out.print(n);

System.out.print(" ");

}

System.out.println("");

} // end main()

} // end class QueueApp

class Queue

{

private int maxSize;

private long[] queArray;

private int front;

private int rear;

private int nItems;

//--------------------------------------------------------------

public Queue(int s) // constructor

{

maxSize = s;

queArray = new long[maxSize];

front = 0;

rear = -1;

nItems = 0;

}

//--------------------------------------------------------------

public void insert(long j) // put item at rear of queue

{

if(rear == maxSize-1) // deal with wraparound

&nbs; rear = -1;

queArray[++rear] = j; // increment rear and insert

nItems++; // one more item

}

//--------------------------------------------------------------

public long remove() // take item from front of queue

{

long temp = queArray[front++]; // get value and incr front

if(front == maxSize) // deal with wraparound

front = 0;

nItems--; // one less item

return temp;

}

//--------------------------------------------------------------

public long peekFront() // peek at front of queue

{

return queArray[front];

}

//--------------------------------------------------------------

public boolean isEmpty() // true if queue is empty

{

return (nItems==0);

}

//--------------------------------------------------------------

public boolean isFull() // true if queue is full

{

return (nItems==maxSize);

}

//--------------------------------------------------------------

public int size() // number of items in queue

{

return nItems;

}

//--------------------------------------------------------------

} // end class Queue

4.JAVA中实现二叉树

前序遍历的规则如下:

若二叉树为空,则退出。否则

⑴访问处理根结点;

⑵前序遍历左子树;

⑶前序遍历右子树;

特点:由左而右逐条访问由根出发的树支 (回溯法的基础)

中序遍历的规则:

若二叉树为空,则退出;否则

⑴中序遍历左子树;

⑵访问处理根结点;

⑶中序遍历右子树;

后序遍历的规则如下:

若二叉树为空,则退出;否则

⑴后序遍历左子树;

⑵后序遍历右子树;

⑶访问处理根结点;

/**

*

* 在JAVA中实现二叉树结构

*

* 讲解:

* 二个方法函数,一个寻找关键字--searchkey 另一个是插入一个结点:insertTree

* 另外这是一个完全的先序遍历二叉树的语法。先根结点,再左结点,如无再右结点, * 如此递归至搜索完毕。

*

*/

public class BinaryTreeTest {

private BinaryTree root = null;

public BinaryTreeTest() {

init();

}

/**

* 初始化给定数据的二叉树结构

*

*/

private void init() {

int data[] = { 12, 11, 34, 45, 67, 38, 56, 43, 22, 8 };

root = new BinaryTree(data[0]);

System.out.println("二叉树的中的数据结构:");

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

System.out.println(data[0] + ":root");

for (int i = 1; i < data.length; i++) {

System.out.print(data[i] + ":");

root.insertTree(root, data[i]);

}

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

}

public void serach(int key) {

if (searchkey(root, key)) {

System.out.println("找到了:" + key);

} else {

System.out.println("没有找到:" + key);

}

}

private boolean searchkey(BinaryTree root, int key) {

if (root == null) {

return false;

} else if (root.data == key) {

return true;

} else if (key >= root.data) {

return searchkey(root.rightpoiter, key);

}

return searchkey(root.leftpoiter, key);

}

class BinaryTree {

int data;

BinaryTree leftpoiter;

BinaryTree rightpoiter;

BinaryTree(int data) {

this.data = data;

leftpoiter = null;

rightpoiter = null;

}

private void insertTree(BinaryTree root, int data) {

if (data >= root.data) {

if (root.rightpoiter == null) {

System.out.println(" -> new rightpoiter");

root.rightpoiter = new BinaryTree(data);

} else {

System.out.print(" -> rightpoiter");

insertTree(root.rightpoiter, data);

}

} else {

if (root.leftpoiter == null) {

System.out.println(" -> new leftpoiter");

root.leftpoiter = new BinaryTree(data);

} else {

System.out.print("
b7a9
-> leftpoiter");

insertTree(root.leftpoiter, data);

}

}

}

}

public static void main(String args[]) {

BinaryTreeTest b = new BinaryTreeTest();

int key = 8; //key:任意数值

b.serach(key); //到二叉树中查找

}

}

运行结果:

C:/java>java BinaryTreeTest二叉树的中的数据结构:------------------------------------12:root11: -> new leftpoiter34: -> new rightpoiter45: -> rightpoiter -> new rightpoiter67: -> rightpoiter -> rightpoiter -> new rightpoiter38: -> rightpoiter -> rightpoiter -> new
leftpoiter56: -> rightpoiter -> rightpoiter -> rightpoiter -> new leftpoiter43: -> rightpoiter -> rightpoiter -> leftpoiter -> new rightpoiter22: -> rightpoiter -> new leftpoiter8: -> leftpoiter -> new leftpoiter------------------------------------找到了:8

5.温习数据结构:散列表的应用

散列表,又称为哈希表,是线性表中一种重要的存储方式和检索方法。在散列表中,可以对节点进行快速检索。散列表算法的基本思想是:由结点的关键码值决定结点的存储地址,即以关键码值k为自变量,通过一定的函数关系h(称为散列函数),计算出对应的函数值h(k)来,将这个值解释为结点的存储地址,将结点存入该地址中,检索时,根据要检索的关键码值,用同样的散列函数计算出地址,然后,到相应的地址中去获取要找的结点数据。因此,散列表有一个重要特征:平均检索的长度不直接依赖于表中元素的个数。

散列表设计与实现程序

【问题描述】

设计散列表实现电话号码查找系统。

【基本要求】

1) 设每个记录有下列数据项:电话号码、用户名、地址;

2) 从键盘输入各记录,分别以电话号码和用户名为关键字建立散列表;

3) 采用一定的方法解决冲突;

4) 查找并显示给定电话号码的记录;

5) 查找并显示给定用户名的记录。

6) 用c或者c++编写

代码如下:

#include "iostream.h"

#include "string.h"

#include "fstream.h"

#define NULL 0

unsigned int key;

unsigned int key2;

int *p;

struct node //建节点

{

char name[8],address[20]; char num[11];

node * next;

};

typedef node* pnode;

typedef node* mingzi;

node **phone;

node **nam;

node *a;


void hash(char num[11]) //哈希函数

{

int i = 3;

key=(int)num[2];

while(num[i]!=NULL)

{

key+=(int)num[i];

i++;

}

key=key%20;

}

void hash2(char name[8]) //哈希函数

{

int i = 1;

key2=(int)name[0];

while(name[i]!=NULL)

{

key2+=(int)name[i];

i++;

}

key2=key2%20;

}

node* input() //输入节点

{

node *temp;

temp = new node;

temp->next=NULL;

cout<<"输入姓名:"<<endl;

cin>>temp->name;

cout<<"输入地址:"<<endl;

cin>>temp->address;

cout<<"输入电话:"<<endl;

cin>>temp->num;

return temp;

}

int apend() //添加节点

{

node *newphone;

node *newname;

newphone=input();

newname=newphone;

newphone->next=NULL;

newname->next=NULL;

hash(newphone->num);

hash2(newname->name);

newphone->next = phone[key]->next;

phone[key]->next=newphone;

newname->next = nam[key2]->next;

nam[key2]->next=newname;

return 0;

}

void create() //新建节点

{

int i;

phone=new pnode[20];

for(i=0;i<20;i++)

{

phone[i]=new node;

phone[i]->next=NULL;

}

}

void create2() //新建节点

{

int i;

nam=new mingzi[20];

for(i=0;i<20;i++)

{

nam[i]=new node;

nam[i]->next=NULL;

}

}

void list() //显示列表

{

int i;

node *p;

for(i=0;i<20;i++)

{

p=phone[i]->next;

while(p)

{

cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;

p=p->next;

}

}

}

void list2() //显示列表

{

int i;

node *p;

for(i=0;i<20;i++)

{

p=nam[i]->next;

while(p)

{

cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;

p=p->next;

}

}

}

void find(char num[11]) //查找用户信息

{

hash(num);

node *q=phone[key]->next;

while(q!= NULL)

{

if(strcmp(num,q->num)==0)

break;

q=q->next;

}

if(q)

cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;

else cout<<"无此记录"<<endl;

}

void find2(char name[8]) //查找用户信息

{

hash2(name);

node *q=nam[key2]->next;

while(q!= NULL)

{

if(strcmp(name,q->name)==0)

break;

q=q->next;

}

if(q)

cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;

else cout<<"无此记录"<<endl;

}

void save() //保存用户信息

{

int i;

node *p;

for(i=0;i<20;i++)

{

p=phone[i]->next;

while(p)

{

fstream iiout("out.txt", ios::out);

iiout<<p->name<<"_"<<p->address<<"_"<<p->num<<endl;

p=p->next;

}

}

}

void menu() //菜单

{

cout<<"0.添加记录"<<endl;

cout<<"3.查找记录"<<endl;

cout<<"2.姓名散列"<<endl;

cout<<"4.号码散列"<<endl;

cout<<"5.清空记录"<<endl;

cout<<"6.保存记录"<<endl;

cout<<"7.退出系统"<<endl;

}

int main()

{

char num[11];

char name[8];

create();

create2() ;

int sel;

while(1)

{

menu();

cin>>sel;

if(sel==3)

{ cout<<"9号码查询,8姓名查询"<<endl;

int b;

cin>>b;

if(b==9)

{cout<<"请输入电话号码:"<<endl;

cin >>num;

cout<<"输出查找的信息:"<<endl;

find(num);

}

else

{cout<<"请输入姓名:"<<endl;

cin >>name;

cout<<"输出查找的信息:"<<endl;

find2(name);}}

if(sel==2)

{cout<<"姓名散列结果:"<<endl;

list2();}

if(sel==0)

{cout<<"请输入要添加的内容:"<<endl;

apend();}

if(sel==4)

{cout<<"号码散列结果:"<<endl;

list();

}

if(sel==5)

{cout<<"列表已清空:"<<endl;

create();create2();}

if(sel==6)

{ cout<<"通信录已保存:"<<endl;

save();}

if(sel==7) return 0;

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息