新聞中心
java循環(huán)單鏈表實(shí)現(xiàn)約瑟夫環(huán),我的代碼出列順序不正確
你的remove方法不對(duì),你的方法每次刪掉的是從head開(kāi)始第m個(gè)位置的節(jié)點(diǎn),
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的青秀網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
但約瑟夫環(huán)需要的是要?jiǎng)h掉每次循環(huán)數(shù)到m的位置的節(jié)點(diǎn)。
remove方法可以去掉,再把out方法改一下就可以了。
public void out(int m) throws Exception {
Node p = head;
Node pre = null;
int count = 1;
while (curlen 0) {
if (count == m) {
System.out.print(p.getData() + " ");
if(pre != null){
pre.setNext(p.getNext());
}
p = p.getNext();
curlen = curlen - 1;
count = 1;
} else {
pre = p;
p = p.getNext();
count++;
}
}
}
java循環(huán)單鏈表實(shí)現(xiàn)約瑟夫環(huán)
看了你的代碼,不是很明白,給你提幾個(gè)建議吧:
1、不需要tail節(jié)點(diǎn)
2、remove方法應(yīng)該對(duì)刪除節(jié)點(diǎn)前面的節(jié)點(diǎn)操作,而不是使用數(shù)字找
給你我修改的LinkList類(lèi),你參考一下:
public?class?LinkList?{
private?Node?head;
int?curlen?=?0;
//?創(chuàng)建鏈表
public?void?createlist(int?code)?throws?Exception?{
insert(curlen,?code);
}
public?void?insert(int?i,?int?code)?throws?Exception?{
Node?s?=?new?Node(code);
if?(i?==?0)?{
s.setNext(head);
head?=?s;
}
Node?p?=?head;
int?j?=?0;
while?(p?!=?null??j??i?-?1)?{
p?=?p.getNext();
j++;
}
if?(j??i?||?p?==?null)?{
throw?new?Exception("插入位置不合理");
}
s.setNext(p.getNext());
p.setNext(s);
// tail?=?s;
// tail.setNext(head);
curlen?=?curlen?+?1;
}
public?void?remove(int?i)?throws?Exception?{
Node?p?=?head,?q?=?null;
int?j?=?0;
i?=?i?-?1;
while?(j??i)?{
q?=?p;
p?=?p.getNext();
j++;
}
if?(j??i?||?p?==?null)
throw?new?Exception("刪除位置不合法");
if?(q?==?null)?{
// tail.setNext(p.getNext());
head?=?head.getNext();
}?else
q.setNext(p.getNext());
curlen?=?curlen?-?1;
}
/**
?*?按照節(jié)點(diǎn)刪除
?*?@param?i
?*?@throws?Exception
?*/
public?void?remove(Node?p)?throws?Exception?{
if(p.getNext()==p){
p=null;
head=null;
}
else{
Node?q?=?p.getNext();
p.setNext(q.getNext());
}
curlen?=?curlen?-?1;
}
public?void?out(int?m)?throws?Exception?{
Node?p?=?head;
if(m==1){
System.out.print("按照順序出列");
return;
}
int?count?=?1;
int?n=m-1;
while?(curlen??0)?{
if?(count?==?n)?{
System.out.print(p.getNext().getData()?+?"??");
remove(p);
count?=?1;
}?else?{
count++;
}
p?=?p.getNext();
}
}
public?void?display()?{
Node?node?=?head;
for?(int?i?=?0;?i??2?*?curlen;?i++)?{
System.out.print(node.getData()?+?"?");
node?=?node.getNext();
}
System.out.println();
}
}
用java語(yǔ)言編寫(xiě)單循環(huán)鏈表約瑟夫生死游戲
LinkList p;; /link,int k;*循環(huán)地刪除隊(duì)列結(jié)點(diǎn)*,m為出列者喊到的數(shù)
{
/
}
p-;n最后被刪除的元素是;
for(int i=0;data);link;
for(i=0;*使鏈表循環(huán)起來(lái)*,int m) /m-1,P-;
p=p-;;i++)
{
p=(LinkList)malloc(sizeof(LNode)); /link,p-;ilt;
}
r-;n為總?cè)藬?shù);link;
p=p-;;
p=list;
p=r-;
}
,k為第一個(gè)開(kāi)始報(bào)數(shù)的人;i++)
{
r=p;* p為當(dāng)前結(jié)點(diǎn) r為輔助結(jié)點(diǎn);
⒊不斷地從鏈表中刪除鏈結(jié)點(diǎn);
else
r-;link=p;
free(p),r;
p-;data);,無(wú)頭結(jié)點(diǎn)的循環(huán)鏈表;*建立循環(huán)鏈表*:%4dquot;ilt;*使p指向頭節(jié)點(diǎn)*!=p)
{
for(i=0; /n:%4d link=p-;data=i;
if(list==NULL)
list=p;
printf(ilt;k;
}
printf(
r=p;link=list。
void JOSEPHUS(int n;*把當(dāng)前指針移動(dòng)到第一個(gè)報(bào)數(shù)的人*/
/i++)
{
r=p,指向p的前驅(qū)結(jié)點(diǎn) list為頭節(jié)點(diǎn)*/link解決問(wèn)題的核心步驟,list:(程序的基本算法)
⒈建立一個(gè)具有n個(gè)鏈結(jié)點(diǎn);
⒉確定第1個(gè)報(bào)數(shù)人的位置;
while(p-,直到鏈表為空;被刪除的元素
Java單向鏈表代碼。
這是我寫(xiě)的一個(gè)差不多,你看一下吧:
package com.test.list;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class LinkedList {
public static void main(String[] args) {
MyList l = new MyList();
MyListNode node = l.createList();
l.printNode(node);
//l.searchNode(node, 4);
//node = l.insertNode(node, 3, "g");
//l.printNode(node);
node = l.deleteNode(node, "d");
l.printNode(node);
}
}
class MyListNode {
public String data;
public MyListNode nextNode;
}
class MyList {
public MyListNode createList() {
MyListNode node = new MyListNode();
MyListNode q ,p;
q = new MyListNode();
q = node;
while (true) {
String s = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("請(qǐng)輸入節(jié)點(diǎn)數(shù)據(jù):");
s = br.readLine();
if (s.equals("0")) {
break;
} else {
p = new MyListNode();
p.data = s;
p.nextNode = null;
q.nextNode = p;
q = p;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return node;
}
public void printNode(MyListNode node) {
MyListNode p = node.nextNode;
while (p!= null) {
System.out.print(" "+p.data);
p = p.nextNode;
}
}
public void searchNode(MyListNode node, int i){
MyListNode p = node.nextNode;
int j = 1;
while (p != null ji) {
p = p.nextNode;
j++;
}
if( p == null || ji) {
System.out.println("error");
}
System.out.println(" --"+p.data+"--");
}
public MyListNode insertNode(MyListNode node, int i ,String s) {
MyListNode p = node.nextNode;
int j = 1;
while (p != null ji-1) {
p = p.nextNode;
j++;
}
if( p == null || ji-1) {
System.out.println("error");
}
MyListNode n = new MyListNode();
n.data = s;
n.nextNode = p.nextNode;
p.nextNode = n;
return node;
}
public MyListNode deleteNode(MyListNode node ,String s) {
MyListNode p = node;
while(p.nextNode != null !p.nextNode.data.equals(s)) {
p = p.nextNode;
}
p.nextNode = p.nextNode.nextNode;
return node;
}
}
/*逆位序創(chuàng)建
public MyListNode createList() {
MyListNode node = new MyListNode();
node.nextNode = null;
while(true) {
String s = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("請(qǐng)輸入節(jié)點(diǎn)數(shù)據(jù):");
s = br.readLine();
if(s.equals("0")) {
break;
}else {
MyListNode n = new MyListNode();
n.data = s;
n.nextNode = node.nextNode;
node.nextNode = n;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return node;
}
*/
標(biāo)題名稱(chēng):?jiǎn)窝h(huán)鏈表java代碼,java實(shí)現(xiàn)單鏈表代碼
分享地址:http://fisionsoft.com.cn/article/dsepiij.html