尾插法代码java 尾插法怎么理解

想存入一个链表之后以$结束(使用尾插法),输出当前链表,再删除第i个位置的元素,最后输出最终的链表?

#includestdio.h

创新互联公司是一家专注于成都网站设计、成都网站建设与策划设计,定南网站建设哪家好?创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:定南等地区。定南做网站价格咨询:13518219792

#includestdlib.h

#includemalloc.h

typedef int ElemType;

typedef struct Node

{

ElemType data;

struct Node *next;

}Node,*Linklist;

//以下是菜单选择函数

int menu_select()

{

int sn;

printf("\n");

printf(" 主菜单\n");

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

printf(" 1.单链表的建立\n");

printf(" 2.单链表的结点的删除\n");

printf(" 3.单链表的输出\n");

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

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

printf(" 请选择0---3: ");

for(;;)

{

scanf("%d",sn);

if(sn0||sn3)

printf("\n\t输入错误,重选?0---3: ");

else

break;

}

return sn;

}

//通过键盘输入链表中元素值,利用尾插法建单链表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;

}

//在带头结点的单链表L中删除第i个元素。

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;

}

//释放内存

void free_list(Linklist L)

{

Linklist p;

while(L != NULL)

{

p = L;

L = L-next;

free(p);

}

return;

}

//主控菜单处理调试程序。

int main()

{

Linklist L = NULL;

int i;

for(;;)

{

switch(menu_select())

{

case 1:

printf("\n单链表的建立");

printf("请输入链表中结点的值(如:1,2,3,.....10,$ is end): \n");

L=CreateFromTail();

break;

case 2:

printf("链表结点的删除\n");

printf("请输入被删除结点的序号i:");

scanf("%d",i);

DelList(L,i);

printf("\n");

printf("\n");

break;

case 3:

printf("输出链表中结点的值: ");

output(L);

printf("\n");

break;

case 0:

printf("再见?\n");

free_list(L);

return 0;

}

}

}

为什么链表的第一个结点没有数据呢?我用的是不含头结点的尾插法,代码如下:

你这个head就是头结点啊,初始化以后并没有插入数据,通过p将head的next指向s,不断创建新节点s赋值,head作为第一个结点并没有数据

JAVA大师请进啊,帮忙

public class SeqLinkOps implements SeqLinkInterface{

private SeqLink head = new SeqLink();

//实现所有接口函数

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("请输入用,号隔开的一组数据");

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("请输入用,号隔开的一组数据");

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("请输入用,号隔开的一组数据");

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利用控制台输入数据建立单链表,我的代码出现java.lang.OutOfMemoryError,不知道怎么解决请大虾指教

public static void main(String[] args) {

Node h = new Node(); // 建立头结点

h.next = null; // 使头结点的指针域为空

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始终指向最后一个元素

}

while (h.next != null) {

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

h = h.next;

}

}

这样改改吧  ,  你的i一直没变, 死循环,内存约占越多

各位java的高手啊 我们老师说这个是头插法 怎样改为尾插法呢 帮忙改一下代码呗

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改了一下,原来的头插法是把后来的数字放在链表的最开始,这样程序输出是数组的倒序5 4 3 2 1,尾插法是把新插入的数字放在链表的最后面,这样输出为1 2 3 4 5。

java 链表的输出问题

几位的回答都比较清楚了,我想另外说点问题

你本就不应该加入‘表尾’这个属性,在数据结构中链表的特点就是能用一个地址带一个长串数据链的,不用这个属性的话思路会更加清晰。我也用java模拟过一些基本数据结构:

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+"-";

}

}

在这个节点定义的基础上的链表定义:

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();

}

}

然后是相关的操作类:

public class LinkListAction {

MyLinkListComparable list;

public LinkListAction(MyLinkListComparable list) {

this.list = list;

}

/**

* 头插法建立单链表(数组)

* */

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;

}

}

/**

* 尾插法建立单链表(数组)

* */

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个结点

* */

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;

}

/**

* 求单链表的长度

* */

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;

}

/**

* 删除数据

* */

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;

}

/**

* 合并两个有序的单链表

* */

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);

}

}

我还写了一些其他的简单数据结构,感兴趣的话,你可以Hi我一下,呵呵。

圣诞快乐!


文章名称:尾插法代码java 尾插法怎么理解
浏览地址:http://myzitong.com/article/doddeph.html