链表的实现

链表分为单链表、双链表和循环链表,这些理论知识在笔记中自然写了,这里我只写出其中的实现:

单链表的实现(c语言实现)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>	

typedef struct Book {
	char bookname[20];
	char author[10];
}Book;

typedef struct Node {
	Book book;
	struct Node *next;
}Node,*LinkList;

//初始化链表 
void initChainedTable(Node **L){    //ok
	*L=(LinkList)malloc(sizeof(Node));
	(*L)->next=NULL;
}
//尾结点插入 
Node* putNodeByTail(Book book,Node *r){   //ok
	Node *node=(Node*)malloc(sizeof(Node));
	node->book= book;
	node->next=NULL;
	r->next=node;
	return node;
}
//首节点插入
void putNodeByHead(Book book,LinkList L){   //ok
	Node *node=(Node*)malloc(sizeof(Node));
	node->book=book;
	node->next=L->next;
	L->next=node;
} 
//删除其中一个结点
void deleteNode(int i,LinkList L){   //ok
	Node *node=L->next;
	int counter=1;
	if (i == 1) {
        if (node != NULL) {
            L->next = node->next;
            free(node);
        } else {
            printf("List is empty, cannot delete.\n");
        }
        return;
    }

	while(node&&counter<(i-1)){
		node=node->next;
		++counter;
	}
	if(!node||i<=0){
		printf("index is out of bound \n");
	}else{
		Node* delnode = node->next;
        if (delnode) {
            node->next = delnode->next;
            free(delnode);
        } else {
            printf("Node at index %d does not exist\n", i);
        }
	}
	
}
//向链表中插入元素
void insertNode(Book book,int i,LinkList L){   //ok
	Node *newnode=(Node*)malloc(sizeof(Node));
	Node *node=L->next;
	newnode->book=book;
	int count=1;
	while(node&&count<(i-1)){
		node=node->next;
		++count;
	}
	if(!node||i<=0){
		printf("index is out of bound\n");
	}else{
		newnode->next=node->next;
		node->next=newnode;	
	}
	
} 
//判断链表是否为空
int isEmpty(LinkList L){  //ok
	return L->next==NULL?1:0;
} 

//获取第i个结点 
Node*  getNode(int i,LinkList L){  //ok
	Node* node=L->next;
	int count=1;
	while(node&&count<i){
		node=node->next;
		++count;
	}
	if(node&&i>0) return node;
	printf("index is out of bound\n"); 
} 
//通过值获取到它的位置
int getIndex(Book book,LinkList L){  //ok
	Node* node=L->next;
	int count=1;
	while(node&&strcmp(node->book.author,book.author)&&strcmp(node->book.bookname,book.bookname)){
		node=node->next;
		++count;
	}
	if(node) return -1;
	return count;
} 
//打印链表中的数据 
void printNodeData(LinkList L){    //ok
	Node *node=L->next;
	while(node){
		printf("bookname:%s;author:%s\n",node->book.bookname,node->book.author);
		node=node->next;
	}
	if(!node) printf("你所给的是个空链表!\n"); 
}
//释放一个链表
void freeChainedTable(LinkList L){ 
	Node *p=L->next;
	Node *q;
	while(p){  
		q=p->next;
		free(p);
		p=q; 
	}
	printf("清除完成!\n");
	L->next=NULL;
}

int main(){
	LinkList L;
	initChainedTable(&L);
	Book book1={"java","zhangyu"};
	Book book2={"php","zhang"};
	Book book3={"c#","nanchengyu"};
	putNodeByHead(book1,L);
	putNodeByHead(book2,L);
	putNodeByHead(book3,L);
	Book book4={"java","zhang"};
	freeChainedTable(L);
	printNodeData(L);
	system("pause");
	return 0;
}
单链表的重要操作实现图解

单链表的实现(java语言实现)

package linearList;

/*
* 带头节点单链表的实现
* */
class Data{
    String username;
    int age;

    public Data(String username, int age) {
        this.username = username;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Data{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}
class Node{

    Data data;
    private Node next;

    public Node(Data data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Node() {
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}
public class LinkList {
    private Node node;
    private Node cruNode;
    //初始化一个链表
    public void init(){
        Node node=new Node();
         this.node=node;
         this.cruNode=node;
    }
    //尾结点插入
    public LinkList putNodeByTail(String username,int age){
        Data data = new Data(username,age);
        Node newNode=new Node(data,null);
        cruNode.setNext(newNode);
        cruNode=newNode;
        return this;
    }
    //头节点插入
    public void putNodeByHead(String username,int age){
        Data data = new Data(username,age);
        Node newNode=new Node(data,null);
        if (node!=null){
            Node tmp=node.getNext();
            newNode.setNext(tmp);
            node.setNext(newNode);
        }
    }
    //删除其中一个结点
    public void deleteNode(int i){
        if (i==1){
            Node next = node.getNext().getNext();
            node.setNext(next);
        }else {
            int count=2;
            Node froNode=node.getNext();
            Node preNode=node.getNext().getNext();
            while (preNode!=null){
                if (count==i){
                    froNode.setNext(preNode.getNext());
                }
                froNode=preNode;
                preNode=preNode.getNext();
                ++count;
            }
        }
    }
    //向链表中插入元素
    public void insertNode(int i,String username,int age){
        Data data = new Data(username, age);
        Node newNode = new Node(data, null);
        int count=1;
        Node preNode=node.getNext();
        Node froNode=null;
        while (preNode!=null){
            if (count==i){
                froNode.setNext(newNode);
                newNode.setNext(preNode);
            }
            froNode=preNode;
            preNode=preNode.getNext();
            ++count;
        }
    }
    //判断链表是否为空
    public boolean isEmpty(){
        return node.getNext()!=null?false:true;
    }
    //获取第i个结点
    public Node getNode(int i){
        Node countNode=node.getNext();
        int count=1;
        while (countNode!=null){
            if (count==i){
                return countNode;
            }
            countNode=countNode.getNext();
            ++count;
        }
        if (countNode==null) return null;
        return countNode;
    }

    //打印链表中的数据
    public void printNodeData(){
        Node preNode=node.getNext();
        while (preNode!=null){
            System.out.println(preNode.data.toString());
            preNode=preNode.getNext();
        }
    }
}
class LinkListTest {
    public static void main(String[] args) {
        LinkList linkList = new LinkList();
        linkList.init();
        linkList.putNodeByTail("zhangshan",10).putNodeByTail("lishi",20)
                .putNodeByTail("wangwu",21).putNodeByHead("qianmian",70);
        System.out.println(linkList.isEmpty());
        linkList.printNodeData();
        linkList.deleteNode(2);
        System.out.println("--------------");
        linkList.printNodeData();
        System.out.println("--------------");
        linkList.insertNode(2,"222",11);
        linkList.printNodeData();
    }
}

双链表的实现(c语言实现)

#include <stdio.h>
#include <stdlib.h>

//这个源文件描述的是不循环双向链表

typedef struct Book {
	char bookname[20];
	char author[10];
}Book;
typedef struct doLinkList{
	Book book;
	struct doLinkList *prior,*next;
	struct doLinkList *ele;
}Node,*LinkList;
//初始化链表 
void initDoLinkList(LinkList *L){
	*L=(LinkList)malloc(sizeof(Node));
	(*L)->prior=NULL;
	(*L)->next=NULL;
	(*L)->ele=*L;
}
//向链表添加结点 
void putNode(Book book,Node *listNode){
	Node *node=(Node*)malloc(sizeof(Node));
	node->book=book;
	listNode->ele->next=node;
	node->prior=listNode->ele;
	node->next=NULL;
	listNode->ele=node;
}
//打印结点数据 
void printNodeData(LinkList list){
	if(!list->next){
		printf("你所给的是个空链表!\n"); 
		return;	
	} 
	Node *node=list->next;
	while(node){
		printf("bookname:%s;author:%s\n",node->book.bookname,node->book.author);
		node=node->next;
	}
}
//查找链表数据 
Node* getNode(int i,LinkList list){
	Node *node=list->next;
	int counter=1;
	while(node&&counter!=i&&i>0){
		node=node->next;
		++counter;
	}
	if(i<0||i>counter){
		printf("索引超出范围!\n");
		return NULL;
	}
	return node;
}

//删除链表结点
void delNode(int i,LinkList list){
	Node *p;
	if(!(p=getNode(i,list))){
		printf("并没有找到该节点!\n");
	}else if(p->next){
		p->prior->next=p->next;
		p->next->prior=p->prior;
	}else{
		p->prior->next=NULL;
	}
	free(p);
	
} 

//向双向节点中插入结点(末尾节点不可以插入) 
void insertNode(int i,Book book,LinkList list){
	Node *p;
	Node *node=(Node*)malloc(sizeof(Node));
	node->book=book;
	if(p=getNode(i,list)){
		p->prior->next=node;
		node->prior=p->prior;
		node->next=p;
		p->prior=node;
	}else {
		printf("插入失败!\n");
	}
} 

int main(){
	LinkList L;
	initDoLinkList(&L);
	Book book1={"java","zhangyu"};
	Book book2={"php","zhang"};
	Book book3={"c#","nan"};
	putNode(book1,L);
	putNode(book2,L);
//	putNode(book3,L);
	insertNode(2,book3,L);
	printNodeData(L);
	system("pause");
	return 0;
}
双链表的重要操作实现图解

双链表的实现(java语言实现)

package linearList;

import java.awt.print.Book;

/*
*带头节点的双链表实现
* */
class User{
    String username;
    int age;

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}
class doNode{
    User data;
    private doNode next;
    private doNode pre;

    public doNode(User user, doNode next,doNode pre) {
        this.data = user;
        this.next = next;
        this.pre=pre;
    }

    public doNode() {
    }

    public doNode getNext() {
        return next;
    }

    public doNode getPre() {
        return pre;
    }

    public void setNext(doNode next) {
        this.next = next;
    }
    public void setPre(doNode pre){
        this.pre=pre;
    }
}

public class doLinkList {
    private doNode node;
    private doNode cruNode;
    //初始化链表
    void initDoLinkList(){
        doNode doNode = new doNode();
        node=doNode;
        cruNode=doNode;
    }
    //向链表添加结点
    doLinkList putNode(String username,int age){
        User user = new User(username, age);
        doNode doNode = new doNode(user,null,cruNode);
        cruNode.setNext(doNode);
        cruNode=doNode;
        return this;
    }
    //打印结点数据
    void printNodeData(){
        if (node.getNext()==null){
            System.out.println("链表为空");
            return;
        }else{
            doNode NodePointer=node.getNext();
            while (NodePointer!=null){
                User user=NodePointer.data;
                System.out.println(user.toString());
                NodePointer=NodePointer.getNext();
            }
        }
    }
    //删除链表结点
    void delNode(int i){
        doNode node=this.node.getNext();
        doNode preNode=this.node;
        int count=1;
        while(node!=null){
            if (count==i){
                preNode.setNext(node.getNext());
                node.getNext().setPre(preNode);
            }
            ++count;
            preNode=node;
            node=node.getNext();
        }
    }
    //向双向节点中插入结点(末尾节点不可以插入)
    void insertNode(String username, int age,int i){
        User user = new User(username, age);
        doNode newNode = new doNode(user,null,null);
        doNode preNode=this.node;
        doNode creNode=preNode.getNext();
        int count=1;
        while (creNode!=null){
            if (count==i){
                creNode.setPre(newNode);
                newNode.setPre(preNode);
                preNode.setNext(newNode);
                newNode.setNext(creNode);
                return;
            }
            preNode=creNode;
            creNode=cruNode.getNext();
            ++count;
        }
        if (creNode==null){
            System.out.println("并不能在末尾节点不可以插入");
        }
    }
}

class DoLinkListTest {
    public static void main(String[] args) {
        doLinkList doLinkList = new doLinkList();
        doLinkList.initDoLinkList();
        doLinkList.putNode("zhangshan",10).putNode("lishi",20).putNode("wangwu",21);
        doLinkList.delNode(1);
        doLinkList.insertNode("lihua",20,2);
        doLinkList.printNodeData();
    }
}

循环链表的实现(c语言实现)

#include <stdio.h>
#include <stdlib.h>

//这个源代码设计的是循环链表,包括设计的尾指针,两个循环链表的组合,但是链表的其他操作我们在这就不去写了
// 设计这个尾指针的目的是在对表的首尾部分进行操作的时候可以降低时间复杂度
typedef struct Book {
	char bookname[20];
	char author[10];
}Book;

typedef struct Node{
	Book book;
	struct Node *next;
}Node;

typedef struct LoopLinkList{
	Node *headNode;
	Node *tailNode;
} *LoopList;
//初始化循环列表 
void initLoopLinkList(LoopList *L){  //ok
	Node *headNode=(Node*)malloc(sizeof(Node));
	*L=(LoopList)malloc(sizeof(LoopList));
	(*L)->headNode=headNode;
	(*L)->tailNode=headNode;
}
//向循环链表中添加结点 
void putNode(Book book,LoopList L){   //ok
	Node *node=(Node*)malloc(sizeof(Node));
	node->book=book;
	L->tailNode->next=node;
	L->tailNode=node;
	node->next=L->headNode;
}

//将第二个循环链表添加到第一个循环链表后
LoopList  connectTable(LoopList L1,LoopList L2){   //0k
	L1->tailNode->next=L2->headNode->next;
	free(L2->headNode);
	L2->tailNode->next=L1->headNode;
	L1->tailNode=L2->tailNode;
	return L1;
}

//打印链表中的数据 
void printNodeData(LoopList L){      //ok
	Node *node=L->headNode->next;
	while(node!=L->headNode){
		printf("bookname:%s;author:%s\n",node->book.bookname,node->book.author);
		node=node->next;
	}
	if(!node) printf("你所给的是个空链表!\n"); 
}
//获取循环列表的长度 
int getSize(LoopList L){    //ok
	Node *node=L->headNode->next;
	int counter=0;
	while(L->headNode!=L->tailNode&&node!=L->headNode){
		node=node->next;
		++counter;
	}
	return counter;
}

int main(){
	LoopList L;
	initLoopLinkList(&L);
	Book book1={"java","zhangyu"};
	Book book2={"php","zhang"};
	Book book3={"c#","nan"};
	putNode(book1,L);
	putNode(book2,L);
	putNode(book3,L);
	printf("表1:\n");
	printNodeData(L);
	LoopList L2;
	initLoopLinkList(&L2);
	Book book4={"jaa","hangyu"};
	Book book5={"ph","zhng"};
	Book book6={"c","an"};
	putNode(book4,L2);
	putNode(book5,L2);
	putNode(book6,L2);
	printf("表2:\n");
	printNodeData(L2);
	LoopList list=connectTable(L,L2);
	printf("连接后的表:\n");
	printNodeData(list);
	system("pause");
	return 0;
}
单链表的重要操作实现图解

循环链表的实现(java语言实现)

package linearList;

/*
* 循环链表的实现
* */
class Book{
    String bookname;
    int Bookid;

    public Book(String bookname, int bookid) {
        this.bookname = bookname;
        Bookid = bookid;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public int getBookid() {
        return Bookid;
    }

    public void setBookid(int bookid) {
        Bookid = bookid;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookname='" + bookname + '\'' +
                ", Bookid=" + Bookid +
                '}';
    }
}
class LpNode {
    Book book;
    LpNode next;

    public LpNode(Book book, LpNode next) {
        this.book = book;
        this.next = next;
    }

    public void setNext(LpNode next) {
        this.next = next;
    }

    public LpNode getNext() {
        return next;
    }
}
public class LoopLinkList {
    LpNode head;
    LpNode tail;

    //初始化循环列表
    void initLoopLinkList(){
        this.head=null;
        this.tail=null;
    }
    //向循环链表中添加结点
    LoopLinkList putNode(String bookname,int booid){
        Book book = new Book(bookname, booid);
        LpNode newNode = new LpNode(book,this.tail);
        if (this.head==null){
            this.head=newNode;
            this.tail=newNode;
        }else{
            this.tail=newNode;
            this.head.setNext(this.tail);
        }
        return this;
    }
    //将第二个循环链表添加到第一个循环链表后
    void connectTable(LoopLinkList linkList){
        linkList.head.setNext(this.tail);
        this.head.setNext(linkList.tail);
    }
    //打印链表中的数据
    int printNodeData(){
        LpNode tmppoint=this.head;
        int count=0;
        do {
            System.out.println(tmppoint.book.toString());
            tmppoint=tmppoint.getNext();
            ++count;
        }while (tmppoint!=this.head);
        return count;
    }
}

class LoopLinkListTest {
    public static void main(String[] args) {
        LoopLinkList loopLinkList = new LoopLinkList();
        loopLinkList.initLoopLinkList();
        loopLinkList.putNode("java入土",10).putNode("php",1).putNode("kl",10);
        loopLinkList.printNodeData();
        System.out.println("--------------");
        LoopLinkList loopLinkList1 = new LoopLinkList();
        loopLinkList1.putNode("lp",00).putNode("kk",10);
        loopLinkList.connectTable(loopLinkList1);
        loopLinkList.printNodeData();


    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值