链表----单项链表

 

/**
 * 单项连接表
 */

package com.szy.structure.list;

import java.util.Scanner;

class Node
{
	public int id;  //节点的ID编号,作为主键使用,有用户指定
	public Node next;
}
public class SinglyLinkedList
{
	private Node START;
	
	public SinglyLinkedList()
	{
		START=null;
	}
	
	/**
	 * 插入节点方法,根据用户输入的ID值遍历链表,把新的
	 * 节点插入的指定位置。要排序的。
	 */
	public void addNode()
	{
		System.out.println("Input node info:");
		Scanner scanner=new Scanner(System.in);
		int nodeId=scanner.nextInt();
		//创建一个新的节点
		Node newNode=new Node();
		newNode.id=nodeId;
		newNode.next=null;
		
		//如果插入的节点是第一个节点
		if ((null==START)||(nodeId<=START.id))
		{
			if ((null!=START)&&(nodeId==START.id))
			{
				System.out.println("\nDuplicate id.");
				return;
			}
			newNode.next=START;
			START=newNode;
			return;
		}
		//遍历链表找到新的插入位置
		Node pre,curr;
		pre=START;
		curr=START;
		while((curr!=null)&&(nodeId>=curr.id))
		{
			if (curr.id==nodeId)
			{
				System.out.println("\nDuplicate id.");
				return;
			}
			pre=curr;
			curr=curr.next;
		}
		pre.next=newNode;
		newNode.next=curr;
	}
	
	/**
	 * 删除指定节点
	 * @param 节点ID
	 */
	public boolean delNode(int id)
	{
		Node pre=START;
		Node current=START;
		while(current!=null)
		{
			if (current.id==id)
			{
				if (current.equals(START))
				{
					START=START.next;
				}
				else
				{
					pre.next=current.next;
				}
				return true;
			}
			pre=current;
			current=current.next;
		}
		return false;
	}
	/**
	 * 查找节点
	 * @param 节点ID
	 * @return
	 */
	public boolean searchNode(int id )
	{
		Node current=START;
		while(current!=null)
		{
			if (current.id==id)
			{
				return true;
			}
			current=current.next;
		}
		return false;
	}
	/**
	 * 循环遍历链表
	 */
	public void traverse()
	{
		if (listEmpty())
		{
			System.out.println("The list is Empty");
		}
		else
		{
			System.out.println("The records in the list are:");
			Node current=START;
			while(current!=null)
			{
				System.out.print(current.id+"-->");
				current=current.next;
			}
		}
	}
	/**
	 * 判断链表是否为空
	 * @return
	 */
	public boolean listEmpty()
	{
		if (START==null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public static void main(String[] args)
	{
		SinglyLinkedList list=new SinglyLinkedList();
		while(true)
		{
			System.out.println("\n------------------------------------");
			System.out.println("Menu:");
			System.out.println("1.Add a record to the list");
			System.out.println("2.Delete a record from the list");
			System.out.println("3.View all the records in the list");
			System.out.println("4.Search for a record in the list");
			System.out.println("5.Exit");
			System.out.println("------------------------------------");
			System.out.println("Enter you choice:");
			Scanner scanner=new Scanner(System.in);
			int select=scanner.nextInt();
			switch (select)
			{
			case 1:
				list.addNode();
				break;
			case 2:
			{
				if (list.listEmpty())
				{
					System.out.println("List is empty");
					break;
				}
				System.out.println("\nInput the id of the list:");
				int id=scanner.nextInt();
				if (list.delNode(id))
				{
					System.out.println("Delete success!");
				}
				else
				{
					System.out.println("This id node is not exit!");
				}
			}
			break;
			case 3:
			{
				list.traverse();
			}
			break;
			case 4:
			{
				if (list.listEmpty())
				{
					System.out.println("List is empty");
					break;
				}
				else
				{
					System.out.println("\nInput the id of the list:");
					int id=scanner.nextInt();
					if (list.searchNode(id))
					{
						System.out.println("Record found!");
					}
					else
					{
						System.out.println("Record not found");
					}
				}
			}
			break;
			case 5:return;
			default:
				break;
			}
		}
	}
}

 

### 关于PTA平台上素数链表相关题目及解答 #### 题目描述 创建一个单向链表,该链表中的节点仅存储素数值。编写函数来实现以下功能: 1. **初始化链表** 2. **判断给定整数是否为素数并将其加入到链表中** 3. **遍历整个链表打印所有元素** 为了完成上述任务,在 C 语言环境中可以定义如下结构体表示链表节点[^1]。 ```c typedef struct Node { int data; struct Node* next; } Node, *LinkList; ``` 对于素数检测部分,可以通过下面的方法来进行优化处理[^2]: - 如果 n 是小于等于 1 的,则不是素数; - 对于大于 1 的自然数 n,如果它能被 2 到 sqrt(n) 中任意一个整除,则说明这个数不是质数;反之则是质数。 下面是完整的代码示例用于解决这个问题: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> // 定义链表结点 typedef struct Node { int data; struct Node* next; } Node; // 创建新节点 Node* create_node(int value){ Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = value; newNode->next = NULL; return newNode; } // 插入数据至链表头部 void insert_at_head(LinkList &head,int num){ Node* temp=create_node(num); if(head==NULL || head->next==NULL){ head=temp; }else{ temp->next=head; head=temp; } } // 检查数字是否为素数 int is_prime(int number){ if(number<=1)return 0; for(int i=2;i*i<=number;++i){ if(number%i==0)return 0; } return 1; } // 主要逻辑:构建素数链表 void build_prime_list(LinkList &list){ list=NULL; // 假设我们想要添加前N个素数进入列表 const int N=10; int count=0,num=2; while(count<N){ if(is_prime(num)){ insert_at_head(list,num); ++count; } ++num; } } // 打印链表内容 void print_linkedList(LinkList list){ LinkList p=list; printf("Prime List: "); while(p!=NULL){ printf("%d ",p->data); p=p->next; } puts(""); } ``` 此段代码实现了从头开始建立含有指定数量素数的单项链表,并提供了简单的测试用例以验证其正确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值