C 实现简单list

/* list.h -  */
/* Copyright 2018-2028 zyyt, Inc. */
/*
modification history
--------------------

01a 19jan18 zj    added this head
*/
/*
DESCRIPTION
NULL.
*/
#ifndef __list_H__
#define __list_H__
#include "PDAtype.h"
#ifdef __cplusplus
extern "C" {
#endif
 /*
 typedef struct //structCard
 {
  int  CardNum; //short int  CardNum;
  short int  Count;
  // unsigned char BatAlarm;
  // unsigned char Emergency;
  union
  {
   struct
   {
    unsigned int BatAlarm : 1;     //LSB
    unsigned int Emergency : 1;
    unsigned int : 1;
    unsigned int : 1;    // CTCSSMode

    unsigned int : 1; //MSB
   } Bits;
   unsigned int StausValue;
  } CardStaus;
  //char VehicleNum[10];
 }TCard;
 */
#define MAX_LIST_LEN 300
 typedef struct //structCard
 {
  int length; //
  TCard  arrayCard[MAX_LIST_LEN];
 }TCardList;
 TCard listNodeAt(int index);
 TCard listGetMin();
 TCard listGetMax();
#ifdef __cplusplus
}
#endif
#endif







/* list.c -  */
/* Copyright 2018-2028 zyyt, Inc. */
/*
modification history
--------------------

01a 19jan18 zj    added this head
*/
/*
DESCRIPTION
NULL.
*/
#include "PDAtype.h"
#include "list.h"
#include <stdio.h>

TCardList cardList;

void listInit()
{
 int i;
 cardList.length = 0;
 for (i = 0; i < MAX_LIST_LEN; i++) {
  cardList.arrayCard[i].CardNum = 0;
  cardList.arrayCard[i].Count = 0;
  cardList.arrayCard[i].CardStaus.StausValue = 0;
 
  
 }
}

void listAdd(TCard card)
{
 if(cardList.length>=MAX_LIST_LEN){
  cardList.length=0;
 }
 
 cardList.arrayCard[cardList.length++] = card;
}
int listIndexOf(TCard card)
{
 int i;
 for (i = 0; i < cardList.length; i++) {
  if (cardList.arrayCard[i].CardNum == card.CardNum)
  {
   return i;
  }
 }
 return -1;
}
void listInsert(int index, TCard card)
{
 int i;
 for (i = cardList.length - 1; i > index - 1; i--) {
  cardList.arrayCard[i + 1] = cardList.arrayCard[i];
 }
 cardList.arrayCard[index] = card;
 cardList.length++;
}
void listUpdate(int index, TCard card)
{
 if (index < cardList.length) {
  cardList.arrayCard[index] = card;
 }
 
}
TCard listNodeAt(int index)
{
 
 return cardList.arrayCard[index];
}
TCard listGetMax()
{
 int i;
 TCard cardMax;
 cardMax = cardList.arrayCard[0];
 for (i = 1; i < cardList.length; i++) {
  if (cardList.arrayCard[i].CardNum > cardMax.CardNum)
  {
   cardMax = cardList.arrayCard[i];
  }
 }
 return cardMax;
}
TCard listGetMin()
{
 int i;
 TCard cardMin;
 cardMin = cardList.arrayCard[0];
 for (i = 1; i < cardList.length; i++) {
  if (cardList.arrayCard[i].CardNum < cardMin.CardNum)
  {
   cardMin = cardList.arrayCard[i];
  }
 }
 return cardMin;
 
}
int listLength()
{
 return cardList.length;
}

void listRemove(TCard card)
{
 int indexOfRm;
 int i;
 indexOfRm = listIndexOf(card);
 if (indexOfRm > 0) {
  for (i = indexOfRm + 1; i < cardList.length; i++) {
   cardList.arrayCard[i - 1] = cardList.arrayCard[i];
  }
  cardList.length--;
 }
}
void listRemoveAll()
{
 listInit();
}

void listPrint()
{
 int i;
 printf("cardList.length %d\r\n", cardList.length);
 for (i = 0; i < cardList.length; i++)
 {
  printf("[%d]%d ", i, cardList.arrayCard[i].CardNum);
 }
 printf("\r\n");
}
void listMake()
{
 TCard card;
 int i;
 card.CardNum = 3;
 listRemoveAll();
 for (i = 0; i < 30; i++)
 {
  card.CardNum = 1+i;
  card.Count = 5;
  card.CardStaus.Bits.BatAlarm = 1;
  listAdd(card);
 }
 listPrint();
}


void listTest()
{
 TCard card1, card2, card3;
 TCard card4, card5, card6;
 card1.CardNum = 3;
 card2.CardNum = 7;
 card3.CardNum = 8;
 card4.CardNum = 71;
 card5.CardNum = 82;
 card6.CardNum = 85;

 listAdd(card1);
 listPrint();
 listAdd(card2);
 listAdd(card3);
 listPrint();
 listRemove(card2);
 listPrint();
 listInsert(1, card4);
 listPrint();
 listAdd(card5);
 listAdd(card6);
 listPrint();
 
 printf("listIndexOf %d %d\r\n",card5.CardNum,listIndexOf(card5));
 
 listRemoveAll();
 listPrint();
 
 listAdd(card6);
 listPrint();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值