日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
尾插法代碼java 尾插法怎么理解

想存入一個(gè)鏈表之后以$結(jié)束(使用尾插法),輸出當(dāng)前鏈表,再刪除第i個(gè)位置的元素,最后輸出最終的鏈表?

#includestdio.h

創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),定南網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:定南等地區(qū)。定南做網(wǎng)站價(jià)格咨詢:13518219792

#includestdlib.h

#includemalloc.h

typedef int ElemType;

typedef struct Node

{

ElemType data;

struct Node *next;

}Node,*Linklist;

//以下是菜單選擇函數(shù)

int menu_select()

{

int sn;

printf("\n");

printf(" 主菜單\n");

printf("*********************\n");

printf(" 1.單鏈表的建立\n");

printf(" 2.單鏈表的結(jié)點(diǎn)的刪除\n");

printf(" 3.單鏈表的輸出\n");

printf(" 0.退 出 \n");

printf("*********************\n");

printf(" 請(qǐng)選擇0---3: ");

for(;;)

{

scanf("%d",sn);

if(sn0||sn3)

printf("\n\t輸入錯(cuò)誤,重選?0---3: ");

else

break;

}

return sn;

}

//通過(guò)鍵盤輸入鏈表中元素值,利用尾插法建單鏈表L。

Linklist CreateFromTail()

{

Linklist phead = (Linklist)malloc(sizeof(Node));

phead-next = NULL;

phead-data = NULL;

Linklist ptail = phead;

ptail-next = phead;

getchar();

char ch;

int i=0;

while(ch = getchar())

{

if(ch != ',' ch != '$')

i = i*10+ch-'0';

if(ch == ',' )

{

Linklist pnew = (Linklist)malloc(sizeof(Node));

pnew-data = i;

i = 0;

ptail-next = pnew;

ptail = pnew;

pnew-next = NULL;

}

if(ch == '$')

break;

}

getchar();

return phead;

}

//在帶頭結(jié)點(diǎn)的單鏈表L中刪除第i個(gè)元素。

void DelList(Linklist L,int i)

{

Linklist ptail = NULL;

while(L-next-data != i L-next != NULL)

L = L-next;

if(L-next == NULL)

{

printf("刪除i的位置不成立!");

return ;

}

ptail = L-next;

L-next = L-next-next;

free(ptail);

return ;

}

//輸出鏈表中的值。

void output(Linklist L)

{

while(L-next != NULL)

{

L = L-next;

printf("%d ",L-data);

}

return;

}

//釋放內(nèi)存

void free_list(Linklist L)

{

Linklist p;

while(L != NULL)

{

p = L;

L = L-next;

free(p);

}

return;

}

//主控菜單處理調(diào)試程序。

int main()

{

Linklist L = NULL;

int i;

for(;;)

{

switch(menu_select())

{

case 1:

printf("\n單鏈表的建立");

printf("請(qǐng)輸入鏈表中結(jié)點(diǎn)的值(如:1,2,3,.....10,$ is end): \n");

L=CreateFromTail();

break;

case 2:

printf("鏈表結(jié)點(diǎn)的刪除\n");

printf("請(qǐng)輸入被刪除結(jié)點(diǎn)的序號(hào)i:");

scanf("%d",i);

DelList(L,i);

printf("\n");

printf("\n");

break;

case 3:

printf("輸出鏈表中結(jié)點(diǎn)的值: ");

output(L);

printf("\n");

break;

case 0:

printf("再見?\n");

free_list(L);

return 0;

}

}

}

為什么鏈表的第一個(gè)結(jié)點(diǎn)沒有數(shù)據(jù)呢?我用的是不含頭結(jié)點(diǎn)的尾插法,代碼如下:

你這個(gè)head就是頭結(jié)點(diǎn)啊,初始化以后并沒有插入數(shù)據(jù),通過(guò)p將head的next指向s,不斷創(chuàng)建新節(jié)點(diǎn)s賦值,head作為第一個(gè)結(jié)點(diǎn)并沒有數(shù)據(jù)

JAVA大師請(qǐng)進(jìn)啊,幫忙

public class SeqLinkOps implements SeqLinkInterface{

private SeqLink head = new SeqLink();

//實(shí)現(xiàn)所有接口函數(shù)

public void Rear_Create() {

SeqLink r = new SeqLink();

r = head = null;

String s;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader buf = new BufferedReader(isr);

System.out.println("請(qǐng)輸入用,號(hào)隔開的一組數(shù)據(jù)");

try {

s = buf.readLine();

StringTokenizer fenxi = new StringTokenizer(s, ",");

while (fenxi.hasMoreTokens()) {

SeqLink link = new SeqLink(fenxi.nextToken());

if (head == null)

head = link;

else

r.next = link;

r = link;

}

if (r != null)

r.next = null;

} catch (IOException e){

e.printStackTrace( );

}

}

public void Front_Create() {

SeqLink r = new SeqLink();

r = head = null;

String s;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader buf = new BufferedReader(isr);

System.out.println("請(qǐng)輸入用,號(hào)隔開的一組數(shù)據(jù)");

try {

s = buf.readLine();

StringTokenizer fenxi = new StringTokenizer(s, ",");

while (fenxi.hasMoreTokens()) {

SeqLink link = new SeqLink(fenxi.nextToken());

if (head == null)

head = link;

else

r.next = head;

r = head;

}

if (r != null)

r.next = null;

} catch (IOException e){

e.printStackTrace( );

}

}

public void Rear_Create_Head() {

SeqLink r = new SeqLink();

r = head;

String s;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader buf = new BufferedReader(isr);

System.out.println("請(qǐng)輸入用,號(hào)隔開的一組數(shù)據(jù)");

try {

s = buf.readLine();

StringTokenizer fenxi = new StringTokenizer(s, ",");

while (fenxi.hasMoreTokens()) {

SeqLink link = new SeqLink(fenxi.nextToken());

if (head == null)

head = link;

else

r.next = link;

r = link;

}

if (r != null)

r.next = null;

} catch (IOException e){

e.printStackTrace( );

}

}

public void print() {

SeqLink ptr;

ptr=head.getNext();

while(ptr!=null){

System.out.print(" "+ptr.getData()+"-");

ptr=ptr.getNext();

}

System.out.println(" NULL");

}

java利用控制臺(tái)輸入數(shù)據(jù)建立單鏈表,我的代碼出現(xiàn)java.lang.OutOfMemoryError,不知道怎么解決請(qǐng)大蝦指教

public?static?void?main(String[]?args)?{

Node?h?=?new?Node();?//?建立頭結(jié)點(diǎn)

h.next?=?null;?//?使頭結(jié)點(diǎn)的指針域?yàn)榭?/p>

Scanner?input?=?new?Scanner(System.in);

String[]?str?=?input.nextLine().split("?");

int?i?=?0;

Node?t?=?h;

for?(String?s:str)?{?//?尾插法

int?j?=?Integer.parseInt(s);

Node?p?=?new?Node(j);

p.next?=?null;

t.next?=?p;

t?=?p;?//?t始終指向最后一個(gè)元素

}

while?(h.next?!=?null)?{

System.out.print(h.next.data);

h?=?h.next;

}

}

這樣改改吧 ?, ?你的i一直沒變,?死循環(huán),內(nèi)存約占越多

各位java的高手啊 我們老師說(shuō)這個(gè)是頭插法 怎樣改為尾插法呢 幫忙改一下代碼唄

package com.test;

class node {

int data;

node next;

node(int data, node next) {

this.data = data;

this.next = next;

}

node(int data) {

this.data = data;

}

}

class ilink {

node head;

ilink() {

}

void insert(int a) {

if (head == null) {

head = new node(a);

head.next = null;

} else {

node temp = head;

while (temp.next != null) {

temp = temp.next;

}

node newNode = new node(a);

newNode.next = null;

temp.next = newNode;

}

}

void print() {

while (head != null) {

System.out.print(head.data + "\t");

head = head.next;

}

}

}

public class Test {

public static void main(String[] args) {

int[] b = { 1, 2, 3, 4, 5 };

ilink il = new ilink();

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

il.insert(b[i]);

il.print();

}

}

就是把ilink中的insert改了一下,原來(lái)的頭插法是把后來(lái)的數(shù)字放在鏈表的最開始,這樣程序輸出是數(shù)組的倒序5 4 3 2 1,尾插法是把新插入的數(shù)字放在鏈表的最后面,這樣輸出為1 2 3 4 5。

java 鏈表的輸出問題

幾位的回答都比較清楚了,我想另外說(shuō)點(diǎn)問題

你本就不應(yīng)該加入‘表尾’這個(gè)屬性,在數(shù)據(jù)結(jié)構(gòu)中鏈表的特點(diǎn)就是能用一個(gè)地址帶一個(gè)長(zhǎng)串?dāng)?shù)據(jù)鏈的,不用這個(gè)屬性的話思路會(huì)更加清晰。我也用java模擬過(guò)一些基本數(shù)據(jù)結(jié)構(gòu):

public class MyNodeT {

public T value;

public MyNodeT next;

public MyNode() {

}

public MyNode(T value) {

this.value = value;

}

public MyNode(MyNodeT t) {

this.value = t.value;

this.next = t.next;

}

public void connect(MyNodeT t){

this.next = t;

}

@Override

public String toString() {

return null==value?"":value+"-";

}

}

在這個(gè)節(jié)點(diǎn)定義的基礎(chǔ)上的鏈表定義:

public class MyLinkListT{

public MyNodeT next;

public MyLinkList() {

this.next = new MyNodeT();

}

public MyLinkList(T[] tList) {

if(tList.length==0)return;

next = new MyNodeT(tList[0]);

MyNodeT temp = next;

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

temp.connect(new MyNodeT(tList[i]));

temp = temp.next;

}

}

@Override

public String toString() {

StringBuilder sb = new StringBuilder();

MyNodeT t = next;

while (null != t) {

sb.append(t);

t = t.next;

}

return sb.toString();

}

}

然后是相關(guān)的操作類:

public class LinkListAction {

MyLinkListComparable list;

public LinkListAction(MyLinkListComparable list) {

this.list = list;

}

/**

* 頭插法建立單鏈表(數(shù)組)

* */

public void createFromHead(Comparable...objects){

MyNodeComparable start;

for (int i = 0; i objects.length; i++) {

start = new MyNodeComparable(objects[i]);

start.next = list.next;

list.next = start;

}

}

/**

* 尾插法建立單鏈表(數(shù)組)

* */

public void createFromTail(Comparable...objects){

MyNodeComparable start;

MyNodeComparable end = list.next;

for (int i = 0; i objects.length; i++) {

start = new MyNodeComparable(objects[i]);

end.next = start;

end = start;

}

end.next = null;

}

/**

* 在單鏈表中查找第i個(gè)結(jié)點(diǎn)

* */

public MyNodeComparable get(int i){

if(i 0)return null;

MyNodeComparable node = list.next;

int index = 0;

while (node != null index i) {

node = node.next;

index++;

}

return node;

}

/**

* 在單鏈表中的按值查找

* */

public MyNodeComparable locate(Comparable obj){

if(null == obj)return new MyNodeComparable();

MyNodeComparable node = list.next;

while (node != null !obj.equals(node.value)) {

node = node.next;

}

return node;

}

/**

* 求單鏈表的長(zhǎng)度

* */

public int getLength(){

int length = 0;

MyNodeComparable node = list.next;

while(null != (node = node.next)){

length++;

}

return length;

}

/**

* 單鏈表的插入操作(按位置)

* */

public void insert(Comparable obj,int location){

int length = 0;

MyNodeComparable node = list.next;

while(node!=null location != length++){node = node.next;}

if(null == node)throw new RuntimeException("插入位置有誤!");

MyNodeComparable inserter = new MyNodeComparable(obj);

inserter.next = node.next;

node.next = inserter;

}

/**

* 刪除數(shù)據(jù)

* */

public Comparable delete(int i){

int length = 0;

MyNodeComparable node = list.next;

while(node!=null i != length++){node = node.next;}

if(null == node)throw new RuntimeException("刪除位置有誤!");

Comparable o = node.next.value;

node.next = node.next.next;

return o;

}

/**

* 合并兩個(gè)有序的單鏈表

* */

public static MyLinkListComparable mergeLinkList(MyLinkListComparable la,MyLinkListComparable lb){

MyLinkListComparable lc = new MyLinkListComparable();

MyNodeComparable pc = lc.next;

MyNodeComparable pa = la.next.next;

MyNodeComparable pb = lb.next.next;

while(null != pa || null != pb){

if(null == pa){

pc.next = pb;

break;

}

if(null == pb){

pc.next = pa;

break;

}

if(pa.value.compareTo(pb.value) = 0){

pc.next = pa;

pa = pa.next;

}

else {

pc.next = pb;

pb = pb.next;

}

pc = pc.next;

}

return lc;

}

@Override

public String toString() {

return list.toString();

}

public static void main(String[] args) {

MyLinkListComparable list1 = new MyLinkListComparable();

MyLinkListComparable list2 = new MyLinkListComparable();

LinkListAction lla = new LinkListAction(list1);

// lla.createFromHead(1,3,4,6,8,10);

lla.createFromTail(1,3,4,6,8,10);

LinkListAction llb = new LinkListAction(list2);

llb.createFromTail(2,5,7,9,11);

System.out.println(lla);

System.out.println(llb);

// System.out.println(lla.locate(7));

// System.out.println(lla.getLength());

//

// lla.insert(20, 6);

// System.out.println(lla);

// System.out.println(lla.delete(4));

System.out.println(LinkListAction.mergeLinkList(lla.list, llb.list));

System.out.println(lla);

System.out.println(llb);

}

}

我還寫了一些其他的簡(jiǎn)單數(shù)據(jù)結(jié)構(gòu),感興趣的話,你可以Hi我一下,呵呵。

圣誕快樂!


網(wǎng)站標(biāo)題:尾插法代碼java 尾插法怎么理解
當(dāng)前網(wǎng)址:http://www.dlmjj.cn/article/doddeph.html