Posted on 2013-01-15 11:10 code2012 阅读(1) 评论(0) 编辑 收藏
#ifndef _MYVECTOR_H_
#define  _MYVECTOR_H_
  
  
  
  
typedef struct _InstrExNode
{
    char OpcodeName[16];
  
    int DstNum; //目标寄存器的个数
    char DesReg[8][16] ; //存放 目标寄存器的的名字
  
    int ScrNum; //源寄存器的个数
  
    char ScrReg[8][16] ; //存放 源寄存器的的名字
    struct _InstrExNode *next;
}InstrExNode,*PInstrExNode;
  
  
//链表头
typedef struct _LinkedList{
  
    int size;
    bool bIsempty;
    InstrExNode head;
}LinkedList,*PLinkedList;
  
  
PLinkedList LinkedList_new();
void LinkedList_PushBack(LinkedList *pLinkedList, InstrExNode *pInstrExNode);
void LinkedList_Free(LinkedList *pLinkedList);
  
  
#endif
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
  
#include "MyVector.h"
  
  
//新建链表 
PLinkedList LinkedList_new()
{
  
    LinkedList *linkedlist = NULL;
    linkedlist=(LinkedList *)malloc(sizeof(LinkedList));
    if(linkedlist == NULL){
        return NULL;
    }
  
    linkedlist->size = 0;
    linkedlist->bIsempty = true;  
  
    memset(&linkedlist->head,0,sizeof(InstrExNode));
    return linkedlist;       
}
  
  
  
  
  
  
//使用后插法创建单链表(头结点不为空,如果为空则需要返回值)
void LinkedList_PushBack(LinkedList *pLinkedList, InstrExNode *pInstrExNode)
{
  
    //如果是第一个元素
    if (pLinkedList->bIsempty == true){
        pLinkedList->head.next = pInstrExNode;
        pLinkedList->bIsempty = false ;
        pLinkedList->size++;
    }
    else
    {   
        InstrExNode *pCurNode= pLinkedList->head.next; //第一个节点
        InstrExNode *pLastNode = NULL ;
        while(pCurNode != NULL){
            pLastNode = pCurNode;
            pCurNode = pCurNode->next;
        }
        pLastNode->next = pInstrExNode;
        pLinkedList->size++;
    }
}
  
  
//
void LinkedList_Free(LinkedList *pLinkedList)
{
    //释放所有的节点
    if (pLinkedList->bIsempty == false)
    {
        InstrExNode *pNodeTmp= NULL ; 
        InstrExNode *pCurNode = pLinkedList->head.next; //第一个节点
  
        while(pCurNode != NULL){
            pNodeTmp = pCurNode->next;
  
            free(pCurNode);
            pCurNode = NULL ;
            pLinkedList->size--;
  
            pCurNode = pNodeTmp;
        }
  
        free(pLinkedList);
        pLinkedList = NULL ;
  
    }
}

 

posted on 2013-07-30 14:14  HPU---张振强  阅读(124)  评论(1编辑  收藏  举报