概述
首先看main函数中双向链表的用法:
#include <stdio.h>
#include <stdlib.h>
#include "dlist.h"
int main(void) {
LinkedList* list = newLinkedList();
list->addLast(list, 10)->addLast(list, 20)->addLast(list, 30)->addLast(list, 40)->addLast(list, 50);
ListIter* iter = list->iterator(list);
while(iter->hasNext(iter)) {
ListNode* node = iter->next(iter);
printf("%d, ", node->val);
}
printf("n");
int val;
list->removeFirst(list, &val);
printf("the first element %d has been removed!n", val);
iter = list->iterator(list);
while(iter->hasNext(iter)) {
ListNode* node = iter->next(iter);
printf("%d, ", node->val);
}
printf("n");
int len = list->size(list);
printf("current length is %dn", len);
return 0;
}
输出结果如下:
10, 20, 30, 40, 50,
the first element 10 has been removed!
20, 30, 40, 50,
current length is 4
再看头文件"dlist.h"中的内容:
#ifndef DLIST_H_
#define DLIST_H_
typedef struct ListNode {
struct ListNode *prev;
struct ListNode *next;
int val;
} ListNode;
typedef struct ListIter {
ListNode * current;
ListNode* (*next)(struct ListIter * iter);
int (*hasNext)(struct ListIter *iter);
} ListIter;
typedef struct LinkedList {
ListNode *head;
ListNode *tail;
int len;
int (*size)(struct LinkedList *list);
struct LinkedList* (*addLast)(struct LinkedList *list, int val);
int (*removeFirst)(struct LinkedList *list, int *val);
ListIter* (*iterator) (struct LinkedList *list);
} LinkedList;
ListNode* newListNode(int val);
ListNode* newListNode2(ListNode *prev, ListNode *next, int val);
LinkedList* newLinkedList(void);
int size(LinkedList *list);
LinkedList* addLast(LinkedList *list, int val);
int removeFirst(LinkedList *list, int *val);
ListIter* iterator(LinkedList *list);
ListNode* next(ListIter *iter);
int hasNext(ListIter *iter);
#endif /* DLIST_H_ */
最后是源文件"dlist.c"中的实现:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include "dlist.h"
ListNode* newListNode(int val) {
ListNode *node = (ListNode*)malloc(sizeof(ListNode));
if(node==NULL) return NULL;
node->prev = NULL;
node->next = NULL;
node->val = val;
return node;
}
ListNode* newListNode2(ListNode *prev, ListNode *next, int val) {
ListNode *node = (ListNode*)malloc(sizeof(ListNode));
if(node==NULL) return NULL;
node->prev = prev;
node->next = next;
node->val = val;
return node;
}
LinkedList* newLinkedList(void) {
LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
if(list==NULL) return NULL;
list->head = NULL;
list->tail = NULL;
list->len = 0;
list->addLast = addLast;
list->removeFirst = removeFirst;
list->iterator = iterator;
list->size = size;
return list;
}
int size(LinkedList *list) {
if(list==NULL) {
errno = 22; //22代表无效的参数
perror("NULL pointer Exception");
exit(-1);
}
return list->len;
}
LinkedList* addLast(LinkedList *list, int val) {
if(list==NULL) {
errno = 22; //22代表无效的参数
perror("NULL pointer Exception");
exit(-1);
}
ListNode* node = newListNode(val);
if(list->tail==NULL) {
list->head = list->tail = node;
}
else {
node->prev = list->tail;
list->tail = list->tail->next = node;
}
list->len++;
return list;
}
int removeFirst(LinkedList *list, int *val) {
if(list==NULL || val==NULL) {
errno = 22; //22代表无效的参数
perror("NULL pointer Exception");
exit(-1);
}
*val = list->head->val;
if(list->head==list->tail) {
free(list->head);
list->head = NULL;
list->tail = NULL;
}
else {
ListNode* tmp = list->head;
list->head = list->head->next;
free(tmp);
}
list->len--;
return 0;
}
ListIter* iterator(LinkedList *list) {
if(list==NULL) {
errno = 22; //22代表无效的参数
perror("NULL pointer Exception");
exit(-1);
}
ListIter* listIter = (ListIter*)malloc(sizeof(ListIter));
if(listIter==NULL) return NULL;
listIter->current = list->head;
listIter->hasNext = hasNext;
listIter->next = next;
return listIter;
}
ListNode* next(ListIter *iter) {
if(iter==NULL) {
errno = 22; //22代表无效的参数
perror("NULL pointer Exception");
exit(-1);
}
ListNode *tmp = iter->current;
iter->current = iter->current->next;
return tmp;
}
int hasNext(ListIter *iter) {
if(iter==NULL) {
errno = 22; //22代表无效的参数
perror("NULL pointer Exception");
exit(-1);
}
if(iter->current != NULL)
return 1;
else
return 0;
}
以上代码只是粗略地模拟了一下面向对象的一点点特性,水平有限,如有不当之处,还望指正!
最后
以上就是酷炫酒窝为你收集整理的C语言实现面向对象风格带迭代器的双向链表的全部内容,希望文章能够帮你解决C语言实现面向对象风格带迭代器的双向链表所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复