参考文章:http://blog.csdn.net/zhangerqing/article/details/8796518
1. 单链表结构:
2. 插入和删除操作:
3. java实现
/**
*
*/
package com.demo;
/**
* @author Maggie
*
* @param <T>
*/
public class MyLinkedList<T>
{
/**
* set a inner calss: Node<T>
* @author Maggie
*
* @param <T>
*/
static class Node<T>
{
T data;
Node<T> next;
Node(T data, Node<T> next){
this.data = data;
this.next = next;
}
Node(T data){
this(data, null);
}
Node() {
}
}
/**
* create a empty LinkedList
*
* @return head
*/
public Node<T> create()
{
Node<T> head = new Node<T>();
head.next = null;
return head;
}
/**
* juge empty
*
* @param head
* @return
*/
public boolean isEmpty(Node<T> head)
{
if(null == head.next)
return true;
else
return false;
}
/**
* insert a data at the end position
*
* @param head
* @param data
*/
public void insert(Node<T> head, T data)
{
Node<T> p = head;
while(p.next != null)
{
p = p.next;
}
// insert data at the end position
Node<T> s = new Node<T>(data);
p.next = s;
}
/**
* traverse & print
*
* @param head
*/
public void print(Node<T> head)
{
Node<T> p = head;
if(isEmpty(head))
System.out.println("Empty LinkedList!");
else {
while(p.next != null)
{
p = p.next;
System.out.println(p.data + " ");
}
}
System.out.println();
}
/**
* delete the node
*
* @param head
* @param data
* @return data
*/
public T delete(Node<T> head, T data)
{
Node<T> m, p;
T val = null;
m = p = head;
if(this.isEmpty(head))
{
System.out.println("Empty LinkedList!");
return val;
}
// looking for the Node that node's data is data
while(p.next != null & p.data != data)
{
m = p;
p = p.next;
}
// if find out
if(p.data == data)
{
m.next = p.next;
val = p.data;
p = null;
System.out.println("找到了值为" + data + "的节点,并依据将其删除");
}
// not find it out
System.out.println("没有找到值为" + data + "的节点,无法删除");
return val;
}
/**
* index of
*
* @param head
* @param data
* @return index
*/
public int indexOf(Node<T> head, T data)
{
Node<T> p = head;
int index = 0;
while(p.next != null && p.data != data)
{
index++;
p = p.next;
}
// if do not meet the right Node, return -1
if(p.data ==data)
index -= 1;
else
index = -1;
return index;
}
}
4. C++实现
# include <String>
# include <iostream>
# include <cstdlib>
using namespace std;
struct Node
{
int val;
struct Node* next;
}
typedef Node* PNode;
PNode head = new Node;
head->next = NULL;
PNode p,s;
p=head;
int n;
while(cin>>n)
{
s = new Node;
s->val = n;
s->next = NULL;
p->next = s;
p=s;
}
return head;
int length(PNode head)
{
int n = 0;
PNode p = head;
while(p->next != NULL)
{
n++;
p = p->next;
}
return n;
}
bool delete_node(PNode head, int num)
{
PNode m,p;
m = p = head;
// 如果是一个空链表,则无法删除
if(p->next == NULL)
{
cout<<"链表为空,无法删除"<<endl;
return false;
}
else
{
while(p->next != NULL && p->val != num)
{
m = p;
p = p->next;
}
if(p->val == num)
{
m->next = p->next;
delete(p);
cout<<"找到"<<num<<"并将第一个"<<num<<"删除"<<endl;
return true;
}
else
{
cout<<"此链表不包含"<<num<<",无法删除"<<endl;
return false;
}
}
}
void insert(PNode head, int num)
{
PNode p = head;
// 先循环找到最后一个节点,
while(p->next != NULL)
{
p = p->next;
}
PNode s = new Node;
s->val = num;
s-next = NULL;
p->next = s;
}
void print(PNode head)
{
PNode p = head->next;
while(p != NULL)
{
cout<<p->val<<" ";
p = p->next;
}
cout<<endl;
}
int main()
{
PNode head = creat();
print(head);
int n = length(head);
cout<<n<<endl;
delete_node(head, 1);
insert(head, 25);
insert(head, 100);
print(head);
while(1)
{}
}