自制linked list,源代码

不出意外 ( 指沉迷开船 ) 的话,应该会出教程,以及分享一下心得什么的

索引博客已经写出,详情请见→索引.

node.h: 定义了2个东西,一个是节点 (node) ,一个是头尾指针 (lst)

#include "node.h"
#include <stdio.h>
#include <stdlib.h>
#ifndef _NODE_H_
#define _NODE_H_

typedef struct _node
{
  int value;
  struct _node *point;
}Node;

typedef struct _list
{
  Node* head;
  Node* tail;
}List;

#endif

main:

#include "node.h"
#include <stdio.h>
#include <stdlib.h>

void lstCrt(List *lstp);
void lstPrt(const List *lstp);
void lstAdd(List *lstp,const int input);
void lstSrh(const List *lstp,const int input);
void lstDel(List *lstp,const int input);
void lstClr(List *lstp);

int main(void)
{
  List lst;
  int input = 0;
  lstCrt(&lst);
  int num = 33;//debug
  lstAdd(&lst,num);

  lstPrt(&lst);
  // printf("%p:%d",lst.head,lst.head->value);//debug
  lstSrh(&lst,num);
  lstDel(&lst,num);
  lstPrt(&lst);
  lstClr(&lst);

  return 0;
}

lstCrt: 用以创建一个 linked list(也可以直接后面的 lstAdd 一个个添加)

#include "node.h"
#include <stdio.h>
#include <stdlib.h>

void lstCrt(List *lstp)
{
  //[1]begin
  //preparation:
  Node *p = NULL;
  lstp->head = (Node*) malloc(sizeof(Node));//allocate new space for the head.(lstp->head is just the point of head)
  lstp->head->value = 0;//initialization
  lstp->head->point = NULL;//initialization
  lstp->tail = lstp->head;//when there is no node, the head of the linked list is right the tail.
  printf("A new linkedlist has been created. Please input something(-1 to exit): ");
  //[1]end

  //[2]begin
  //start writing
  while(1) {
    int input = 0;
    scanf("%d",&input);//get a input
    if( input == -1) {//check the input
      break;
    }
    p = (Node*) malloc(sizeof(Node));//allocate new space for the next node
    lstp->tail->point = p;//link the pre-node's point to the new(next) node
    lstp->tail = p;//link the tail to the new node
    p->value = input;//write the input into the value of the new(next) node
    p->point = NULL;//wait for written into
    //[2]end
  }

  return;
}

lstAdd: 添加一个 node 到 linked list 上

#include "node.h"
#include <stdio.h>
#include <stdlib.h>

void lstAdd(List *lstp,const int input)
{
  Node *p = (Node*) malloc(sizeof(Node));
  if(lstp->head) {
    lstp->tail->point = p;//link the pre-node to this node
  }else {
    lstp->head->point = p;//link the head to this node
  }
  lstp->tail = p;//link the tail to this node
  p->value = input;//write input to the value of this node
  p->point = NULL;
}

lstSrh: 搜索 linked list 中的某一 element 或者说 node 的 值 (value)

#include "node.h"
#include <stdio.h>
#include <stdlib.h>

void lstSrh(const List *lstp,const int input)
{
  int isFound = 0;
  for(Node*p=lstp->head->point;p;p=p->point) {//traverse the whole linked list
    if(p->value == input)
    {
      isFound = 1;
      printf("Found!\n");
      break;
    }
  }
  if(!isFound) {
    printf("Sorry! %d not found...\n",input);
  }

  return;
}

lstDel: 删除某一个 node

#include "node.h"
#include <stdio.h>
#include <stdlib.h>

void lstDel(List *lstp,const int input)
{
  int isFound = 0;//to mark whether the input is found
  Node *temp = NULL;//to point to the to-be-del node temporarily, so as to free it
  Node *p = lstp->head;
  for(p=lstp->head;p->point;p=p->point) {//to check the folloings
    if(p->point->value == input) {
      ++isFound;
      temp = p->point;//to point to the to-be-del node temporarily, so as to free it
      p->point = p->point->point;//link the this node to the taget(the node after the next)
      //[] [p] [to be freed] [taget]
      //    ↓                   ↑
      //    →→→→→→→→→→→→→→→→→→→→↑
      free(temp);//free it!
      printf("Success! The first one of %d has been delete!\n",input);
      break;
    }
  }
  if(!isFound) {
    printf("Delete failed.%d not found.\n",input);
  }

  return;
}

lstClr: 最后清除 linked list ,释放内存

#include "node.h"
#include <stdio.h>
#include <stdlib.h>

void lstClr(List *lstp)
{
  Node*temp = lstp->head;
  for(Node*p=temp->point;p;p=p->point) {//traverse all the list
    free(temp);
    temp = p;
  }
  free(temp);//free the last node

  return;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的DMA驱动代码,用于从一个链接列表中传输数据。该代码假定链接列表中的每个元素都包含一个指向数据缓冲区的指针和一个指向下一个元素的指针。该代码使用DMA控制器来传输数据,并在传输完成后调用一个回调函数。 ``` #include <linux/dmaengine.h> #include <linux/list.h> struct dma_data { void *buf; struct list_head list; }; static struct dma_chan *dma_chan; static void dma_callback(void *data) { // DMA transfer is complete, do any necessary cleanup } int dma_transfer(struct list_head *list) { struct dma_data *data; dma_cookie_t cookie; int ret; // Allocate a DMA descriptor struct dma_async_tx_descriptor *desc = dma_chan->device->device_prep_dma_memcpy( dma_chan, NULL, NULL, 0, DMA_PREP_INTERRUPT); // Iterate over the list and add each buffer to the DMA descriptor list_for_each_entry(data, list, list) { dmaengine_prep_slave_sg(desc, &data->buf, sizeof(void *), DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT); desc = dmaengine_desc_chain(desc, desc); } // Submit the DMA transfer and wait for it to complete cookie = dmaengine_submit(desc); ret = dma_submit_error(cookie); if (ret) return ret; ret = dma_async_is_tx_complete(cookie, dma_chan, DMA_TIMEOUT); if (!ret) return -ETIMEDOUT; // Call the callback function dma_callback(NULL); return 0; } ``` 在该代码中,我们首先定义了一个`dma_data`结构体来表示我们链接列表中的数据。然后我们定义了一个指向DMA通道的指针`dma_chan`,以及一个回调函数`dma_callback`,在DMA传输完成后将调用该函数。 `dma_transfer`函数是主要的DMA传输函数。它接受一个指向链接列表的指针,并使用DMA控制器将数据从每个链接列表元素的缓冲区传输到设备。我们首先使用`dma_chan->device->device_prep_dma_memcpy`函数分配一个DMA描述符,然后使用`dmaengine_prep_slave_sg`函数将每个缓冲区添加到描述符中。 一旦我们完成了描述符的构建,我们使用`dmaengine_submit`函数将传输提交到DMA控制器,并使用`dma_async_is_tx_complete`函数等待传输完成。一旦传输完成,我们调用回调函数`dma_callback`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值