字典 java_字典丶Java教程网-IT开发者们的技术天堂

在这之前,先说一个概念:字典。

字典(dictionary)是由一些形如(key,value)的数对所组成的集合,其中key是关键字,value是与关键字key对应的值(另一种说法是,value是值,这个值所对应的关键字就是key)。任意两个数对,其关键字都不等。有关字典的一些基本操作如下:

确定字典是否为空

确定字典有多少数对

寻找一个指定了关键字的数对

插入一个数对

删除一个指定了关键字的数对

一个字典是数对的集合,每个数对都由一个词和它的值组成。一个词的值包括词的意思、发音、词源等。

比如字典中的单词date,在字典中的内容就是date,the point if time at which a transation or event takes place,其中,date就是关键字。

一个多重字典和上述的字典类似,不同的是,多重字典中,两个或多个数对可以具有相同的关键字。

下面是字典的具体实现:

#include

#include"sortedchain.h"

using namespace std;

int main(void)

{

sortedChain z;

pair p;

p.first = 2;p.second = 10;

z.insert(p);

p.first = 10;p.second = 50;

z.insert(p);

p.first = 6;p.second = 30;

z.insert(p);

p.first = 8;p.second = 40;

z.insert(p);

p.first = 1;p.second = 5;

z.insert(p);

p.first = 12;p.second = 60;

z.insert(p);

cout<

cout <

cout<second<

cout<second<

cout<second<

z.erase(1);

z.erase(2);

z.erase(6);

z.erase(12);

cout<

cout<

cout<

return 0;

}

/*

* 数值对节点的定义

* pairNode.h

*/

#ifndef PAIRNODE_H

#define PAIRNODE_H

#include

using namespace std;

template

struct pairNode

{

typedef pair pairType;

pairType element;

pairNode *next;

pairNode(const pairType& thePair):element(thePair) {}

pairNode(const pairType &thePair,pairNode* theNext)

:element(thePair){next = theNext;}

};

#endif // PAIRNODE_H

/*

* 字典的抽象类,定义了字典操作需要的基本函数

* 其中,K代表关键字,E代表值

* dictionary.h

*

*/

#ifndef DICTIONARY_H

#define DICTIONARY_H

#include"pairnode.h"

#include"iostream"

using namespace std;

template//K代表关键字,E代表值

class dictionary

{

public:

virtual ~dictionary(){}

virtual bool empty() const = 0;//如果字典为空,返回true

virtual int size() const = 0;//返回字典中数值对的数量

//返回某关键字所对应的值

virtual pair* find(const K&) const = 0;

//删除某关键字对应的数值对

virtual void erase(const K&) = 0;

//将某对数值对插入字典中

virtual void insert(const pair&) = 0;

};

#endif // DICTIONARY_H

/*

*

*/

#ifndef SORTEDCHAIN_H

#define SORTEDCHAIN_H

#include

#include"dictionary.h"

#include"pairnode.h"

using namespace std;

template

class sortedChain : public dictionary

{

public:

sortedChain() {firstNode = NULL;dSize = 0;}

~sortedChain();

bool empty() const{return dSize == 0;}//有序链表是否为空

int size() const{return dSize;}//返回有序链表元素个数

pair* find(const K&) const;//查找K值对应的某个元素对

void erase(const K&);//删除K值对应的某个元素对

void insert(const pair&);//插入元素对

void output(ostream& out) const;//输出链表元素

protected:

pairNode* firstNode;//指向链表中的第一个节点

int dSize;//字典中元素的个数,一个元素等于一个数对

};

template

sortedChain::~sortedChain()//析构函数,删除所有节点

{

while(firstNode != NULL)//顺次删除所有节点

{

pairNode* nextNode = firstNode->next;

delete firstNode;

firstNode = nextNode;

}

}

template

pair* sortedChain::find(const K& theKey) const//查找theKey对应的数对

{

pairNode* currentNode = firstNode;

while(currentNode != NULL &&

currentNode->element.first != theKey)

currentNode = currentNode->next;

if(currentNode != NULL && currentNode->element.first == theKey)

return &currentNode->element;

return NULL;

}

template

void sortedChain::insert(const pair& thePair)

{

pairNode *p = firstNode,

*tp = NULL;

while (p != NULL && p->element.first < thePair.first)

{

tp = p;

p = p->next;

}

if(p != NULL && p->element.first == thePair.first)

{

p->element.second = thePair.second;

return;

}

pairNode *newNode = new pairNode(thePair,p);

if(tp == NULL)

firstNode = newNode;

else

tp->next = newNode;

dSize++;

return;

}

template

void sortedChain::erase(const K& theKey)

{

pairNode *p = firstNode,

*tp = NULL;

while(p != NULL && p->element.first < theKey)

{

tp = p;

p = p->next;

}

if(p != NULL && p->element.first == theKey)

{

if(tp == NULL)

firstNode = p->next;

else

tp->next = p->next;

delete p;

dSize--;

}

}

template

void sortedChain::output(ostream &out) const

{

for(pairNode* currentNode = firstNode;

currentNode != NULL;

currentNode = currentNode->next)

out <element.first<

<element.second<

}

template

ostream& operator<& x)

{

x.output(out);

return out;

}

#endif // SORTEDCHAIN_H

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package shiyan; import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class AddWin extends JFrame implements ActionListener { private static MySqlUtils mySqlUtils = new MySqlUtils(); JTextField 添加汉语解释_文本条, 添加英语单词_文本条; JButton addbtn, cancelbtn; Connection Con = null; Statement Stmt = null; public AddWin() { super("添加单词"); this.setBounds(250, 250, 250, 200); this.setVisible(true); JPanel p1 = new JPanel(); p1.add(new Label("输入要添加的单词:")); 添加英语单词_文本条 = new JTextField(20); p1.add(添加英语单词_文本条); p1.add(new Label("输入添加的单词的解释:")); 添加汉语解释_文本条 = new JTextField(20); p1.add(添加汉语解释_文本条); addbtn = new JButton("提交"); cancelbtn = new JButton("取消"); p1.add(addbtn); p1.add(cancelbtn); this.add(p1); addbtn.addActionListener(this); cancelbtn.addActionListener(this); this.validate(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == addbtn) { if (添加英语单词_文本条.getText().equals("") || 添加汉语解释_文本条.getText().equals("")) { JOptionPane.showMessageDialog(this, "添加的单词或解释不能为空~", "警告", JOptionPane.WARNING_MESSAGE); } else { try { Word word = new Word(); word.setEnglish(添加英语单词_文本条.getText().toString()); word.setChinese(添加汉语解释_文本条.getText().toString()); mySqlUtils.insert(word); 添加英语单词_文本条.setText(""); 添加汉语解释_文本条.setText(""); } catch (Exception ee) { } } } else if (e.getSource() == cancelbtn) { dispose(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值