数据结构--单链表


具体实现

----------------------------------------------------------------------------------------------------------------------


package LinkListDemo;


public interface List {
public int size();

public boolean empty();

public void insert(int index,Object obj) throws Exception;

public void delete(int index)throws Exception;

public Object get(int index)throws Exception;
}


-----------------------------------------------------------------------------------------------------------------


package LinkListDemo;


public class LinkList implements List {
Node head;//头指针
Node current;//当前结点对象
int size;//结点个数


//初始化一个空链表
public LinkList(){
this.head = current= new Node(null);
this.size=0;
}
//定位函数
public void index(int index) throws Exception{
if(index<-1||index>size-1){
throw new Exception("参数错误");
}
//说明在头结点之后操作
if(index == -1) return;
current =head.next;
int j = 0;//循环变量
while(current != null&& j<index){
current = current.next;
j++;
}
}


public int size() {
return this.size;
}


public boolean empty() {
return size==0;
}


public void insert(int index, Object obj) throws Exception {
if(index<0||index>size){
throw new Exception("参数错误");
}
index(index-1);
current.setNext(new Node(obj, current.next));
size++;//(可以在这里来改变size)
}



public void delete(int index) throws Exception {
if(empty()){
throw new Exception("链表为空");
}
if(index<0||index>size){
throw new Exception("参数错误");
}
index(index-1);//定位到要操作对象的前一个对象
current.setNext(current.next.next);
size--;
}


public Object get(int index) throws Exception {
if(index<0||index>size){
throw new Exception("参数错误");
}
index(index);

return current.getElement();
}
}

--------------------------------------------------------------------------------------------------------------------------------------------

package LinkListDemo;


public class Test {
public static void main(String[] args) throws Exception{
LinkList list = new LinkList();
for(int i=0;i<10;i++){
int temp = (int) (((Math.random())*100)%100);
list.insert(i, temp);
System.out.print(list.get(i)+" ");
}
list.delete(4);
System.out.println("-----删除第五个元素后----");
for(int i=0;i<list.size;i++){
System.out.print(list.get(i)+" ");
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常见的数据结构,它由一个或多个节点组成,每个节点包含一个数据域和一个指针域,指针域指向下一个节点。 实现链表的查找位序算法函数需要以下步骤: 1. 定义一个指针p指向链表的头节点,并定义一个变量count,用于计数。 2. 从头节点开始,遍历链表,当p指向某个节点时,计数器count加1。 3. 如果p指向的节点的数据与目标数据相等,则返回当前的计数器count,即为目标数据的位序。 4. 如果p指向的节点不是目标数据,则将p指向下一个节点,重复步骤3。 5. 如果遍历完链表后仍未找到目标数据,则返回-1,表示未找到。 下面是C语言实现链表查找位序算法函数的代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 typedef struct Node { int data; // 数据域 struct Node* next; // 指针域 } Node; // 查找位序的算法函数 int findPosition(Node* head, int target) { Node* p = head; // 指向头节点 int count = 0; // 计数器初始化为0 while (p != NULL) { count++; // 计数器加1 if (p->data == target) { return count; // 找到目标数据,返回当前计数器的值 } p = p->next; // 指向下一个节点 } return -1; // 遍历完链表未找到目标数据,返回-1 } int main() { // 创建链表 Node* head = (Node*)malloc(sizeof(Node)); head->data = 1; // 头节点数据为1 Node* node1 = (Node*)malloc(sizeof(Node)); node1->data = 2; Node* node2 = (Node*)malloc(sizeof(Node)); node2->data = 3; head->next = node1; node1->next = node2; node2->next = NULL; // 查找位序示例 int target = 3; // 目标数据为3 int position = findPosition(head, target); if (position != -1) { printf("目标数据 %d 的位序为 %d\n", target, position); } else { printf("未找到目标数据 %d\n", target); } // 释放链表内存 free(node2); free(node1); free(head); return 0; } ``` 在上述代码中,我们首先定义了一个指向头节点的指针p和一个计数器count,然后使用while循环遍历链表。当p指向某个节点时,计数器加1,并判断该节点的数据是否与目标数据相等。如果找到了目标数据,则返回当前计数器的值,即为目标数据的位序。如果遍历完链表仍未找到目标数据,则返回-1表示未找到。最后在主函数中演示了调用该算法函数的示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值