- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,其中我必须设置数据结构字典(单链表),其中单词按字母顺序排列(出现在带有文档 ID 的文本文档的句子中的单词)。并找出哪些单词出现在多个文档中,以便教授希望我们进行交集。我真的很困惑如何做交叉点。我还有其他一切(我相信这是正确的)。这是我的代码(我添加了相交算法,但它显然不起作用,我遵循了教授的算法[她从未向我们展示过示例]):
public class dictionary
{
//variables
dNode head;
int size;
//constructor
public dictionary()
{
head = null;
size = 0;
}
//addFirst method
public void addFirst(dNode s)
{
s.setNext(head);
head = s;
size++;
}
public void addLast(dNode s)
{
if ( head == null )
{
head = s;
}
else
{
s.setNext(null);
dNode w = head;
while ( w.getNext() != null )
{
w = w.getNext();
}
w.setNext(s);
}
size++;
}
//toString Method
public String toString()
{
String w = "";
dNode s = head;
while ( s != null )
{
w += s + "\n";
s = s.getNext();
}
return w;
}
//intersection method
public String intersection(pNode head, dNode head) {
int left = posting.head;
int right = dictionary.head;
int result = new dictionary();
while (left != null && right != null) {
if (dID.left < dID.right) {
left = left.next;
else if (dID.left > dID.right)
right = right.next;
else
left = left.next;
right = right.next;
result.push(left.data() );
}
}
return result;
}
}
public class dNode
{
//variables
String sent;
posting post;
dNode nextNode;
//constructor
public dNode(String sent, posting post, dNode nextNode)
{
this.sent = sent;
this.post = post;
this.nextNode = nextNode;
}
//returns element of this node
public String getSent() {
return sent;
}
//retunrs the next node of this node
public dNode getNext() {
return nextNode;
}
//modifier methods
//sets elements of this node.
public void setSent(String newSent) {
sent = newSent;
}
//sets the next node of this node
public void setNext( dNode newNext) {
nextNode = newNext;
}
//toString method
public String toString()
{
return "Sentence and Posting: \n" + sent + "\n" + post;
}
}
public class pNode {
//variables
int dID;
String word;
int occurence;
pNode next;
//constructor
public pNode(int dID, String word, int occurence, pNode next)
{
this.dID = dID;
this.word = word;
this.occurence = occurence;
this.next = next;
}
//return element of this node
public String getWord() {
return word;
}
//Returns the next node of this node
public pNode getNext() {
return next;
}
//Modifier methods
//set the words of this node
public void setWord(String newWord) {
word = newWord;
}
//sets the next node of this node
public void setNext(pNode newNext){
next = newNext;
}
//toString method
public String toString() {
return "Document ID, Word, Occurence: \n " + dID + ", "
+ word + ", " + occurence;
}
}
public class posting
{
//variables
pNode head;
int size;
//constructor
public posting()
{
head = null;
size = 0;
}
//addFirst method
public void addFirst(pNode s)
{
s.setNext(head);
head = s;
size++;
}
//addLast method
public void addLast(pNode s)
{
if ( head == null )
{
head = s;
}
else
{
s.setNext(null);
pNode w = head;
while ( w.getNext() != null )
{
w = w.getNext();
}
w.setNext(s);
}
size++;
}
//toString method
public String toString()
{
String w = "";
pNode s = head;
while ( s != null)
{
w += s + "\n";
s = s.getNext();
}
return w;
}
}
import java.io.*;
import java.util.*;
public class testFile
{
public static void main (String[] args) throws FileNotFoundException
{
File filename = new File("/export/home/hawkdom2/s0878044/CS503/assignment2/sentences.txt");
Scanner scan = new Scanner(filename);
dictionary Dictionary = new dictionary();
while ( scan.hasNextLine() )
{
String sentence = scan.nextLine();
String[] word = sentence.split(" ");
//first element is document id
int dID = Integer.parseInt( word[0] );
//insertion sort
for ( int i = 2; i < word.length; i++ )
{
for ( int j = i; j > 1; j-- )
{
if ( word[j].compareTo( word[j-1] ) > 0 )
{
String switchs = word[j];
word[j] = word[j-1];
word[j-1] = switchs;
}
}
}
//integer array count
int[] count = new int[word.length];
for ( int i = 1; i < word.length; i++)
{
for ( int j = 1; j < word.length; j++)
{
if (word[i].equalsIgnoreCase( word[j] ) )
{
count[i]++;
}
}
}
posting posts = new posting();
for ( int i = 1; i < word.length; i++ )
{
if ( (i > 1 ) && (word[i].equalsIgnoreCase( word[i-1] ) ) )
continue;
else
{
posts.addFirst(new pNode(dID, word[i], count[i], null) );
}
}
Dictionary.addLast(new dNode(sentence, posts, null) );
}
//print out output
System.out.println(Dictionary);
}
}
这是句子文件:
1 a rose is a rose
2 John chased a cat and the cat chased John
3 cats are mammals but mammals are not cats
4 beavers build dams but i know a beaver that does not
5 my dog chased a cat and the cat attacked my dog
6 my dog likes cats but my cat dislikes dogs
7 my dog likes roses but roses dislike my dog
8 my cat dislikes roses but roses like my cat
9 red roses are not my favorite roses
10 my favorite roses are pink roses
如果我能深入了解如何使两个链接列表相交(或者我的程序是否有其他问题),我将非常感激。我上周生病了,我的教授拒绝帮助我解决我错过的事情(如果我生病时不来上课,显然我不是一个认真的程序员)。我真的无法忍受这位教授教授这门课的方式,因为她没有给我们任何程序示例(而且她给我们的极少数程序总是有错误)。她还只是给了我们算法,她已经说过,它们并不总是正确的。我曾经喜欢编程,但她真的让我对它失去了兴趣,我现在想做的就是至少获得一个 C,这样我就可以转向 IT。如果有人可以帮助我,我将非常感激,我非常想完成这门课,而不必再接受这位教授的指导。
我添加了一个相交方法,但仍然收到所有这些错误:发现7个错误:文件:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java [行:86]错误:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java:86:表达式的非法开始文件:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java [行:86]错误:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java:86:“;”预期的文件:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java [行:86]错误:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java:86:不是语句文件:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java [行:86]错误:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java:86:“;”预期的文件:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java [行:86]错误:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java:86:不是语句文件:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java [行:86]错误:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java:86:“;”预期的文件:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java [行:96]错误:/export/home/hawkdom2/s0878044/CS503/assignment2/testFile.java:96:“else”没有“if”
最佳答案
public static void findIntersection(LLNode head1, LLnode head2)
{
HashSet<LLNode> hs= new HashSet<>();
HashSet<LLNode> hs2 = new HashSet<>();
LLNode currentNode1 = head1;
while(currentNode.getNext()!=null)
{
hs.add(currentNode);
currentNode1 = currentNode1.getNext();
}
LLNode currentNode2 = head2;
while(currentNode2.getNext()!=null)
{
if(hs1.contains(currentNode2)){
hs2.add(currentNode2);
}
}
关于java - 如何找到两个单链表的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20183623/
我创建了一个单链表。一切正常。 我只想知道我是否在我的代码中做了任何有潜在危险的事情。我关心的代码片段是我的推送、弹出和清理。部分代码仅用于用户交互,因此并不重要(无论如何我都发布了它,以便更清楚地了
链表:是一个有序的列表,但是它在内存中是分散存储的,使用链表可以解决类似约瑟夫问题,排序问题,搜索问题,广义表 单向链表,双向链表,环形链表 PHP的底层是C,当一个程序运行时,内存分成五个区(
我最近开始专注于使用数据结构及其用例的编码练习。下面是我的程序,将数据插入到单个链表中,其中每个节点存储下一个节点的对象。这个程序运行良好。我想了解下面的代码有多有效,所遵循的逻辑是否有效且高效。当节
尝试编写一种方法,从单链表中删除某个值的所有实例,但它似乎不起作用。 我试图适应头部是否包含该值,但我不确定这是否是正确的方法: public void remove (int value) {
struct nodeStruct { int value; struct nodeStruct *next; } struct nodeStruct* List_createNode
typedef struct node { int data; struct node *next; } NODE; NODE* add_head(NODE **phead, int data) {
void addToEnd() { newnode = (struct node*)malloc(sizeof(struct node)); printf ("Enter the cu
我想编写一种方法,从单向链表中删除具有重复数据值的连续项。该方法应返回移除的项目数。该方法应根据需要清理内存,并应假定内存是使用 new 分配的。 比如传入列表 ->a->b->c->c->a->b-
我有一个存储播放列表的表。它的定义非常简单,只有三列: setID - 引用播放列表中的一行的 16 位十六进制 songID - 16 位十六进制数,引用我的歌曲表中的一行 nextID - 16
为什么我的代码不删除链表的最后一个元素?我创建了一个当前指针来横向穿过我的列表并跳出循环..(下一个是我的结构中名为 Card_Node 的点)。回答起来应该很简单,只是不确定为什么它不会删除列表中的
大家好,我现在正在为期中考试学习,正在努力尝试使用单链表创建一个简单的程序。我想让它做的就是将“1”、“2”、“3”、“4”插入列表并打印出来。请看下面的代码: #include #include
我想创建单链表(使用类),其中每个列表中都有:指向文本的指针、整数、指向下一个列表的指针。 我需要实现 3 个功能:插入(将列表插入单链表并根据指针指向的文本使用strcmp对元素进行排序)remov
我已经实现了一个单链表,我注意到了非常奇怪的行为,但无法查明它发生的确切原因。我已经尝试使用 gdb 找出问题所在,看起来每当我计算列表的大小时,事情就开始出错了。这是我用来测试我的实现的程序,下面是
我正在尝试找出一种从链表中间删除的算法.. 我的想法是遍历链表,找到我要删除的节点之前的节点,命名为Nprev,将Nprev设置为Nnext,其中Nnext在要删除的节点之后Ndelete。 所以 N
我正在尝试创建一个简单的单向链表。以前,我成功地做到了这一点,没有任何错误,但是现在我遇到了错误。我怀疑由于第 23 行中的 if 语句,内存分配存在某种问题。 我尝试过的: 我在我的所有声明中都使用
我正在尝试创建一个简单的单向链表。以前,我成功地做到了这一点,没有任何错误,但是现在我遇到了错误。我怀疑由于第 23 行中的 if 语句,内存分配存在某种问题。 我尝试过的: 我在我的所有声明中都使用
我在学习C++语言的同时尝试构建一个链表,并实现一个从最低到最高的节点插入功能。我遵循了互联网和教科书中的一些教程。 我有一个用于链表节点设置的结构: struct Node { private:
本文实例讲述了Python数据结构与算法之链表定义与用法。分享给大家供大家参考,具体如下: 本文将为大家讲解: (1)从链表节点的定义开始,以类的方式,面向对象的思想进行链表的设计 (2)链表
1561/1563 test cases passed 问题描述: You are given two non-empty linked lists representing two non-nega
我有一个任务来实现一个单链表。我试图找出如何获取头部,但最终出现堆栈溢出错误或空指针错误。有人可以帮助我吗?我已经显示了相关的代码片段: public class Llist { privat
我是一名优秀的程序员,十分优秀!