C and Pointers (continued)

Source: Internet
Author: User
Tags definition

Objective

The previous "C" and "pointers" may be about C and pointers to some of the content does not say through, down to write a list of the implementation, of course, but also with C function pointers to simulate OO structure to do. The list structure itself is more complex (regarding the use of pointers), so this example may be clearer. The reason for choosing the list is that you must have contacted this simple data structure at school, and it may be easier to understand it from a more familiar example.

Interface definition

You can first look at the definition of the interface, similar to Java or C #:

/*
 * File:   IList.h
 * Author: juntao.qiu
 *
 * Created on May 22, 2009, 2:51 PM
 */ 

#ifndef _ILIST_H
#define    _ILIST_H 

typedef struct node{
    void *data;
    struct node *next;
}Node; //定义List中的元素类型,void * 相当于C中的泛型,可以支持任何结构的节点

typedef struct list{
    struct list *_this;
    Node *head;
    int size;
    void (*insert)(void *node);
    void (*drop)(void *node);
    void (*clear)();
    int (*getSize)();
    void* (*get)(int index);
    void (*print)();
}List; //用head (Node)来维护链表的链!

void insert(void *node);
void drop(void *node);
void clear();
int getSize();
void* get(int index);
void print();

#endif    /* _ILIST_H */

Interface to define all the exposed methods, as with all the list structures, we define the

void insert(node);//插入一个新的节点到List
void drop(node);//删除一个指定的节点
void clear();//清空List
int getSize();//取到List的大小
void* get(int index);//取到指定位置的元素
void print();//打印整个List,用于调试

Such a few methods.

Implementation of the interface

Then look at the implementation, as in the previous article, introduce a _this pointer to the tag list itself, and modify the state in the real object by referencing the pointer.

#include <stdlib.h>
#include "IList.h"

Node *node = NULL;
List *list = NULL;

/* Function declaration block, the function is already explained on the top.
void insert (void *node);
void drop (void *node);
void Clear ();
int GetSize ();
void print ();
void* get (int index);

/* Construction Method * *
List *listconstruction () {
List = (list*) malloc (sizeof (list));
node = (node*) malloc (sizeof (node));
list->head = node;
List->insert = insert;
List->drop = drop;
List->clear = clear;
list->size = 0;
List->getsize = GetSize;
List->get = get;
List->print = print;
List->_this = list;

Return (list*) List;
}

void Listdeconstruction () {
}

Insert node, size increased by 1
void insert (void *node) {
Node *current = (node*) malloc (sizeof (node));
current->data = node;
Current->next = list->_this->head->next;
List->_this->head->next = current;
(list->_this->size) + +;
}

Delete a node, size minus 1
void drop (void *node) {
Node *t = list->_this->head;
Node *d = NULL;
int i = 0;
For (I;i < list->_this->size;i++) {
D = list->_this->head->next;
if (D->data = = ((node*) Node)->data) {
List->_this->head->next = d->next;
Free (d);
(list->_this->size)--;
Break
}else{
List->_this->head = list->_this->head->next;
}
}
List->_this->head = t;
}

Take to the node of the specified index
void* get (int index) {
Node *node = list->_this->head;
int i = 0;

if (Index > List->_this->size) {
return NULL;
}else{
For (I;i < index;i++) {
node = node->next;
}
if (node!= (node*) 0) {
Return node->data;
}else{
return NULL;
}
}
}

void Clear () {
Node *node = NULL;
int i = 0;
for (i;i< list->_this->size;i++) {
node = list->_this->head;
List->_this->head = list->_this->head->next;
Free (node);
}
list->_this->size = 0;
list = NULL;
}

int GetSize () {
Return list->_this->size;
}

For debugging purposes, like this getsize (), print (), you need to be aware that you cannot make any changes to the original pointer during the call.
Otherwise, unpredictable errors may occur.
void print () {
Node *node = list->_this->head;
int i = 0;
For (i;i <= list->_this->size;i++) {
if (node!= (node*) 0) {
printf ("[%p] = {%s}\n", &node->data, Node->data);
node = node->next;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.