예제 #1
0
파일: control.c 프로젝트: mingpen/OpenNT
//---------------------------------------------------------------------------
// ELSE
//
// This routine recognizes and compiles the ELSE statement.  The code that is
// generated should simply look like:
//
//              JMP     CSF_ENDBLOCK
//          CSF_ELSEBLOCK:
//
// RETURNS:     Root node of ELSE compilation tree
//---------------------------------------------------------------------------
INT ELSE (INT op)
{
    INT     curCS, n;

    // Ensure that the current CS is an IF...
    //-----------------------------------------------------------------------
    curCS = AssertCSType (CS_IF);
    if (curCS == -1)
        return (-1);

    // The IF control struct was found.  First thing to do is check to see if
    // the ENDBLOCK label is the same as the ELSEBLOCK label.  This indicates
    // that no ELSEx block has been encountered.  If so, we set a new
    // ENDBLOCK label for the next jump.
    //-----------------------------------------------------------------------
    ADVANCE;
    if (GetCSField(curCS, CSF_ENDBLOCK) == GetCSField(curCS, CSF_ELSEBLOCK))
        SetCSField (curCS, CSF_ENDBLOCK, TempLabel());

    // Generate the code trees that jump to the ENDIF (ENDBLOCK), and set the
    // ELSEBLOCK label to -1
    //-----------------------------------------------------------------------
    n = Siblingize (MakeNode (opJMP, GetCSField (curCS, CSF_ENDBLOCK)),
                    MakeNode (opFIXUP, GetCSField (curCS, CSF_ELSEBLOCK)),
                    -1);
    SetCSField (curCS, CSF_ELSEBLOCK, -1);

    return (n);
    (op);
}
예제 #2
0
파일: Test.c 프로젝트: wugsh/wgs
main()
{
	DList *plist = NULL;
	PNode p = NULL;
	
	plist = InitList();
	p = InsFirst(plist,MakeNode(1));
	InsBefore(plist,p,MakeNode(2));
	InsAfter(plist,p,MakeNode(3));

	printf("p前驱位置的值为%d\n",GetItem(GetPrevious(p)));
	printf("p位置的值为%d\n",GetItem(p));
	printf("p后继位置的值为%d\n",GetItem(GetNext(p)));
	
	
	printf("遍历输出各节点数据项:\n");
	ListTraverse(plist,print);
	printf("除了头节点该链表共有%d个节点\n",GetSize(plist));
	FreeNode(DelFirst(plist));
	printf("删除第一个节点后重新遍历输出为:\n");
	ListTraverse(plist,print);
	printf("除了头节点该链表共有%d个节点\n",GetSize(plist));
	DestroyList(plist);
	printf("链表已被销毁\n");
}
예제 #3
0
파일: control.c 프로젝트: mingpen/OpenNT
//---------------------------------------------------------------------------
// ENDIF
//
// This routine recognizes and compiles the ENDIF CS statement
//
// RETURNS:     Nothing
//---------------------------------------------------------------------------
INT ENDIF (INT op)
{
    INT     curCS, n, x1, x2;

    // Ensure that the current CS is an IF...
    //-----------------------------------------------------------------------
    curCS = AssertCSType (CS_IF);
    if (curCS == -1)
        return (-1);

    // The IF control struct was found -- now terminate it
    //-----------------------------------------------------------------------
    ADVANCE;
    x1 = GetCSField (curCS, CSF_ENDBLOCK);
    x2 = GetCSField (curCS, CSF_ELSEBLOCK);
    n = MakeNode (opFIXUP, x1);
    if ((x2 != -1) && (x2 != x1))
        n = Siblingize (n, MakeNode (opFIXUP, x2), -1);

    // Close the control structure and return the tree to fixup the labels
    //-----------------------------------------------------------------------
    CloseCS();
    return (n);
    (op);
}
예제 #4
0
List
MultiplyPolynomial(List PA, List PB) {
  // Both lists of PA & PB are assumed to be in ascending order.

#if _DEBUG
  int MoveCount = 0;
#endif

  List PResult = MakeNode();

  Position P = First(PA), Q;
  while (P != NULL) {
    Q = First(PB);
    Position R = First(PResult);

    while (Q != NULL) {
      int Exponent = P->Exponent + Q->Exponent;
      int Coefficient = P->Coefficient * Q->Coefficient;

      if (IsLast(PResult)) {
        R = PResult->Next = MakeNode();
      } else {
        while (!IsLast(R) && R->Next->Exponent <= Exponent) {
#if _DEBUG
          // If PA has less elements than PB, then we can move less here
          ++MoveCount;
#endif
          R = R->Next;
        }

        if (R->Exponent < Exponent) {
          Position TmpCell = MakeNode();
          TmpCell->Next = R->Next;
          R = R->Next = TmpCell;
#if _DEBUG
        } else if (R->Exponent > Exponent) {
          std::cout << "The input polynomials may be not in ascending order!" << std::endl;
#endif
        }
      }

      R->Exponent = Exponent;
      R->Coefficient += Coefficient;

      Q = Q->Next;
    }

    P = P->Next;
  }

#if _DEBUG
  std::cout << "MoveCount: " << MoveCount << std::endl;
#endif

  return PResult;
}
예제 #5
0
List
MakePolynomialList(const std::vector<std::vector<int> > ArgList) {
  List L = MakeNode();
  Position P = L;

  for (const auto ExponentAndCoefficient: ArgList) {
    P = P->Next = MakeNode();
    P->Exponent = ExponentAndCoefficient[0];
    P->Coefficient = ExponentAndCoefficient[1];
  }

  return L;
}
예제 #6
0
static Node* udinst_uminst ( Node* udinst, Transformer *tprocs)
 {
  
    Node* inst = UdinstInst(udinst);
    Node* left = UdinstLeft(udinst);
    Node* right = UdinstRight(udinst);
    Node* str;
    Node* elems = MakeVector(ELLA_UNIT, 2, tprocs);
    *IndexVector(elems, 1) = left;
    *IndexVector(elems, 2) = right;
    str = MakeNode(ELLA_USTR, Node1(elems), tprocs);
    return MakeNode(ELLA_UMINST, Node2(inst, str), tprocs);
 }
예제 #7
0
PdfContentsGraph::PdfContentsGraph()
    : m_graph()
{
    // Init the root node, leaving an otherwise empty graph.
    Vertex v = add_vertex(m_graph);
    m_graph[v] = MakeNode(KW_RootNode,KW_RootNode);
}
예제 #8
0
SkipList SkipList_Create (Deleter d, Comparator c)
/* Creates a new skip list having Deleter d and Comparator c.
   Returns a pointer to the created structure.
*/
{
	SkipList sl;
	int i;

	sl = (struct SkipList_struct *)
		malloc (sizeof(struct SkipList_struct));
	sl->Member_Delete  = d;
	sl->Member_Compare = c;
	sl->header = MakeNode (MAX_LEVEL, NULL, NULL);
	sl->finger = (SkipList_element *) malloc (MAX_LEVEL *
					       sizeof(SkipList_element));
	sl->distance = (int *) malloc (MAX_LEVEL * sizeof(int));
	sl->level  = 1;
	sl->length = 0;
	for (i = 0; i < MAX_LEVEL; i++) {
		sl->header->forward[i] = NULL;
		sl->header->diff[i] = 1;
		sl->finger[i] = sl->header;
		sl->distance[i] = 1;
	};

	return (sl);
};
예제 #9
0
파일: tree.c 프로젝트: puliuyinyi/learngit
int AddItem(const Item * pi, Tree * ptree)
{
	Node * new_node;

	if(TreeIsFull(ptree))
	{
		fprintf(stderr,"Tree is full.\n");
		return FALSE;
	}

	if(SeekItem(pi,ptree).child != NULL)
	{
		fprintf(stderr,"Attempt to add duplicate item.\n");
		return FALSE;
	}

	new_node = MakeNode(pi);
	if(new_node == NULL)
	{
		fprintf(stderr,"couldn't create node.\n");
		return FALSE;
	}

	ptree->size++;
	if(ptree->root == NULL)
		ptree->root = new_node;
	else
		AddNode(new_node,ptree->root);
	
	return TRUE; 
}
예제 #10
0
std::shared_ptr<Node> SkexmlParser::Parse(std::string filename) {
    std::string input = "";
    std::ifstream file;
    bool valid = false;
    file.open(filename);
    while(!valid and !file.eof()) {
        std::getline(file, input);
        Utils::RemoveEndline(input);
        std::string test = input;
        if (input.length() < 0)
            continue;
        else if (input.substr(0, 1) != "[")
            continue;
        else if (input.find("]") == std::string::npos)
            continue;
        else if (input.length() - 1 > input.find("]"))
            continue;
        valid = true;
    }
    if(!valid)
        return std::make_shared<Node>("");
    std::string node_name = input.substr(1, input.length() - 2);
    std::shared_ptr<Node> node = MakeNode(node_name, file);
    return node;
}
예제 #11
0
BstNode<KEY, VALUE>* AvlTree<KEY, VALUE>::InsertNode
(const KEY& insertkey, const DataStorePtr<VALUE>& insertvalue, BstNode<KEY, VALUE>* tree, int& confirm)
{
  if (tree == NULL)
  {
    confirm = 1;
    return MakeNode(insertkey, insertvalue);
  }

  if (insertkey < tree->key)
    tree->left = InsertNode(insertkey, insertvalue, tree->left, confirm);
  else if (insertkey > tree->key)
    tree->right = InsertNode(insertkey, insertvalue, tree->right, confirm);
  else
  {
    confirm = 0;
    tree->value = insertvalue;
    
    if (tree->value.Pointee() == NULL)
      cerr <<ERRMSG_NOMEMBST;
  }

  if (confirm)
    return Rebalance(tree);

  return tree;
}
예제 #12
0
BstNode<KEY, VALUE>* AvlTree<KEY, VALUE>::InsertNode
(const KEY& insertkey, VALUE* insertvalue, int StoreAttrib_, BstNode<KEY, VALUE>* tree, int& confirm)
{
  if (tree == NULL)
  {
    confirm = 1;
    return MakeNode(insertkey, insertvalue, StoreAttrib_);
  }

  if (insertkey < tree->key)
    tree->left = InsertNode(insertkey, insertvalue, StoreAttrib_, tree->left, confirm);
  else if (insertkey > tree->key)
    tree->right = InsertNode(insertkey, insertvalue, StoreAttrib_, tree->right, confirm);
  else
  {
    confirm = 0;  
    StoreAttrib_ |= DataStorageAttributes::ACTIVE;
    tree->value.AssignPtr(insertvalue, StoreAttrib_);
    
    if (tree->value.Pointee() == NULL)
      cerr <<ERRMSG_NOMEMBST;
  }

  if (confirm)
    return Rebalance(tree);

  return tree;
}
예제 #13
0
BOOL EnQueue(Queue* pQueue, int nData)
{
    QueueNode* pNode = NULL;
    
    pNode = MakeNode(nData);
    if (pNode == NULL)
    {
        return FALSE;
    }

    if (pQueue->nLength == 0)
    {
        pQueue->pFront = pNode;
        pQueue->pRear = pQueue->pFront;
        pQueue->nLength = 1;
    }
    else
    {
        pQueue->pRear->pNext = pNode;
        pQueue->pRear = pNode;
        ++(pQueue->nLength);
    }

    return TRUE;
}
예제 #14
0
파일: tree.c 프로젝트: 18616378431/myCode
//添加元素,即节点
BOOL AddItem(const Item * pi,Tree * ptree)
{
	Node * new_node;//新节点
	
	if(TreeIsFull(ptree))//判断树是否已满
	{
		fprintf(stderr,"Tree is full.\n");
		return FALSE;
	}
	if(SeekItem(pi,ptree).child != NULL)//在树中查找目标项目
	{
		fprintf(stderr,"Attempted to add duplicate item\n");
		return FALSE;
	}
	new_node = MakeNode(pi);//创建新节点,成功返回新节点,否则返回NULL
	if(new_node == NULL)//创建失败,返回false
	{
		fprintf(stderr,"Couldn't create node!\n");
		return FALSE;
	}
	//节点创建成功,向树中添加节点
	ptree->size++;//树的大小加1
	if(ptree->root == NULL)//如果树为空
	{
		ptree->root = new_node;
	}
	else
	{
		AddNode(new_node,ptree->root);//找到新节点应该添加到的位置
	}
	return TRUE;
}
예제 #15
0
파일: list.cpp 프로젝트: keitee/kb
// add only at the end. see there are approaches to find end; one is to use pnext and the other is
// to use count. 
bool AddList( List* list, EntryType entry )
{
  Node* pnode, *pend;

  if( (pnode = MakeNode(entry)) == NULL )
  {
    std::cout << "add: mem is full" << std::endl;
    return false;
  }

  if( ListEmpty( list ) )
  {
    list->header = pnode;
  }
  else
  {
#ifdef USE_PNEXT
    // search the end using pnext
    for( pend = list->header; pend->pnext; pend = pend->pnext )
      ;
#else
    // search the end using count
    pend = list->header;
    for( int current = 1; current < list->count; current++) // note that less than
      pend = pend->pnext;
#endif

    pend->pnext = pnode;
  }

  list->count++;

  return true;
}
예제 #16
0
int ConstExpression::P()
{
    Operator *op;
    if(op=GetOperator(Next()->type_id(),1)) // unary
    {
        Consume();
        int q = op->prec;
        int t = Exp(q);
        return MakeNode(op,t,0);
    }
    else if(Next()->type_id()==TKN_L_PAREN)
    {
        Consume();
        int t = Exp(0);
        Expect(TKN_R_PAREN);
        return t;
    }
    else if(Next()->type_id()==TKN_NUMBER)
    {
        int t = atoi(Next()->get_text().c_str());
        Consume();
        return t;
    }
    else
        exit(0);

}
예제 #17
0
 void InsertBook(IdxListType *idxlist,int i,int bno)
 { /* 在索引表idxlist的第i项中插入书号为bno的索引 */
   Link p;
   if(!MakeNode(&p,bno)) /* 分配失败 bo2-6.c */
     exit(OVERFLOW);
   p->next=NULL;
   Append(&(*idxlist).item[i].bnolist,p); /* 插入新的书号索引 bo2-6.c */
 }
예제 #18
0
main() {
	BiTNode * n1 = MakeNode(10, NULL, NULL);
	BiTNode * n2 = MakeNode(20, NULL, NULL);
	BiTNode * n3 = MakeNode(30, n1, n2);
	BiTNode * n4 = MakeNode(40, NULL, NULL);
	BiTNode * n5 = MakeNode(50, NULL, NULL);
	BiTNode * n6 = MakeNode(60, n4, n5);
	BiTNode * n7 = MakeNode(70, NULL, NULL);

	BiTree tree = InitBiTree(n7);
	SetLChild(tree, n3);
	SetRChild(tree, n6);

	printf("树的深度为:%d \n", GetDepth(tree));
	printTree(tree, GetDepth(tree));

	printf("\n先序遍历如下:");
	PreOrderTraverse(tree, print);

	printf("\n中序遍历如下:");
	InOrderTraverse(tree, print);

	printf("\n后序遍历如下:");
	PostOrderTraverse(tree, print);

	DeleteChild(tree, 1);
	printf("\n后序遍历如下:");
	PostOrderTraverse(tree, print);

	DestroyBiTree(tree);
	if (IsEmpty(tree))
		printf("\n二叉树为空,销毁完毕\n");
}
예제 #19
0
ReadingList::operator ValueNode::Container() const
{
	ValueNode::Container con;

	for(const auto& bm : GetList())
		con.insert(MakeNode("bm" + to_string(con.size()),
			ValueNode::Container(bm)));
	return con;
}
int main() {
  struct node *only_one = MakeNode(1);
  MergeSort(&only_one);
  
  struct node *two = MakeNode(2);
  two->next = MakeNode(1);
  printf("two: "); PrintList(two);
  MergeSort(&two);
  printf("two: "); PrintList(two);
  
  int spec[8] = {19, 3, 99, 1, 22, 44, 12, 2};
  struct node *head = ListFromArray(spec, 8);
  
  printf("head: "); PrintList(head);
  MergeSort(&head);
  printf("head: "); PrintList(head);
  
  return 0;
}
예제 #21
0
		void TokenList::AddBefore(TokenListNode *node, Token *value)
		{
			if (node->list == this)
			{
				node->prev = MakeNode(value);
				node->prev->next = node;
				m_count++;
				if (head == node)
					head = node->prev;
			}
		}
예제 #22
0
RayEngine* RayBVHEngineBuilder::Build(shared_ptr<Surface> surface, const Intervalf& time, int timeSamples)
{
    if (shared_ptr<MeshShape> shape = dynamic_pointer_cast<MeshShape>(surface->ShapeRef()))
    {
        return MakeNode(shape, surface->MaterialRef(), surface->XformRef());
    }
    else if (shared_ptr<SphereShape> shape = dynamic_pointer_cast<SphereShape>(surface->ShapeRef()))
    {
        return MakeNode(shape, surface->MaterialRef(), surface->XformRef());
    }
    else if (shared_ptr<CurveShape> shape = dynamic_pointer_cast<CurveShape>(surface->ShapeRef()))
    {
        return MakeNode(shape, surface->MaterialRef(), surface->XformRef());
    }
    else
    {
        cerr << "shape type not supported" << endl;
        return NULL;
    }
}
예제 #23
0
파일: listhw.c 프로젝트: ourzizz/learnc
Status CreateListByArray(ElemType *array,int ArraySize,LinkList *L,Status (*InsertFun)(LinkList *,Link,Link))
{
    Link tmp = NULL;
    int i = 0;
    for (i = 0; i < ArraySize; ++i) {
        MakeNode(&tmp,array[i]);
        /*InsFirst(L->head,tmp);*/
        InsertFun(L,L->head,tmp);
    }
    return OK;
}
예제 #24
0
		void TokenList::AddAfter(TokenListNode *node, Token *value)
		{
			if (node->list == this)
			{
				node->next = MakeNode(value);
				node->next->prev = node;
				m_count++;
				if (tail == node)
					tail = node->next;
			}
		}
예제 #25
0
파일: control.c 프로젝트: mingpen/OpenNT
//---------------------------------------------------------------------------
// ELSEIF
//
// This routine recognizes and compiles the ELSEIF statement.  The pcode
// generated is as follows:
//
//  STATEMENT:      ELSEIF <intexp> THEN
//
//  PCODE:              JMP     CSF_ENDBLOCK
//                  CSF_ELSEBLOCK:
//                      <intexp>
//                      POPA
//                      JE      0, CSF_ELSEBLOCK        ; (new ELSE block)
//
// RETURNS:     Root node of ELSEIF compilation tree
//---------------------------------------------------------------------------
INT ELSEIF (INT op)
{
    INT     curCS, n, n2, v;

    // Ensure that the current CS is an IF...
    //-----------------------------------------------------------------------
    curCS = AssertCSType (CS_IF);
    if (curCS == -1)
        return (-1);

    // The IF control struct was found.  First thing to do is check to see if
    // the ENDBLOCK label is the same as the ELSEBLOCK label.  This indicates
    // that no ELSEx block has been encountered.  If so, we set a new
    // ENDBLOCK label for the next jump.
    //-----------------------------------------------------------------------
    ADVANCE;
    v = GetCSField (curCS, CSF_ELSEBLOCK);
    if (GetCSField(curCS, CSF_ENDBLOCK) == v)
        SetCSField (curCS, CSF_ENDBLOCK, TempLabel());

    // Generate the code trees that jump to the ENDIF (ENDBLOCK), and make a
    // new ELSEBLOCK label
    //-----------------------------------------------------------------------
    n = Siblingize (MakeNode (opJMP, GetCSField (curCS, CSF_ENDBLOCK)),
                    MakeNode (opFIXUP, v), -1);
    SetCSField (curCS, CSF_ELSEBLOCK, v = TempLabel());

    // Generate another IF tree
    //-----------------------------------------------------------------------
    n2 = MakeParentNode (IntExpression(), opPOPA);
    n2 = MakeParentNode (n2, opJE, 0L, v);
    n = Adopt (MakeNode (opNOP), n, n2, -1);

    // Insist on the THEN keyword, and we're done!
    //-----------------------------------------------------------------------
    ReadKT (ST_THEN, PRS_SYNTAX);
    return (n);
    (op);
}
예제 #26
0
Node* CreateList(const int* pData, int nLength)
{
	Node* pHead = NULL;
	Node* pTail = NULL;
	int i = 0;

	for (; i < nLength; ++i)
	{
		if (pHead == NULL)
		{
			pHead = MakeNode(pData[i]);
			pTail = pHead;
		}
		else
		{
			pTail->pNext = MakeNode(pData[i]);
			pTail = pTail->pNext;
		}
	}

	return pHead;
}
예제 #27
0
/**
 * 算法2.20,在带头结点的单链线性表L的第i个元素之前插入元素e
 */
Status ListInsert(LinkList &L, int i, ElemType e)
{
	Link h, s;
	if (!LocatePos(L, i-1, h))
		return ERROR;
	if (!MakeNode(s, e))
		return ERROR;
	if (h == L.tail) 
		L.tail = s;   // 修改尾指针
	InsFirst(h, s);       //对于从第不个结点开始的链表,第i-1个结点是它的头结点
	L.len++;
	return OK;
}
int main() {
  struct node *a = NULL;
  struct node *b = MakeNode(3);
  b->next = MakeNode(4);
  
  printf("a: "); PrintList(a);
  printf("b: "); PrintList(b);
  Append(&a, &b);
  printf("a: "); PrintList(a);
  printf("b: "); PrintList(b);
  
  struct node *x = BuildOneTwoThree();
  struct node *y = MakeNode(4);
  y->next = MakeNode(5);
  
  printf("\nx: "); PrintList(x);
  printf("y: "); PrintList(y);
  Append(&x, &y);
  printf("x: "); PrintList(x);
  printf("y: "); PrintList(y);
  
  return 0;
}
예제 #29
0
		void TokenList::AddFirst(Token *value)
		{
			TokenListNode *node = MakeNode(value);
			m_count++;

			if (head == nullptr)
				head = tail = node;
			else
			{
				head->prev = node;
				node->next = head;
				head = node;
			}
		}
예제 #30
0
		void TokenList::AddLast(Token *value)
		{
			TokenListNode *node = MakeNode(value);
			m_count++;

			if (head == nullptr)
				head = tail = node;
			else
			{
				tail->next = node;
				node->prev = tail;
				tail = node;
			}
		}