新手,很多问题要问,有耐心的请进~~

Jean09 2009-10-15 01:14:01
学的没什么头绪,希望有人能指导一下,从最简单的开始。。90度鞠躬,小妹在此谢过各位!!

算法1:顺序表va递增有序,将x插入适当位置,保持该表的有效性。

可能写完了我还有问题要问。。唉~~我比较麻烦撒
...全文
68 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jean09 2009-10-18
  • 打赏
  • 举报
回复
数据结构为什么要用伪码或类C?这样我觉得一点都不方便
我看不懂撒~
Jean09 2009-10-18
  • 打赏
  • 举报
回复
#define OK 1
#define ERROR 0
为什么要定义这个
直接用Ok或error不行么?
Jean09 2009-10-18
  • 打赏
  • 举报
回复
#define OK 1
#define ERROR 0
为什么要定义这个
直接用Ok或error不行么?
fire_woods 2009-10-15
  • 打赏
  • 举报
回复
不难.
Jean09 2009-10-15
  • 打赏
  • 举报
回复
回复于:2009-10-15 13:34:06我是不是走错路了,这是java板吗

我学的数据结构是C语言版的~~还是谢谢你咯
gukuitian 2009-10-15
  • 打赏
  • 举报
回复

public class Node2
{
private int num;
private Node2 next;
private static int size=0;
//构造函数1-生成表头
public Node2()
{
num=0;
next=null;
}
//生一个值是x,指向node的结点
private Node2(int x,Node2 node)
{
this.num=x;
this.next=node;
}
//头部添加
public void addBefore(int x)
{
Node2 node=new Node2(x,this.next);
this.next=node;
size++;
}
public int get(int index)
{
if(index>size)
{
throw new IndexOutOfBoundsException("Node2.get():"+index);
}
Node2 temp=this.next;
for(int i=0;i<index;i++)
{
temp=temp.next;
}
return temp.num;
}

public int size()
{
return size;
}
//冒泡
public void sort()
{
for(int i=0;i<size;i++)
{
//记录在案3个结点,分别是当前结点b,前一个和后一个结点a c
//其实可以不用C,用b.next也可,只为更直观
Node2 a=this;
Node2 b=a.next;
Node2 c=b.next;
for(int j=0;j<size-i;j++)
{
//到时尾则停止
if(b==null||c==null)
{
continue;
}
if(b.num>c.num)
{
//b c交换
a.next=c;
b.next=c.next;
c.next=b;
//重新记录a c
a=c;
c=b.next;
}
//不要交换时abc同时后移
else
{
a=a.next;
b=b.next;
c=c.next;
}
}

}
}

public void addSort(int x)
{
Node2 newNode=new Node2(x,null);
//当前操作数为b ,前一个结点是a
Node2 a=this;
if(a.next==null)
{
a.next=newNode;
size++;
}
Node2 b=a.next;

for(int i=0;i<size;i++)
{
//插入
if(b.num>x)
{
a.next=newNode;
newNode.next=b;
size++;
return ;
}
//a b后移
a=a.next;
b=b.next;
}
}
public static void main(String args[])
{
Node2 n=new Node2();
n.addSort(8);
n.addSort(1);
n.addSort(5);
n.addSort(2);
//n.sort();
for(int i=0;i<n.size;i++)
{
System.out.println(n.get(i));
}
}
}

gukuitian 2009-10-15
  • 打赏
  • 举报
回复
我是不是走错路了,这是java板吗
huangbo0603 2009-10-15
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int ElemType;
typedef struct
{
ElemType elem[MAXSIZE];
int last;
}SeqList;
int InsList(SeqList *L,ElemType e)
{
int k,i;
if((i<1)||(i>L->last+2))
{ printf("\n插入位置i值不合法");
return(ERROR);
}
if(L->last>=MAXSIZE-1)
{ printf("表已经无法插入");
return(ERROR);
}
for(k=L->last;k>=i-1;k--)
L->elem[k+1]=L->elem[k];
L->elem[i-1]=e;
L->last++;
return(OK);
}//InsList
int find(SeqList *L,int a)//a 表示查找的数
{
int j,c;
for(j=0;j<L->last;j++)
{
if(a>=L->elem[j]&&a<=L->elem[j+1])
c=j;
}
}
void output(SeqList L)
{
int i;
printf("\nResult of current LineList: ");
for(i=0;i<L.last;i++)
printf("%d ",L.elem[i]);
printf("\n");
}
void main()
{
SeqList L;//L表示你的va
int i=0,e,x;
printf("\nInput element into LineList(1,5,11,……0):");
i=-1;
e=-1;
while (e!=0) //建立顺序表
{ scanf("%d,",&e);
i++;
L.elem[i]=e;
}
L.last=i;
output(L);
printf("\nInput element inserted: ");
scanf("%d",&x);
printf("\nInput ith location of the LineList: ");
scanf("%d",&i);
if(InsList(&L,i,x))
output(L);
return;
}//main

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧