链表java与c++实现
来源:互联网 发布:网络作家作品集 编辑:程序博客网 时间:2024/06/05 22:18
package pack;class Node { int num; //数据域 Node next; //指针域 Node(int num) { this.num = num; } }class List { Node head = null; //头结点 Node tail = null; //尾结点 static int len = 0; //链表长度 boolean createNodeList() { //创建链表 head = new Node(0); if(head == null) { System.out.println("error"); return false; } head.next = null; tail = head; len++; return true; } boolean addNode(Node node) { //末尾增加结点 if(node == null) { System.out.println("invaild input"); return false; } tail.next = node; tail = node; tail.next = null; len++; return true; } void lengthofNode() { //输出链表长度 int num = 0; Node p = head; while(p != null) { num++; p = p.next; } System.out.println("链表长度:"+num); } void printNode() { //输出链表数据 Node p = head; while( p!= null){ System.out.println(p.num); p = p.next; } } boolean insertNode(int index, Node node) { //根据结点号码0,1,2...插入结点 if(index<=0 && index>=len) { System.out.println("invaid input"); return false; } Node p = head; for(int i = 0; i<index-1; i++) { p = p.next; } node.next = p.next; p.next = node; len++; return true; } boolean getNode(int index) { //获取结点存放数据 if(index<0 && index>len) { System.out.println("invaild input"); return false; } Node p = head; for(int i = 0;i<index; i++) { p = p.next; } System.out.println(p.num); return true; } boolean deleteNode(int number) { //删除指定数字的结点 if(number > len) { System.out.println("invaild input"); return false; } Node p = head; while(p.next!=null && p.num!=number-1) { p = p.next; } p.next = p.next.next; len--; return true; } boolean destroyNode() { //销毁链表 if (head == null) { System.out.println("invaild head node"); return false; } Node p = head; while(p != null) { p = p.next; head = null; head = p; } return true; } void reverseNode() { //倒序 Node pre, now, next; pre = null; now = head; while(now != null) { next = now.next; now.next = pre; pre = now; now = next; } } void sortNode() { //冒泡排序 Node p = null; Node q = null; for (p = head; p.next != null; p = p.next) { for (q = p.next; q!= null; q = q.next) { if (p.num > q.num) { p.num = p.num^q.num; q.num = p.num^q.num; p.num = p.num^q.num; } } } }}public class Main { public static void main(String[] args) { List li = new List(); Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); li.createNodeList(); li.addNode(node1); li.addNode(node2); li.addNode(node3); li.addNode(node4); li.addNode(node5); //li.insertNode(3, node5); //li.getNode(3); //li.deleteNode(4); li.sortNode(); li.lengthofNode(); li.printNode(); }}
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;struct Node { int num; Node *pNext;};Node* head = NULL; //头结点Node* tail = NULL; //尾节点int len = 0; //链表长度bool createNodeList() { //创建链表 head = (Node*)malloc(sizeof(Node)); if (head == NULL) { cout << "malloc error" << endl; return false; } else { head->num = 0; head->pNext = NULL; } tail = head; return true;}bool addNode(Node *node) { //末尾增加结点 if (node == NULL) return false; tail->pNext = node; tail = node; tail->pNext = NULL;}void printNode(Node *head) { //输出链表数据 Node *p = head; while (p != NULL) { cout << p->num << " "; p = p->pNext; }}void lengthOfNode() { //输出链表长度 Node *p = head; while (p != NULL) { len++; p = p->pNext; } cout << "链表长度:"<< len << endl;}bool insertNode(int index, Node *node) { //根据结点号码0,1,2...插入结点 if (index <= 0 && index >= len) { cout << "invaild index" << endl; return false; } Node *p = head; int num = 0; while (p!=NULL && num != (index-1)) { num++; p = p->pNext; } Node *q = p->pNext; p->pNext = node; node->pNext = q; return true;}bool getNode(int number) { //获取结点存放数据 if (number > len) { cout << "invaild number" << endl; return false; } Node *p = head; for(int i = 0; i<number; i++){ p = p->pNext; } cout << p->num << endl; return true;}bool deleteNode(int number) { //删除指定数字的结点 if (number > len) { cout << "invaild number" << endl; return false; } Node *p = head; Node *q = NULL; while (p->num!=number && p != NULL) { q = p; //保存当前结点 p = p->pNext; if (p->num == number) { q->pNext = p->pNext; //删除中间结点 free(p); p = NULL; break; } } return true;}bool destroyNode() { //销毁链表 if (head == NULL) { cout << "invaild head node" << endl; return false; } Node *p = head; while (p != NULL) { p = p->pNext; free(head); head = p; } return true;}void reverseNode() { //倒序 Node *pre, *now, *next; pre = NULL; now = head; while(now != NULL) { next = now->pNext; now->pNext = pre; pre = now; now = next; } head = pre;}void sortNode() { //冒泡排序 Node *p = NULL; Node *q = NULL; for (p = head; p->pNext != NULL; p = p->pNext) { for (q = p->pNext; q!= NULL; q = q->pNext) { if (p->num > q->num) { p->num = p->num^q->num; q->num = p->num^q->num; p->num = p->num^q->num; } } }}int main() { createNodeList(); Node *n1 = (Node*)malloc(sizeof(Node)); n1->num = 1; n1->pNext = NULL; addNode(n1); Node *n2 = (Node*)malloc(sizeof(Node)); n2->num = 2; n2->pNext = NULL; addNode(n2); Node *n3 = (Node*)malloc(sizeof(Node)); n3->num = 3; n3->pNext = NULL; addNode(n3); Node *n4 = (Node*)malloc(sizeof(Node)); n4->num = 4; n4->pNext = NULL; addNode(n4); Node *n5 = (Node*)malloc(sizeof(Node)); n5->num = 5; n5->pNext = NULL; addNode(n5); //Node *n = (Node*)malloc(sizeof(Node)); //n->num = 100; //n->pNext = NULL; //insertNode(3, n); //deleteNode(3); //reverseNode(); /*printNode(head); reverseNode(); printNode(head);*/ lengthOfNode(); system("pause"); return 0;}
0 0
- 汉诺塔实现c与java
- 实现Java与C语言接口
- 实现Java与C语言接口
- 实现Java与C语言接口
- 实现Java与C语言接口
- JNI 实现java 与 c ++ 调用
- C与java实现命令行系统
- linux jni 实现JAVA与C 通讯
- C语言与Java实现:数制转换
- java与c通信实现方案
- C++,C#与JAVA实现多态的比较
- Android使用JNI实现Java与C之间传递数据
- Android使用JNI实现Java与C之间传递数据
- Android使用JNI实现Java与C之间传递数据
- Android使用JNI实现Java与C之间传递数据
- Android使用JNI实现Java与C之间传递数据
- Android JNI实现Java与C的数据传递
- Android使用JNI实现Java与C之间传递数据
- 硬盘容量换算公式
- loadrunner链接mysql数据库
- cookie的使用
- linux 关于进程查找删除的命令
- PAT1038 统计同成绩学生
- 链表java与c++实现
- firefxo 关闭导入向导
- 用GirdView来实现复杂显示的日历
- VC下执行DOS命令并得到输出
- EJB 会话Bean的生命周期
- Swift数组
- struts2 文件上传 三种方式
- jquery 如何修改itle的内容
- js点击某个图标或按钮弹出文件选择框