链表的基本操作

实现链表的基本运算

//链表的基本操作

class Linkegg{
	public Node head;
	public int size=0;
	private class Node{
		 int data;
		 Node next;
	}
	public boolean isEmpty(){
		return head.next==null;
	}
	//初始化
	public void addLast(int a[]){
		Node newData;
		Node oldLast=new Node();
		head=oldLast;
		for(int i=0;i<a.length;i++){
			newData=new Node();
			newData.data=a[i];
			oldLast.next=newData;
			newData.next=null;
			oldLast=newData;
			size++;
		}
	}
	//链中插入
	public void insertData(int d,int index){
		Node p=new Node();
		p.data=d;
	    Node upa=head;
	    Node down=head;
		for(int i=0;i<index;i++){
			upa=down;
			down=down.next;
		}
		upa.next=p;
		p.next=down;
	    size++;
	}
	public void toStringData(){
		Node da;
		da=head.next;
		while(da!=null){
			System.out.print(da.data+" ");
			da=da.next;
	
		}
		System.out.println(" ");
	}
	//链尾插入
	public void addListData(int a){
		Node p=new Node();
		p.data=a;
		Node daa=head;
		for(int i=0;i<size;i++){
			daa=daa.next;
		}
		daa.next=p;
		p.next=null;
		size++;
	}
	//链头插入
	public void addHead(int a){
		Node p=new Node();
		
		p.data=a;
		p.next=head.next;
		head.next=p;
		size++;
	}
	public void movie(int index){
		Node p=head;
		for(int i=0;i<index-1;i++){
			p=p.next;
		}
		p.next=p.next.next;
	}
}
public class myLink{
  public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a[]={0,9,6,3};
        Linkegg aa=new Linkegg();
        aa.addLast(a);
        aa.toStringData();
        aa.insertData(2,2);
        aa.toStringData();
        aa.addListData(5);
        aa.toStringData();
        aa.addHead(5);
        aa.toStringData();
        aa.movie(2);
        aa.toStringData();
        }

}

演示结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C语言链表基本操作的代码示例: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; }; struct Node* create_node(int data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = data; new_node->next = NULL; return new_node; } void add_node(struct Node** head, int data) { struct Node* new_node = create_node(data); if (*head == NULL) { *head = new_node; return; } struct Node* current_node = *head; while (current_node->next != NULL) { current_node = current_node->next; } current_node->next = new_node; } void delete_node(struct Node** head, int data) { struct Node* current_node = *head; struct Node* prev_node = NULL; while (current_node != NULL && current_node->data != data) { prev_node = current_node; current_node = current_node->next; } if (current_node == NULL) { return; } if (prev_node == NULL) { *head = current_node->next; } else { prev_node->next = current_node->next; } free(current_node); } void print_list(struct Node* head) { struct Node* current_node = head; while (current_node != NULL) { printf("%d ", current_node->data); current_node = current_node->next; } printf("\n"); } int main() { struct Node* head = NULL; add_node(&head, 5); add_node(&head, 10); add_node(&head, 15); add_node(&head, 20); print_list(head); delete_node(&head, 15); print_list(head); delete_node(&head, 5); print_list(head); return 0; } ``` 以上是一个简单的链表实现,包括了节点创建、添加、删除以及遍历链表并打印数据的操作。请注意,这只是一个示例代码,实际应用时需要根据具体问题进行适当的修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值