数据结构之单向线性表 c语言与java语言实现

一、java语言实现

      这里的输入数据是从initial.txt文件中获得的;所以,总共包括下面几个文件:

    1、输入文件initial.txt,

    [the number of row]
    10 50 20 80 90 520 480 50 120 900

    2、方法的接口文件baselist.java文件

public interface baseList{
	  int getList(int i);
	  int getSize();
	  boolean initialList(Object[] obj,int n);
	  boolean insertList(int i,int x);
	  boolean deleteList(int i);
	  int findList(Object x);
}

     3、将读取到的字符串转化为整数的类bufferfile.java

public class bufferfile{
	/*public static void main(String[] args){
		int i;
		try{
		BufferedReader b = new BufferedReader(new FileReader("initial.txt"));
		String str=b.readLine();
		str=b.readLine();
		str=b.readLine();
		int[] num=stringToInt(str);
		for(i=0;i<num.length;i++)
		    System.out.print(num[i]+" ");
		}catch(Exception e){
			e.printStackTrace();
		}   
		   
	}   */
	public static int[] stringToInt(String str){
		   double[] num=new double[100];
		   int s=0;
		   double k=0;
		    for(int i=0;i<str.length();i++)
			    if(str.charAt(i)!=' '){
					k++;
					num[s]+=(str.charAt(i)-48)*Math.pow(0.1,k);						
				}
				else{
					num[s]*=Math.pow(10,k);
					if(i<str.length()) 
					k=0;
					s++;					
				}
		     num[s]*=Math.pow(10,k);
			 int[] num0=new int[s+1];
			 for(int i=0;i<=s;i++)
				 num0[i]=(int)num[i];
		   return num0;
		
	}
	
}


    4、实现接口的文件baseseqlist.java

import java.io.*;
public class baseseqlist implements baseList{
	private int Len,size;
	private Object[] obj;
	public baseseqlist(){
		this.Len=20;
		this.size=0;
		this.obj=new Integer[this.Len];
	}
	public int getList(int i){
		if(i<this.Len) return (int)this.obj[i-1];
		return 0;
	}
	public int getSize(){
		return this.size;
	}
		 
	public boolean initialList(Object[] obj,int n){
		if(n>this.Len) return false;
		for(int i=0;i<n;i++)
			this.obj[i]=obj[i];
		this.size=n;
		return true;
	}
 	public boolean insertList(int i,int x){
		for(int j=this.size;j>=i-1;j--)
			this.obj[j+1]=this.obj[j];
		this.obj[i-1]=x;
		this.size++;
		return true;
	}
    public boolean deleteList(int i){
		for(int j=i-1;j<this.size;j++)
			this.obj[j]=this.obj[j+1];
		this.size--;
		return true;
	}
	public int findList(Object x){
		for(int i=0;i<this.size;i++)
			if(this.obj[i]==x)
				return i+1;
	    return -1;
	}
	public static void main(String[] args){
		baseseqlist list=new baseseqlist();
		try{
	    BufferedReader b = new BufferedReader(new FileReader("initial.txt"));
		String str=b.readLine();
		str=b.readLine();
		//bufferfile file=new bufferfile();
		int[] num=bufferfile.stringToInt(str);
		for(int i=0;i<num.length;i++)
			System.out.print(num[i]+" ");
		Integer[] ss=new Integer[num.length];
		for(int i=0;i<num.length;i++)
			ss[i]=num[i];
		list.initialList(ss,num.length);
		}catch(Exception e){
			e.printStackTrace();
		}

		System.out.println(" "+list.getList(2));
		list.insertList(3,10);
		System.out.println(list.getSize()+" "+list.getList(11));
	}
	
}

二、c语言实现

   1、作为输入的文件initial.txt

[the number of M]
6
[thenumber of N]
100 20 40 12 41 21


   2、定义线性表的头文件initial.h

#include "stdio.h"
#define MAX_SIZE 20
typedef struct{
    int data[MAX_SIZE];
	int len;
}SeqList;
int getList(SeqList *,int);
int insertList(SeqList *,int,int);
int deleteList(SeqList *,int);
int locateList(SeqList *,int);//return the first location
int initialList(SeqList *,int*,int);

   3、实现线性表的文件initial.cpp

#include "stdafx.h"
#include "initial.h"
int getList(SeqList* L,int i){
     if(i<=L->len)
          return L->data[i-1];
	 else return 1;
}
int insertList(SeqList *L,int x,int i){
     if(i>MAX_SIZE) return 1;
	 L->data[i-1]=x;
	 return 0;
}
int deleteList(SeqList *L,int i){
     if(i>MAX_SIZE) return 1;
	 for(int j=i-1;j<L->len-1;j++)
		  L->data[j]=L->data[j+1];
	 L->len--;
	 return 0;
}
//return the first location
int locateList(SeqList *L,int x){
     for(int i=0;i<L->len;i++)
		 if(L->data[i]==x) 
			  return i+1;
	 return -1;
}
int initialList(SeqList *L,int *num,int n){
	if(n<= MAX_SIZE){
        for(int i=0;i<n;i++)
             L->data[i]=num[i];
		L->len=n;
	}
   else  return 1;
   return 0;
}

   4、运用线性表的文件application.cpp

//*********************************************
//**********sequence list
//********author:yao
//********time:2016/11/04
//*********************************************
#include "stdafx.h"
#include "initial.h"
#define MAX_STRING 512
#include <iostream>
using namespace std;
typedef struct smart{
         FILE *fp;
		 char str[MAX_STRING];
		 int M;
		 int num[MAX_STRING];
};
int _tmain(int argc, _TCHAR* argv[])
{   
    SeqList *L;
	struct smart * small;
	small=(struct smart*)malloc(sizeof(struct smart));
	if((small->fp=fopen("initial.txt","r"))==NULL)
		  exit(0);
	//**********************获取文件中的初始数值******************8
	fgets(small->str,MAX_STRING,small->fp);
	fscanf(small->fp,"%d",&small->M);
    fgets(small->str,MAX_STRING,small->fp);
	fgets(small->str,MAX_STRING,small->fp);
	for(int i=0;i<small->M;i++)
		 fscanf(small->fp,"%d ",&small->num[i]);

	L=(SeqList*)malloc(sizeof(SeqList));
    initialList(L,small->num,small->M);
	cout<<small->M<<endl;
    for(int i=0;i<L->len;i++)
		cout<<L->data[i]<<" ";
	cout<<endl;
	cout<<locateList(L,21);
	cout<<endl;
	deleteList(L,2);
    for(int i=0;i<L->len;i++)
		cout<<L->data[i]<<" ";
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值