查看: 2416|回复: 32
|
Linked- List
[复制链接]
|
|
请教linked- list 的教学方法~ 根本不能明白书上写的是什么!
求教~~ |
|
|
|
|
|
|
|
发表于 22-3-2013 09:55 AM
|
显示全部楼层
linked list 有很多种啊,问题再具体一些 ,不然帮不到。不过先说好,我不是很PRO的PROGRAMMER,就我用过的就帮而已,分享经验 
Linked list其中一个例子就好像排队那样,你安排人(就是DATA)队伍的次序,谁排在最前,谁排在最后,谁可以插队等。
它跟ARRAY有点相似,但它比较FLEXIBLE |
|
|
|
|
|
|
|
发表于 5-4-2013 12:56 AM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 9-4-2013 04:59 PM
|
显示全部楼层
天魔神 发表于 22-3-2013 09:55 AM 
linked list 有很多种啊,问题再具体一些 ,不然帮不到。不过先说好,我不是很PRO的PROGRAMMER,就我用过的 ...
quene, 是什么来的? 我读data structure 有读到这个~~
|
|
|
|
|
|
|
|
发表于 9-4-2013 06:55 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 9-4-2013 07:14 PM
|
显示全部楼层
歩む 发表于 9-4-2013 06:55 PM 
dynamic的array来的,没什么特别的。
它是怎样的?可以给我一个concept它是什么吗?
|
|
|
|
|
|
|
|
发表于 9-4-2013 07:23 PM
|
显示全部楼层
莲花 发表于 9-4-2013 07:14 PM 
它是怎样的?可以给我一个concept它是什么吗?
用讲的你可能比较难理解诶……
我现在不在公司,电脑只有eclipse,我用Java写个sample给你看好吗?
|
|
|
|
|
|
|
|
发表于 9-4-2013 07:30 PM
|
显示全部楼层
莲花 发表于 9-4-2013 07:14 PM 
它是怎样的?可以给我一个concept它是什么吗?
先跟你讲基本的东西先。
Linked List是一种Data Structure,说它是dynamic的arrays也是有点不对。
这种data structure用来保存一整个队列的data,但不是通过index来存取data,而是通过不断地traversal来找到你要的东西。
也就是说,你在一个LinkedList里面存了3个data,你要拿index为2的data,就会先从找index 0的下一个,然后再找index 1的下一个。
因为Linked List在insert或者add data进去时只需要记录这个新的data的上一个和下一个data,所以insert的时候会比别的data structure快。
|
|
|
|
|
|
|
|
发表于 9-4-2013 07:56 PM
|
显示全部楼层
这里是sample,因为是临时写的,很乱,但是基本的功能都有了,你自己看看吧,那些exception我也懒惰catch了。
- public class LinkedList
- {
- private class Node
- {
- public Node nextNode;
- public Object data;
-
- public Node(Object data)
- {
- this.data = data;
- }
- }
-
- private Node firstNode;
-
- public LinkedList()
- {
- firstNode = new Node(0);
- }
-
- public boolean add(int index, Object object)
- {
- if (index < ((Integer)firstNode.data))
- {
- Node current = firstNode;
- for (int i = 0; i < index; i++)
- current = current.nextNode;
-
- Node node = new Node(object);
- node.nextNode = current.nextNode;
- current.nextNode = node;
- firstNode.data = ((Integer)firstNode.data) + 1;
-
- return true;
- }
-
- return false;
- }
-
- public boolean add(Object object)
- {
- Node current = firstNode;
- while (current.nextNode != null)
- current = current.nextNode;
-
- Node node = new Node(object);
- current.nextNode = node;
- firstNode.data = ((Integer)firstNode.data) + 1;
-
- return true;
- }
-
- public Object get(int index)
- {
- Node current = firstNode.nextNode;
- for (int i = 0; i < index; i++)
- current = current.nextNode;
-
- return current.data;
- }
-
- public Object remove(int index)
- {
- Node current = firstNode.nextNode;
- for (int i = 0; i < index - 1; i++)
- current = current.nextNode;
-
- Node result = current.nextNode;
- current.nextNode = null;
- return result;
- }
-
- public boolean contains(Object object)
- {
- Node current = firstNode;
- while (current.nextNode != null)
- {
- current = current.nextNode;
- if (current.data.equals(object))
- return true;
- }
-
- return false;
- }
-
- public boolean isEmpty()
- {
- return firstNode.nextNode == null;
- }
-
- public void clear()
- {
- firstNode = new Node(0);
- }
- }
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 9-4-2013 08:10 PM
|
显示全部楼层
歩む 发表于 9-4-2013 07:56 PM 
这里是sample,因为是临时写的,很乱,但是基本的功能都有了,你自己看看吧,那些exception我也懒惰catch了 ...
真的谢谢你,我现在只学着c programming, 虽然不是很明白,但还是很谢谢你~
它跟last in last out 有什么关系?
|
|
|
|
|
|
|
|
发表于 9-4-2013 10:35 PM
|
显示全部楼层
莲花 发表于 9-4-2013 08:10 PM 
真的谢谢你,我现在只学着c programming, 虽然不是很明白,但还是很谢谢你~
它跟last in last out 有 ...
last in last out 说的是queue,不是LinkedList,如字面上所说,就像排队一样,最后一个进来的只能最后一个出去。
|
|
|
|
|
|
|
|

楼主 |
发表于 10-4-2013 11:09 PM
|
显示全部楼层
歩む 发表于 9-4-2013 10:35 PM 
last in last out 说的是queue,不是LinkedList,如字面上所说,就像排队一样,最后一个进来的只能最后一 ...
ohh, queue 还有stack 的 coding 会有什么分别?
linked list 的 source code 是不是struct 里面又有struct*next node ?
|
|
|
|
|
|
|
|
发表于 11-4-2013 12:17 AM
来自手机
|
显示全部楼层
莲花 发表于 10-4-2013 11:09 PM
ohh, queue 还有stack 的 coding 会有什么分别?
linked list 的 source code 是不是struct 里面又有str ...
嗯,有那个nextnode的原因就是linkedlist就是用这种第一个记着下一个node,下一个node记得再下一个node的方式来保存data的。
queue也就和你说的一样last in last out,和排队一样,stack就不同,可以理解为一个罐子,我放了a进去,再放b进去,最后是c,接着我就拿不到a和b了,只能先把c拿出来,就是last in first out。
我也只有一年工作经验,恐怕说的不完全对,你参考一下就行了。 |
|
|
|
|
|
|
|

楼主 |
发表于 11-4-2013 05:18 PM
|
显示全部楼层
歩む 发表于 11-4-2013 12:17 AM 
嗯,有那个nextnode的原因就是linkedlist就是用这种第一个记着下一个node,下一个node记得再下一个node的 ...
在c programming 的 source code 里,他们有什么分别?
谢谢~~
|
|
|
|
|
|
|
|
发表于 11-4-2013 06:22 PM
来自手机
|
显示全部楼层
c的话,因为是没有oop性质的language,所以大概是这样:
struct Node
{
void* data;
struct Node* next;
}
你可以看到,非常简陋,基本上我想拿到第12个node我就需要for loop12次。 |
|
|
|
|
|
|
|
发表于 11-4-2013 06:23 PM
来自手机
|
显示全部楼层
莲花 发表于 11-4-2013 05:18 PM
在c programming 的 source code 里,他们有什么分别?
谢谢~~
还有就是,我觉得你以其学着c,不如直接学c++之类的oop language,你会爱上oop的。 |
|
|
|
|
|
|
|
发表于 12-4-2013 07:47 PM
|
显示全部楼层
歩む 发表于 11-4-2013 06:23 PM 
还有就是,我觉得你以其学着c,不如直接学c++之类的oop language,你会爱上oop的。
残念,现在很多学院/大学的不长进的教师们还在教着C, 而且还是Console C。。。
|
|
|
|
|
|
|
|
发表于 12-4-2013 08:09 PM
|
显示全部楼层
莲花 发表于 9-4-2013 08:10 PM 
真的谢谢你,我现在只学着c programming, 虽然不是很明白,但还是很谢谢你~
它跟last in last out 有 ...
Last In Last Out, 缩写LILO,也叫做First In First Out (FIFO),也有人称之为Pipe, 因为存入在里面的数据就好像流入水喉的水,先进入的那一滴水会先从另一边流出来。
First In Last Out/Last In First Out,也称为Stack,因为数据就像一层层的货物一般,先存进去的会被后来的压住,要把东西拿出来就得把后面存进去的先拿出来。
两者之间的差别只是在于你的Data Pointer(指向要优先拿出来的数据的Pointer)如何运作,Pipe的Data Pointer永远指向数据的最前端(D0),每个数据都会有一个指标指向下一个数据(D0指向D1,D1指向D2,D2指向D3,以此类推,直到最后的Dn后面没有东西),当你提取数据的时候,先被提取的将会是D0,这时Data Pointer就会被更新,指向D1,当D1 被提取之后就会被更新去指向D2,etc...
Stack则是相反,Data Pointer永远指向最后进去的数据,自然在提取数据时就会优先提取最后一个。
Stack和Pipe都可以是Link List, 不过 Linked List是自由的连接数据,就像一条链子一般,Data Pointer可以自由移动(如果是Bi-directional Linked List,可以双向,也就是前后移动,Single-directional Linked List则只能单向移动罢了),同时Linked List也可以是Linear List,也就是像Pipe一样,水喉一条,只是在Linked List里面,你可以自由移动Data Pointer,随意的提取任何数据,也可以将数据塞入任何位置。另一种Linked List则是Circular List,头尾相连,没有所谓哪里开头哪里结束。
|
|
|
|
|
|
|
|

楼主 |
发表于 14-4-2013 10:57 AM
|
显示全部楼层
geekman 发表于 12-4-2013 08:09 PM 
Last In Last Out, 缩写LILO,也叫做First In First Out (FIFO),也有人称之为Pipe, 因为存入在里面的 ...
谢谢你~~
|
|
|
|
|
|
|
|
发表于 15-4-2013 12:45 AM
|
显示全部楼层
嗨,您好!我不知道您到了什么程度,如果是初学者的话,建议学习linked list之前把pointer的concept搞好,对你读code有很大的帮助。以下是我以前写的linked list虽然不是很完整而写的目的是要明白什么是linked list。- /* Linked List act as memory effciency array.
- * Not like array, linked list does not need pre-defined size.
- * It is dynamic allocatiion memory block only when caller need to store more data */
- //linked_list.c
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "linked_list.h"
- LinkedListItem* linked_list_item_create() {
- LinkedListItem* linked_list_item = NULL;
-
- linked_list_item = (LinkedListItem*)malloc(sizeof(*linked_list_item));
-
- memset(linked_list_item, 0, sizeof(linked_list_item));
-
- return linked_list_item;
- }
- void linked_list_item_destroy(LinkedListItem* linked_list_item) {
-
- if(!linked_list_item)
- return;
-
- free(linked_list_item);
-
- }
- void linked_list_append(LinkedList* linked_list, void* data) {
-
- LinkedListItem* linked_list_item = NULL;
-
- if((!linked_list) || (!data))
- return;
-
- linked_list_item = linked_list_item_create();
- linked_list_item->data = data;
-
- if(linked_list->len == 0) {
- linked_list->head = linked_list_item;
- linked_list->tail = linked_list_item;
- }
- else {
- /* Modify next apppender link */
- linked_list_item->previous = linked_list->tail;
- linked_list_item->next = NULL;
-
- /* Modify previous apppender link */
- linked_list->tail->next = linked_list_item;
-
- /* update tail item */
- linked_list->tail = linked_list_item;
- }
-
- linked_list->len++;
- }
- void* linked_list_extract_first(LinkedList* linked_list) {
-
- LinkedListItem* previous = NULL;
- void* data = NULL;
-
- if(!linked_list || linked_list->head == NULL)
- return NULL;
-
- data = linked_list->head->data;
-
- if(linked_list->head->next == NULL) {
- linked_list_item_destroy(linked_list->head);
- linked_list->head = NULL;
- linked_list->tail = NULL;
- }
- else {
- linked_list->head = linked_list->head->next;
- linked_list_item_destroy(linked_list->head->previous);
- linked_list->head->previous = NULL;
- }
-
- linked_list->len--;
-
- return data;
- }
- LinkedList* linked_list_create() {
- LinkedList* linked_list = NULL;
-
- linked_list = (LinkedList*)malloc(sizeof(*linked_list));
-
- memset(linked_list, 0, sizeof(linked_list));
-
- linked_list->append = linked_list_append;
- linked_list->extract_first = linked_list_extract_first;
- }
复制代码- #ifndef _LINKED_LIST_H
- #define _LINKED_LIST_H
- //linked_list.h
- typedef struct LinkedList LinkedList;
- typedef struct LinkedListItem LinkedListItem;
- struct LinkedListItem {
- LinkedListItem* previous;
- LinkedListItem* next;
- void* data;
- };
- /* Data structure describe linked list properties and its general function */
- struct LinkedList {
- LinkedListItem* head;
- LinkedListItem* tail;
- unsigned int len;
-
- /* General function */
- void (*append)(LinkedList* linked_list, void* data);
- void* (*extract_first)(LinkedList* linked_list);
- };
- LinkedList* linked_list_create();
- void* linked_list_extract_first(LinkedList* linked_list);
- void linked_list_append(LinkedList* linked_list, void* data);
- #endif
复制代码- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "linked_list.h"
- //test.c
- int main() {
-
- LinkedList* ll = NULL;
- char* data = NULL;
-
- ll = linked_list_create();
-
- if(ll->extract_first(ll) == NULL) {
- printf("is NULL\n");
- }
-
- data = "hello";
-
- ll->append(ll, data);
-
- data = "world";
-
- ll->append(ll, data);
-
- printf("data = %s\n", (char*)ll->extract_first(ll));
- printf("data = %s\n", (char*)ll->extract_first(ll));
-
- if(ll->extract_first(ll) == NULL) {
- printf("is NULL\n");
- }
-
- return 0;
- }
复制代码 简单来说,linked list就像array, 只是array 要预先defined size, 比如 unsigned int id[10]; 那么最多就只可以储存10个id。
linked list 是无限量的,只要电脑还有足够的memory。
还有就是其实C语言并不是大家所说的无法实现OOP。
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|