数据结构——线性表

今天是本学期的第一堂马克思方面的课,因为一直还没去买书(以前认为学校在大一时发了),直到今天早上才知道上课了还没书,没见到那书到底啥样,所以连具体的名字的都不知道呵。
中午头晕晕的,再加上一直大雨,也难得跑出去去买,干脆来了个逃课,本学期的第一次。还不知道那老师长啥样了,听说是个“铁娘子”。
呆在宿舍也没什么事干,看了看《C++ primer》的前几章,没学过的东西太多了,这学期啃书本的日子还长着呢?最终还是决定拿起数据结构编下书上的东西,复习一下,感觉上学期自己不够重视这门课呵。没有达到自己想要的效果。现在有空就复习下它咯。对于像我这样的,还是一步步来学吧。。先是线性表:
#include <iostream>
#include <stdlib.h>
#include<malloc.h>
#define  LIST_INIT_SIZE 100
#define  LISTINCREMENT 10
typedef  struct{
 int  *elem;   //存储空间基址
 int length;   //当前长度
 int  listsize;//分配容量
 }sqlist;
 
 
 void InitList(sqlist &L)
  {
   /*线性表的初始化*/
   L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
   if(!L.elem)  exit(0);
   L.length=0;
   L.listsize=LIST_INIT_SIZE;
  }//////////INitlist
 
 
 void  Creatlist(sqlist &L)
  {
   /*线性表的建立*/
   int  i,n;
   i=0;
   cin>>n;   //线性表的数据以输入的数据为-1时结束
   while(n!=-1)
    {
     L.elem[i++]=n;
     L.length++;
     cin>>n;  
     }
   L.listsize=L.length;
  } ///////////Creatlist
 
 
  void sortlist(sqlist &L)
  {
   /*线性表的排序*/
  int  i,j,temp;
   for(i=0;i<L.length-1;i++)
     for(j=i+1;j<L.length;j++)
      if(L.elem[i]>L.elem[j])
       {temp=L.elem[i];
        L.elem[i]=L.elem[j];
        L.elem[j]=temp;
        } 
   }  //////////sortlist
  
  
  void Insertlist(sqlist &L,int  e)
  {
   /*有序线性表的插入*/
   int  i,j;
   for(i=0;i<L.length&&L.elem[i]<e;i++);
    if(i>=L.length)  L.elem[i]=e;
    else
     {//插入位置在线性表中
       for(j=L.length;j>i;j--)
         L.elem[j]=L.elem[j-1];
         L.elem[j]=e;    
     }
    L.length++;
    L.listsize=L.length; 
 
  }/////////INitlist
 
 
  void  deletelist(sqlist &L,int  e1)
  {
   /*线性表的删除*/
    int i,j;
    for(i=0;i!=L.length&&L.elem[i]!=e1;i++);
     if(i==L.length)  cout<<"要删除的数据在该线性表中不存在,请核对数据"<<endl;
     else
       {
       for(j=i;j<L.length-1;j++)
        L.elem[j]=L.elem[j+1];
        L.length--;
        L.listsize=L.length;
      
       }
  }/////deletelist
 
 
  void  mergelist(sqlist A,sqlist B,sqlist &C)
   {
   /*线性表的归并*/
     int  i,j,k;
     i=j=k=0;
     while(i!=A.length&&j!=B.length)
      {
        if(A.elem[i]>B.elem[j])  {C.elem[k++]=B.elem[j];j++;}
        else   {C.elem[k++]=A.elem[i];i++;}     
      }  
   
     while(i!=A.length)
      {
      C.elem[k++]=A.elem[i];
      i++;
      }
     
      while(j!=B.length)
        {
      C.elem[k++]=B.elem[j];
      j++;
      }
      
     C.length=k;
     C.listsize=C.length; 
  
   }//////////////mergelist
 
  
  void printlist(sqlist L)
   {
   /*线性表的输出*/
   int  i;
     for(i=0;i<=L.length-1;i++)
       cout<<L.elem[i]<<" ";
      cout<<endl;
   } ////////printlist
 
 
int main(int argc, char *argv[])
{
  sqlist A,B,C;
   int e,e1;
   cout<<"输入要插入的数据:";
   cin>>e;
   cout<<"输入要删除的数据"<<endl;
   cin>>e1;
  InitList(A);
  InitList(B);
  InitList(C);
   cout<<"请输入第一个线性表的数据:"<<endl;
  Creatlist(A);
  sortlist(A);
   cout<<"第一个线性表的数据为:"<<endl;
   printlist(A);
   cout<<"请输入第二个线性表的数据:"<<endl;
  Creatlist(B);
  sortlist(B);
   cout<<"第二个线性表的数据为:"<<endl;
   printlist(B);
   mergelist(A,B,C);
   cout<<"归并后的线性表中的数据为:"<<endl;
   printlist(C);
   cout<<"输出插入前的数据:"<<endl;
   printlist(C);
   Insertlist(C,e);
   cout<<"输出插入后的数据:"<<endl;
   printlist(C);
    cout<<"输出删除前的数据:"<<endl;
   printlist(C);
   deletelist(C,e1);
    cout<<"输出删除后的数据:"<<endl;
   printlist(C);
  
  
  system("PAUSE"); 
  return 0;
}
虽然给定引用中未直接提及Java线性表算法与数据结构的测试相关内容,但可以基于线性表的实现来构建测试思路。 对于线性表的顺序存储,以顺序表为例,可测试其各项操作。以下是一个简单的顺序表测试代码示例: ```java import java.util.Arrays; // 假设的顺序表类 class SeqList { private Object[] data; private int length; private static final int DEFAULT_CAPACITY = 10; public SeqList() { data = new Object[DEFAULT_CAPACITY]; length = 0; } public void clear() { length = 0; } public boolean isEmpty() { return length == 0; } public int length() { return length; } public Object get(int i) throws Exception { if (i < 0 || i >= length) { throw new Exception("Index out of bounds"); } return data[i]; } public void insert(int i, Object x) throws Exception { if (i < 0 || i > length) { throw new Exception("Invalid insert position"); } if (length == data.length) { data = Arrays.copyOf(data, data.length * 2); } for (int j = length; j > i; j--) { data[j] = data[j - 1]; } data[i] = x; length++; } public void remove(int i) throws Exception { if (i < 0 || i >= length) { throw new Exception("Index out of bounds"); } for (int j = i; j < length - 1; j++) { data[j] = data[j + 1]; } length--; } public int indexOf(Object x) { for (int i = 0; i < length; i++) { if (data[i].equals(x)) { return i; } } return -1; } public void display() { for (int i = 0; i < length; i++) { System.out.print(data[i] + " "); } System.out.println(); } } // 测试类 class SeqListTest { public static void main(String[] args) { SeqList list = new SeqList(); try { // 测试插入 list.insert(0, 1); list.insert(1, 2); list.insert(2, 3); list.display(); // 测试获取元素 System.out.println("Element at index 1: " + list.get(1)); // 测试删除元素 list.remove(1); list.display(); // 测试查找元素 System.out.println("Index of 3: " + list.indexOf(3)); // 测试清空 list.clear(); System.out.println("Is list empty after clear? " + list.isEmpty()); } catch (Exception e) { e.printStackTrace(); } } } ``` 对于线性表的链式存储,以单链表为例,可参考引用[4]中的`PrimeLinkedList`类进行测试,以下是一个简单的测试代码示例: ```java class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } class PrimeLinkedList { ListNode head; public boolean isPrime(int num) { if (num < 1) { return false; } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } public void insertPrime(int num) { if (!isPrime(num)) { System.out.println(num + "不是素数"); return; } ListNode newNode = new ListNode(num); if (head == null) { head = newNode; } else { ListNode current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } public void printList() { ListNode current = head; while (current != null) { System.out.print(current.val + " "); current = current.next; } System.out.println(); } } class LinkedListTest { public static void main(String[] args) { PrimeLinkedList primeList = new PrimeLinkedList(); int[] numbers = {2, 3, 4, 5, 6, 7, 11, 13, 17, 19, 23}; for (int number : numbers) { primeList.insertPrime(number); } primeList.printList(); } } ``` 在上述测试中,分别对顺序表和单链表的插入、删除、查找、获取元素、清空等操作进行了测试,确保线性表的各项功能正常工作。
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值