lingceng 发表于 2013-1-26 12:30:46

Simple List with c and java

总结:
    从下面的代码可以看出,C中几乎所有函数都需要持有list指针,而且在使用上也十分麻烦,需要在主函数中维护头指针。sdfdf
    Java用了三个文件,ListNode.java,List.java还有一个Test.java。从下面贴出的List.java的代码可以看出,函数实现相对简单一些,不必要维护list指针,因为有对象属性存在。这里可以看出一些面向对象编程的好处。

LinkedList.c:
#include <stdio.h> #include <stdlib.h>#include <string.h>typedef struct node_s {void *data; struct node_s *next;} NODE;NODE *list_create(void *data) {NODE *node;if (!(node = malloc(sizeof(NODE)))) {return NULL;}node->data = data;node->next = NULL;return node;}NODE *list_insert_after(NODE *node, void *data) {NODE *newnode;newnode = list_create(data);newnode->next = node->next;node->next = newnode;return newnode;}NODE *list_insert_beginning(NODE *list, void *data) {NODE *newnode;newnode = list_create(data);newnode->next = list;return newnode;}int list_remove(NODE *list, NODE *node) {while (list->next && list->next != node) {list = list->next;}if (list->next) {list->next = node->next;free(node);return 0;} else {return -1;}}int list_foreach(NODE *node, int (*func)(void *)) {while (node) {if (func(node->data) != 0) return -1;node = node->next;}return 0;}NODE *list_find(NODE *node, int (*func)(void *, void *), void *data) {while (node) {if (func(node->data, data) > 0) {return node;}node = node->next;}return NULL;}int printstring(void *s) { printf("%s\n", (char *)s);return 0;}int findstring(void *listdata, void *searchdata) {return strcmp((char*)listdata, (char *)listdata) ? 0 : 1;}int main() {NODE *list, *second, *inserted;NODE *match;/* Create initial elements of list */list = list_create((void *)"First");second = list_insert_after(list, (void *)"second");list_insert_after(second, (void *)"Third");printf("Initail list:\n");list_foreach(list, printstring);putchar('\n');/* Insert one extra element in the beginning */list = list_insert_beginning(list, "BeforeFirst");printf("After list_insert_beginning():\n");list_foreach(list, printstring);putchar('\n');/* Insert one extra element after second */inserted = list_insert_after(second, "AfterSecond"); printf("After list_insert_after():\n");list_foreach(list, printstring);putchar('\n');/* Remove the element */list_remove(list, inserted);printf("After list_remove():\n");list_foreach(list, printstring);putchar('\n');/* search */if ((match = list_find(list, findstring, "Third")) != NULL) {printf("Found \"Third\"\n");} else {printf("Did not find \"Third\"\n");}return 0;}

List.java
package com.lingceng;import com.lingceng.util.ListNode;public class List {private ListNode root;private int max = 10;private int number;public List() {this.root = null;this.number = 0;}public ListNode getRoot() {return this.root;}public int getNumber() {return this.number;}public int getMax() {return this.max;}public void setRoot(ListNode root) {this.root = root;}public void insertAfter(int value) {ListNode tmp = new ListNode(value);ListNode tmp2 = this.root;if (this.number == this.max) {System.out.println("insert full list");return;}while (tmp2.getNext() != null) {tmp2 = tmp2.getNext();}tmp2.setNext(tmp);this.number++;}public void insertBeginning(int value) {ListNode tmp = new ListNode(value, this.root);if (this.number == this.max) {System.out.println("insert full list");return;}this.root = tmp;this.number++;}public void removeIndex(int n) {ListNode tmp = this.root;if (this.root == null) {return;}if (n == 0) {this.root = this.root.getNext();return;}for(int i = n; i > 1; i--) {if(tmp.getNext() == null) {return;}tmp = tmp.getNext();}tmp.setNext(tmp.getNext().getNext());this.number--;}public void removeValue(int value) {ListNode tmp = this.root;if (this.root == null) {return;}if (this.root.getValue() == value) {this.root = this.root.getNext();return;}while (tmp.getNext().getValue() != value) {tmp = tmp.getNext();if (tmp.getNext() == null) {return;}}tmp.setNext(tmp.getNext().getNext());this.number--;}@Overridepublic String toString() {ListNode tmp = this.root;if (this.root == null) {return "Empty List";}StringBuffer sb = new StringBuffer();while (tmp != null) {sb.append(tmp.getValue());if (tmp.getNext() != null) {sb.append("->");}tmp = tmp.getNext();}return sb.toString();}}

所有Java代码见附件
页: [1]
查看完整版本: Simple List with c and java