单链表
C实现
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE{
int data;
NODE *next;
}Node;
// 删除节点
void deleteNode(Node *pHead, int index){
int count = 0;
while (pHead->next != NULL && count < index)
{
pHead = pHead->next;
count++;
}
Node *delNode = pHead->next;
pHead->next = delNode->next;
free(delNode);
}
// 更新节点
void updateNode(Node *pHead,Node *node,int index){
int count = 0;
while (pHead->next != NULL && count < index)
{
pHead = pHead->next;
count++;
}
Node *delNode = pHead->next;
pHead->next = delNode->next;
free(delNode);
node->next = pHead->next;
pHead->next = node;
}
// 增加节点
void addNode(Node *pHead,Node *node,int index){
int count = 0;
while (pHead->next != NULL && count < index)
{
pHead = pHead->next;
count++;
}
node->next = pHead->next;
pHead->next = node;
}
// 创建链表(头插法)
void createList1(Node *pHead){
Node *p;
Node *next = (Node *)malloc(sizeof(Node));
next->data = 45;
next->next = NULL;
int i;
for (i = 0; i < 10; i ++){
p = (Node *)malloc(sizeof(Node));
p->data = i;
if (i == 0)
{
p->next = NULL;
}
else{
p->next = next;
}
pHead->next = p;
next = p;
}
}
// 创建链表(尾插法)
void createList(Node *pHead){
printf("createList %p\n", pHead);
Node *p;
int i;
for (i = 0; i < 10;i++){
p = (Node *)malloc(sizeof(Node));
if (p == NULL)
{
printf("申请失败\n");
break;
}
p->data = i;
p->next = NULL;
//printf("%d\t%p\n",p->data,*p);
pHead->next = p;
pHead = p;
}
}
// 遍历链表
void printList(Node *pHead){
if (pHead == NULL){
printf("链表为空\n");
}
while (pHead != NULL){
printf("%d\n",pHead->data);
pHead = pHead->next;
}
printf("链表遍历完毕\n");
}
//反转链表
void reversalList(Node *pHead){
Node *pre = pHead->next;
Node *last = pre->next;
Node *temp = last->next;
pre->next = NULL;
while (temp != NULL)
{
last->next = pre;
pre = last;
last = temp;
temp = temp->next;
}
last->next = pre;
pHead->next = last;
}
Java实现
/**
* 创建列表
* @return
*/
private static Node createList(Node head){
Node temp = new Node();
for (int i = 0; i < 2; i++) {
Node node = new Node();
node.data = (i+1) + "";
node.next = null;
if(i == 0){
head.next = node;
}
temp.next = node;
temp = node;
}
return head;
}
/**
* 遍历列表
* @param head
*/
private static void printList(Node head){
if(head == null || head.next == null){
return ;
}
while(head.data != null){
System.out.print(head.data+"\t");
if(head.next == null){
break;
}
head = head.next;
}
}
/**
* 在链表中添加节点
* @param node 要添加的节点
* @param index 要添加的位置
* @param head 被添加的链表
*/
private static void addNode(Node node,int index,Node head){
if(index < 0 || index > getLength(head)){
System.out.println("插入的位置不对,请重试");
return;
}
int count = 0;
while(count < index){
head = head.next;
count ++;
}
node.next = head.next;
head.next = node;
}
/**
* 删除节点
* @param index 要删除节点的位置
* @param head 被删的链表
*/
private static void deleteNode(int index,Node head){
if(index < 0 || index > getLength(head)){
System.out.println("要删除的位置不对,请重试");
return;
}
int count = 0;
while(count < index){
head = head.next;
count++;
}
head.next = head.next.next;
}
/**
* 更新链表某处的节点
* @param node 需要更新的新节点
* @param index 需要更新的位置
* @param head 被更新的链表
*/
private static void updateNode (Node node,int index,Node head){
if(index < 0 || index > getLength(head)){
System.out.println("要更新的位置不对,请重试");
return;
}
int count = 0;
while(count < index){
head = head.next;
count ++;
}
head.next = head.next.next;
node.next = head.next;
head.next = node;
}
/**
* 获取链表长度
* @param head
* @return
*/
private static int getLength(Node head){
Node list = head.next;
int length = 0;
while(list.next != null){
length ++;
list = list.next;
}
return (length+1);
}
/**
* 反转链表
* @param node
*/
private static Node reversalList(Node head){
Node mid = head.next;
Node next = mid.next;
Node temp = next.next;
mid.next = null;
while(temp != null){
next.next = mid;
mid = next;
next = temp;
temp = temp.next;
}
next.next = mid;
head = next;
return head;
}
static class Node{
public String data;
public Node next;
}
双链表
C实现
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
typedef struct NODE{
int data;
NODE *previous;
NODE *next;
}Node;
// 创建链表
void crateList(Node *head,Node *tail){
Node * temp = head;
for (int i = 0; i < 10; i++){
Node *node = (Node *)malloc(sizeof(Node));
node->data = i;
node->previous = temp;
node->next = NULL;
temp->next = node;
temp = node;
}
temp->next = tail;
tail->previous = temp;
}
// 遍历链表
void printList(Node *head,Node * tail){
if (head == NULL){
printf("链表为空");
return;
}
Node *node = head->next;
while (node->data != -2){
printf("%d\n", node->data);
node = node->next;
}
}
// 获取长度
int getLength(Node *head){
if (head == NULL){
printf("链表为空");
return 0;
}
int count = 0;
Node *temp = head->next;
while (temp->data != -2){
temp = temp->next;
count++;
}
return count;
}
// 增加节点
void addNode(Node *node,int index,Node *head,Node *tail){
if (index < 0 || index > getLength(head))
{
printf("插入位置不对");
return;
}
int count = 0;
while (count < index){
count++;
head = head->next;
}
node->previous = head;
node->next = head->next;
head->next->previous = node;
head->next = node;
}
// 删除节点
void deleteNode(int index, Node *head, Node *tail){
if (index < 0 || index > getLength(head))
{
printf("删除位置不对");
return;
}
int count = 1;
while (count < index){
count++;
head = head->next;
}
Node *temp = head->next;
head->next = temp->next;
temp->next->previous = head;
temp->previous = NULL;
temp->next = NULL;
free(temp);
}
Java实现
/**
* 创建链表
* @param head 链表的头结点
* @param tail 链表的尾结点
*/
private static void createList(Node head,Node tail){
Node front = head;
for (int i = 0; i < 10; i++) {
Node node = new Node();
node.data = (i + 1) + "";
node.previous = front;
node.next = null;
front.next = node;
front = node;
}
front.next = tail;
tail.previous = front;
}
/**
* 遍历链表
* @param head
*/
private static void printList(Node head){
if(head == null || head.next == null){
System.out.println("链表为空");
return;
}
System.out.println(head.data);
while(head.next != null){
head = head.next;
System.out.println(head.data);
}
}
/**
* 获取链表长度
* @param head
* @return
*/
private static int getLength(Node head){
if(head == null || head.next == null){
return 0;
}
int length = 0;
while(head.next != null){
head = head.next;
length++;
}
return length - 1;
}
/**
* 添加节点
* @param node 需添加的节点
* @param index 添加的位置
* @param head 被添加链表的头结点
* @param tail 被添加链表的尾节点
*/
private static void addNode(Node node,int index,Node head,Node tail){
if(index < 0 || index > getLength(head)){
System.out.println("插入的位置不对");
return;
}
if(index > getLength(head) / 2){
// 从尾部插入
int length = getLength(head);
while(length > index){
length --;
tail = tail.previous;
}
node.next = tail;
node.previous = tail.previous;
tail.previous.next = node;
tail.previous = node;
}else{
// 从头插入
int count = 0;
while(count < index){
count++;
head = head.next;
}
node.previous = head;
node.next = head.next;
head.next.previous = node;
head.next = node;
}
}
/**
* 删除节点
* @param index 要删除的位置
* @param head
* @param tail
*/
private static void deleteNode(int index,Node head,Node tail){
if(index <= 0 || index > getLength(head)){
System.out.println("插入的位置不对");
return;
}
if(index > getLength(head) / 2){
int length = getLength(head);
while(length > index){
length -- ;
tail = tail.previous;
}
Node temp = tail.previous;
temp.previous.next = tail;
tail.previous = temp.previous;
temp.previous = null;
temp.next = null;
}else{
int count = 0;
while(count < index - 1){
count++;
head = head.next;
}
Node temp = head.next;
temp.next.previous = head;
head.next = temp.next;
temp.previous = null;
temp.next = null;
}
}
static class Node{
public String data;
public Node previous;
public Node next;
}