java顺序结构的代码,编程的顺序结构
用java写个顺序表插入算法的实现?
int insert( struct student *head, int i )
创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站制作、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的嘉祥网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
{
struct student *p,*q;
q = locate( head, i );
/*调用链表定位函数,获取序号i结点的指针*/
if(q == NULL) return 0;
/*找不到序号i对应的位置,返回0,表示插入失败*/
p=(struct student*) malloc(sizeof(struct student));
/*申请新结点*/
printf ( "Input No:" );
scanf ( "%d", p- no );
printf ( "Input Name:" );
scanf ( "%s", p- name );
p- next = q- next; /*新结点的next指针的处理*/
q- next = p; /*定位结点的next指针处理*/
return 1; /*返回1,表示插入成功*/
int delete ( struct student *head,int i )
{
struct student *p, *q;
/*调用链表定位函数,获取序号i结点的指针*/
q = locate ( head, i– 1 );
if ( q == NULL ) return 0;
/*找不到序号i-1对应的位置,返回0,表示删除失败*/
/*找序号i对应的位置*/
p = q - next;
if(p == NULL) return 0;
/*找不到序号i对应的位置,返回0,表示删除失败*/
q- next = p- next;
free(p); /*释放结点内存*/
return 1; /*返回1,表示删除成功*/
java里升序和降序最简短的代码分别是什么?
直接调用jdk里面的方法Arrays.sort(args)方法。而且这个方法重载实现了多个参数,排序下标[N~M]位数字~倒叙,升序等等~
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("升序:" + list);
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("降序:" + list);
设计一个有序顺序表 数据结构(java)
package array;
class OrderArray{
private long[] a;
private int nElems;
public OrderArray(int maxSize){
a = new long[maxSize];
nElems = 0;
}
public void insert(long value){
int pos = 0;
for(pos=0;posnElems;pos++){
if(a[pos]value){
break;
}
}
for(int i=nElems;ipos;i--){
a[i] = a[i-1];
}
a[pos] = value;
nElems++;
}
public boolean delete(long value){
int pos = find(value);
if(pos!=-1){
for(int i=pos;inElems;i++){
a[i] = a[i+1];
}
nElems --;
return true;
}
return false;
}
public int find(long keySearch){
int lowerBound = 0;
int upperBound = nElems-1;
int curIn = 0;
while(true){
curIn = (lowerBound+upperBound)/2;
if(a[curIn]==keySearch){
return curIn;
}else if(lowerBoundupperBound){
return -1;
}else{
if(a[curIn]keySearch){
upperBound = curIn -1;
}else{
lowerBound = curIn +1;
}
}
}
}
public void display(){
for(int i=0;inElems;i++){
System.out.println(a[i]);
}
}
}
public class OrderArrayApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int maxSize = 100;
OrderArray orderArray = new OrderArray(maxSize);
orderArray.insert(1);
orderArray.insert(7);
orderArray.insert(9);
orderArray.insert(8);
orderArray.insert(6);
orderArray.insert(4);
orderArray.display();
orderArray.delete(8);
orderArray.display();
}
}
用Java语言编写数据结构中顺序表的插入删除查找代码并实现
public class Test {
public static void main(String[] args) {
int length = 5;
int ai = 1;
String data = "data";
String[] array = insertArrar(data, ai, length);
data = delArray(array, ai, length);
System.out.println(data);
}
public static String[] insertArrar(String data,int ai,int length){
String[] array = new String[length];
array[ai] = data;
return array;
}
public static String delArray(String[] array,int ai,int length){
String data = "";
data=array[ai];
array[ai]=null;
for(int i = 0; iarray.length;i++){
System.out.println(array[i]);
}
return data;
}
}
关于数据结构(java)的一个代码
描述栈抽象数据类型的SStack接口的声明
public interfaceSStackE //栈接口
{
boolean isEmpty(); //判断是否空栈,若空栈返回true
boolean push(E element); //元素element入栈,若操作成功返回true
E pop(); //出栈,返回当前栈顶元素,若栈空返回null
E get(); //取栈顶元素值,未出栈,若栈空返回null
}
顺序栈类具体操作方法的声明:
importdataStructure.linearList.SStack;
public classSeqStackE implements SStackE
//顺序栈类
{
private Object value[]; //存储栈的数据元素
private int top; //top为栈顶元素下标
public SeqStack(int capacity) //构造指定容量的空栈
{
this.value = newObject[Math.abs(capacity)];
this.top=-1;
}
public SeqStack() //构造默认容量的空栈
{
this(10);
}
public boolean isEmpty() //判断是否空栈,若空栈返回true
{
return this.top==-1;
}
public boolean push(E element) //元素element入栈,若操作成功返回true
{
if (element==null)
return false; //空对象(null)不能入栈
if (this.top==value.length-1) //若栈满,则扩充容量
{
Object[] temp = this.value;
this.value = newObject[temp.length*2];
for (int i=0; itemp.length;i++)
this.value[i] = temp[i];
}
this.top++;
this.value[this.top] = element;
return true;
}
public E pop() //出栈,返回当前栈顶元素,若栈空返回null
{
if (!isEmpty())
return (E)this.value[this.top--];
else
return null;
}
public E get() //取栈顶元素值,未出栈,栈顶元素未改变
{
if (!isEmpty())
return (E)this.value[this.top];
else
return null;
}
public String toString() //返回栈中各元素的字符串描述
{
String str="{";
if (this.top!=-1)
str +=this.value[this.top].toString();
for (int i=this.top-1; i=0; i--)
str += ","+this.value[i].toString();
return str+"} ";
}
实例引用public static void main(String args[])
{
SeqStackString stack = newSeqStackString(20);
System.out.print("Push: ");
char ch='a';
for(int i=0;i5;i++)
{
String str =(char)(ch+i)+"";
stack.push(str);
System.out.print(str+" ");
}
System.out.println("\n"+stack.toString());
System.out.print("Pop : ");
while(!stack.isEmpty()) //全部出栈
System.out.print(stack.pop().toString()+" ");
System.out.println();
}
当前标题:java顺序结构的代码,编程的顺序结构
地址分享:http://myzitong.com/article/hcoocd.html