Ejemplo n.º 1
0
void SList::insert(int newData) {
    SLNode* newNode = new SLNode(newData);
    if (head == NULL)
        insertHead(newData);
    else if (head -> getNextNode() == NULL) {
        if ((*head).getContents() > newData)
            insertHead(newData);
        else
            insertTail(newData);
    }
    else {
        if (newData <= head -> getContents())
            insertHead(newData);
        else {
            SLNode* trailer = NULL;
            SLNode* spot = head;
            while (spot -> getNextNode() != NULL && newData > spot -> getContents()) {
                trailer = spot;
                spot = spot -> getNextNode();
            }
            if (newData > spot -> getContents() && spot -> getNextNode() == NULL)
                insertTail(newData);
            else {
                newNode -> setNextNode(spot);
                trailer -> setNextNode(newNode);
                size++;
            }
        }
    }
}
Ejemplo n.º 2
0
int main(){
	
	Element* head = (Element*)malloc(sizeof(Element));
	memset(head, 0, sizeof(Element));
	head->data = 0;
	head->next = NULL;
	insertHead(&head, 1);
	insertHead(&head, 2);
	insertHead(&head, 3);
	
	// Test to insert in front of head
	insertAfter(&head, NULL, 4);
	insertAfter(&head, head, 5);
	
	printHeadData(&head);
	printLinkedList(&head);
	// deleteHead(&head);
	// deleteHead(&head);
	// deleteHead(&head);
	// deleteHead(&head);
	
	Element* select = findMToLastElement(&head, 2);
	printf("lastMElem: %d\n", select->data);
	
	deleteHead(&head);
	deleteList(&head);
	printHeadData(&head);
	printLinkedList(&head);
	
	
	return 0;
}
Ejemplo n.º 3
0
uintptr_t splitBlock(uintptr_t **cursor, size_t req_size, size_t allocate_size, size_t total_size){
	uintptr_t *header_address = *cursor;
	uintptr_t *footer_address = header_address + (allocate_size / 8) + 1;
	
	uintptr_t *freeHeader_address = footer_address + 1;
	uintptr_t *freeFooter_address = header_address + (total_size / 8) + 1;

	uintptr_t freeSize = total_size - allocate_size - 16;

	uintptr_t allocated_info = createHeader(req_size, allocate_size + 16, MARK_USED);
	uintptr_t free_info = createHeader(0,freeSize + 16 , MARK_FREE);

	*header_address = allocated_info;
	*footer_address = allocated_info;

	*freeHeader_address = free_info;
	*freeFooter_address = free_info;

	*(freeHeader_address + 1) = *(header_address + 1);
	*(freeHeader_address + 2) = *(header_address + 2);

	/* split block resides between two free blocks */
	if((uintptr_t) *(freeHeader_address + 1) != NULL_POINTER 
	&& (uintptr_t) *(freeHeader_address + 2) != NULL_POINTER ){
		uintptr_t *next = (uintptr_t*) *(freeHeader_address + 1);
		uintptr_t *previous = (uintptr_t*) *(freeHeader_address + 2);

		*(next + 2) = (uintptr_t) previous;
		*(previous + 1) = (uintptr_t) next;

		insertHead(freeHeader_address);
	}

	/* split block has a block infront but not behind */
	else if((uintptr_t) *(freeHeader_address + 1) != NULL_POINTER 
		 && (uintptr_t) *(freeHeader_address + 2) == NULL_POINTER ){
		uintptr_t *next = (uintptr_t*) *(freeHeader_address + 1);
		
		*(next + 2) = (uintptr_t) freeHeader_address;

		head = (void *) freeHeader_address;
	}

	/* split block has a block behind but not infront */
	else if((uintptr_t) *(freeHeader_address + 1) == NULL_POINTER 
		 && (uintptr_t) *(freeHeader_address + 2) != NULL_POINTER ){
		uintptr_t *previous = (uintptr_t*) *(freeHeader_address + 2);
		
		*(previous + 1) = (uintptr_t) NULL_POINTER;
		insertHead(freeHeader_address);

	}else{
		/* must of split the only free block in the list*/
		head = (void *) freeHeader_address;
	}

	return 1;
}
Ejemplo n.º 4
0
bool QLCFixtureMode::loadXML(const QDomElement& root)
{
    if (root.tagName() != KXMLQLCFixtureMode)
    {
        qWarning() << Q_FUNC_INFO << "Mode tag not found";
        return false;
    }

    /* Mode name */
    QString str = root.attribute(KXMLQLCFixtureModeName);
    if (str.isEmpty() == true)
    {
        qWarning() << Q_FUNC_INFO << "Mode has no name";
        return false;
    }
    else
    {
        setName(str);
    }

    /* Subtags */
    QDomNode node = root.firstChild();
    while (node.isNull() == false)
    {
        QDomElement tag = node.toElement();
        if (tag.tagName() == KXMLQLCFixtureModeChannel)
        {
            /* Channel */
            Q_ASSERT(m_fixtureDef != NULL);
            str = tag.attribute(KXMLQLCFixtureModeChannelNumber);
            insertChannel(m_fixtureDef->channel(tag.text()),
                          str.toInt());
        }
        else if (tag.tagName() == KXMLQLCFixtureHead)
        {
            /* Head */
            QLCFixtureHead head;
            if (head.loadXML(tag) == true)
                insertHead(-1, head);
        }
        else if (tag.tagName() == KXMLQLCPhysical)
        {
            /* Physical */
            QLCPhysical physical;
            physical.loadXML(tag);
            setPhysical(physical);
        }
        else
        {
            qWarning() << Q_FUNC_INFO << "Unknown Fixture Mode tag:" << tag.tagName();
        }

        node = node.nextSibling();
    }

    // Cache all head channels
    cacheHeads();

    return true;
}
Ejemplo n.º 5
0
int main(){
	int h=0;
	struct data *head=NULL;
	struct data *tail=head;
	struct data *cur=head;
	char ch;
	
	while((ch=getchar()) == 'H' || ch == 'E');

	do{
		if( ch == 'H' ){
			h=1;
		}
		else if( ch == 'E' ){
			h=0;
		}
		else if ( h==1 ){
			insertHead(&head,&cur,ch);
			h=2;
		}
		else if (h == 2 ){
			insertCur(&cur,ch);
		}
		else 
			insertTail(&tail,&head,&cur,ch);
	}while((ch = getchar()) != '\n');
	printLine(&head);
	return 0;
}
Ejemplo n.º 6
0
/**
*	insertList
*
*		Insert a new entry either before or after an existing list entry. Paramater
*		pMember represents the address of the data to be inserted whereas iLocation
*		identifies the position relative to the existing entry pEntry.
*		If iLocation is less than zero the new entry is insert BEFORE the existing
*		entry otherwise it is inserted AFTER the existing entry.
*
*	@note	Do NOT test for the specific values like -1, 0 and 1 as different C
*			implementations and Operating Systems may return different values
*			when comparing strings.
*
*	@param	pMember			Address arbitrary data.
*	@param	pEntry			Address existing list entry.
*	@param	iLocation		Integer identifying the location were the new entry
*							is to be inserted relative to pEntry.
*
*	@return		Address newly allocated list ENTRY struct.
**/
ENTRY *insertList( void *pMember, ENTRY *pEntry, int iLocation )
{
	if( iLocation < 0 )
		return insertTail( pMember, (LIST *)pEntry );
	else
		return insertHead( pMember, (LIST *)pEntry );
}
Ejemplo n.º 7
0
bool QLCFixtureMode::loadXML(QXmlStreamReader &doc)
{
    if (doc.name() != KXMLQLCFixtureMode)
    {
        qWarning() << Q_FUNC_INFO << "Mode tag not found";
        return false;
    }

    /* Mode name */
    QString str = doc.attributes().value(KXMLQLCFixtureModeName).toString();
    if (str.isEmpty() == true)
    {
        qWarning() << Q_FUNC_INFO << "Mode has no name";
        return false;
    }
    else
    {
        setName(str);
    }

    /* Subtags */
    while (doc.readNextStartElement())
    {
        if (doc.name() == KXMLQLCFixtureModeChannel)
        {
            /* Channel */
            Q_ASSERT(m_fixtureDef != NULL);
            str = doc.attributes().value(KXMLQLCFixtureModeChannelNumber).toString();
            insertChannel(m_fixtureDef->channel(doc.readElementText()),
                          str.toInt());
        }
        else if (doc.name() == KXMLQLCFixtureHead)
        {
            /* Head */
            QLCFixtureHead head;
            if (head.loadXML(doc) == true)
                insertHead(-1, head);
        }
        else if (doc.name() == KXMLQLCPhysical)
        {
            /* Physical */
            QLCPhysical physical;
            physical.loadXML(doc);
            setPhysical(physical);
        }
        else
        {
            qWarning() << Q_FUNC_INFO << "Unknown Fixture Mode tag:" << doc.name();
            doc.skipCurrentElement();
        }
    }

    // Cache all head channels
    cacheHeads();

    return true;
}
Ejemplo n.º 8
0
 void insertTail(NODE* newNode) {
     if (mLast == 0) {
         insertHead(newNode);
     } else {
         newNode->prev = mLast;
         newNode->next = 0;
         mLast->next = newNode;
         mLast = newNode;
     }
 }
Ejemplo n.º 9
0
int
OrderList::add(ListEntry *node, AddCode where, bool mvcursor)
{
    int             status = OLIST_OK;

    if (node != NULL) {
        if (f_cursor == NULL)
	  insertNew(node);
	else {
	    switch (where)
	      {
		case addAfter:
		  if (f_cursor == f_tail)
		    insertTail(node);
		  else
		    insertAfter(node);
		  break;
		case addBefore:
		  if (f_cursor == f_head)
		    insertHead(node);
		  else
		    insertBefore(node);
		  break;
		case addHead:
		  insertHead(node);
		  break;
		case addTail:
		  insertTail(node);
		  break;
		default:
		  return OLIST_ERROR;
	      }
	}
        if ((mvcursor) || (f_cursor == NULL))
	  f_cursor = node;
    }
    else
        return OLIST_ERROR;

	f_size++;

    return OLIST_OK;
}
 ListNode *swap(ListNode *head, ListNode *originNext){
     ListNode *newHead = new ListNode(-1);
     newHead->next = originNext;
     ListNode *p = head;
     while(p != originNext){
         ListNode *node = p;
         p = p->next;
         insertHead(newHead, node);
     }
     return newHead->next;
 }
Ejemplo n.º 11
0
void SList::insertTail (int contents) {
    if(head == NULL){
        insertHead(contents);
    }
    else {
    SLNode* i = head;
    while (i->getNextNode() !=NULL) {
        i=i->getNextNode();
    }
    SLNode* node = new SLNode(contents);
    i->setNextNode(node);
    size++;
    }
}
Ejemplo n.º 12
0
	void SList::insert (int contents) {
	    if(head == NULL){
            insertHead(contents);
        } else {
            SLNode* temp=head;
            SLNode* temp2=temp;
            while (temp!=NULL && temp->getContents()<contents) {
                temp2=temp;
                temp=temp->getNextNode();
            }
            if(temp ==NULL){
                insertTail(contents);
            } 
            else if(temp==head){
                insertHead(contents);
                } else {
    	        SLNode* node= new SLNode(contents);
    	        node->setNextNode(temp);
    	        temp2->setNextNode(node);
    	        size++;
    	    }
	    }
	}
Ejemplo n.º 13
0
void SList::insertTail (int newTail) {
    SLNode* temp = new SLNode(newTail);
    if (head == NULL) {
        insertHead(newTail);
    }
    else {
    SLNode* i = head;
    while (i->getNextNode() != NULL) {
        i = i->getNextNode();
    }
    i->setNextNode(temp);
    size++;
    }
}
Ejemplo n.º 14
0
void SList::insert (int newContents) {
	if (head == NULL) {
		insertHead (newContents);
	}
	else if (head->getNextNode() == NULL) {
		if (newContents < head->getContents()) {
			insertHead(newContents);
		}
		else {	
			insertTail(newContents);
		}
	}
	else {
		SLNode* trailer = NULL;
		SLNode* leader = head;
		while (leader->getNextNode() != NULL && newContents > leader->getContents()) {
			trailer = leader;
			leader = leader->getNextNode();
		}
		
		if (leader->getNextNode() == NULL && newContents > leader->getContents()) {
			insertTail(newContents);
		}
		else {
			SLNode* theNode = new SLNode (newContents);
			theNode->setNextNode(leader);
			if (trailer == NULL) {
				head = theNode;
				numNodes++;
			}
			else {
			trailer->setNextNode(theNode);
			numNodes++;
			}
		}
	}
}
Ejemplo n.º 15
0
void SList::insertTail (int contents) {
	if (head == NULL) {
		insertHead(contents);
	}
	else {
		SLNode* i = head;
		SLNode* newNode = new SLNode(contents);
		
		while (i->getNextNode() != NULL) {
			i = i->getNextNode();
		}
		i->setNextNode(newNode);
		++numNodes;
	}
}
Ejemplo n.º 16
0
void SList::insertTail (int content)
{
    if (head == NULL)
        insertHead (content);
    
    else
    {
        SLNode* aNode = new SLNode(content);
        SLNode* i = head;
        while (i -> getNextNode() != NULL)
            i = i -> getNextNode();
        i -> setNextNode(aNode);
        size++;
    }
}
Ejemplo n.º 17
0
int main() {
  // the list's name is "nodolista"
  insertHead(&nodolista.next, 1);
  insertTail(&nodolista.next, 6);
  insertTail(&nodolista.next, 3);
  insertTail(&nodolista.next, 8);
  insertTail(&nodolista.next, 4);
  insertTail(&nodolista.next, 7);
  insertTail(&nodolista.next, 5);
  insertTail(&nodolista.next, 9);
  insertTail(&nodolista.next, 2);
  // end
  
  //printList
  printList(nodolista.next);
  
  //getHead
  printf("\nHead:\t[ (%d) %d ]\n", getHead(nodolista.next)->index, getHead(nodolista.next)->info);
  
  //getTail
  printf("Tail:\t[ (%d) %d ]\n", getTail(nodolista.next)->index, getTail(nodolista.next)->info);
 
  // getPrev 
  int k = 0;
  printf("Insert an index to return the prev (starting from 0 to %d): ", getIndex(getTail(nodolista.next)));
  scanf("%d", &k);
  printf("The prev of value %d is: %d\n", getValue(nodolista.next, k)->info, \
  									(getPrev(nodolista.next, k) ? getPrev(nodolista.next, k)->info : 0));
  
  // search
  k = 0;
  printf("Insert a value to search into the list: ");
  scanf("%d", &k);
  printf("The search went: %s\n", (search(nodolista.next, k) ? "true" : "false"));
  
  // delete
  printf("Insert an item in the list to delete: ");
  scanf("%d", &k);
  delete(nodolista.next, k);
  printList(nodolista.next);
  
  // inserction sort
  inserctionSort(nodolista.next);
  printList(nodolista.next);
  return 0;
} //end main
Ejemplo n.º 18
0
/*!
 * \brief encode
 * \param bufferIn : buffer di dati in ingresso
 * \param bufferOut : buffer di dati in uscita dove vengono duplicati i DLE ed inoltre
 *                    vengono aggiunti i DLE-STX in testa al buffer e DLE-ETX in coda
 *                    al buffer
 */
void encode (const QByteArray &bufferIn, QByteArray &bufferOut)
{
    insertHead(bufferOut);

    int end = bufferIn.length();
    char dato;
    for (int idx = 0; idx < end; idx++)
    {
        dato = bufferIn[idx];
        bufferOut.append(dato);
        // Se il dato da inserire e' il DLE allora lo duplico
        if (dato == DLE)
            bufferOut.append(DLE);
    }

    insertTail(bufferOut);
}
Ejemplo n.º 19
0
void SList::insertTail(int value)
{
    if(head == NULL)
    {
        insertHead(value);
    }
    else
    {
        SLNode* newNode = new SLNode(value);
        SLNode* temp = head;
        while(temp->getNextNode() != NULL)
        {
            temp = temp->getNextNode();
        }
        temp->setNextNode(newNode);
        size++;
    }
}// create a new SLNode and attach at the end of list
Ejemplo n.º 20
0
void SList::insertTail (int contents){
     
	if(head != NULL) {
		//Finds the tail. 
		SLNode* i = head;
		while (i->getNextNode() != NULL) {
			//Starts at head and moves it over to the last node. 
			i = i->getNextNode();
		}
		SLNode* node = new SLNode(contents);
		//points at the end and inserts the new node that was just created. 
		i->setNextNode(node);
		size++;
	} else {
		//Empty list - make it to insert head as it's the same thing. 
		insertHead(contents);
	}
 
}
Ejemplo n.º 21
0
void SList::insertTail(int nodeContents) {
    if (head == NULL) {
        insertHead(nodeContents);
    } else {
        SLNode* newTail = new SLNode(nodeContents);
        if (head->getNextNode() == NULL) {
            head->setNextNode(newTail);
        } else {
            SLNode* oldTail = head->getNextNode();
            for (unsigned int i = 1; i < size; i++){
                if (oldTail-> getNextNode() != NULL){
                    oldTail = oldTail->getNextNode();
                }
            }
            oldTail->setNextNode(newTail);
            oldTail = NULL;
        }
        newTail = NULL;
        size++;
    }
}
Ejemplo n.º 22
0
void SList::insert(int contents) {
    if (head == NULL) {
        insertHead(contents);
    } else {
        size++;
        SLNode* newNode = new SLNode(contents);
        if (head->getContents() > contents){
            SLNode* oldHead = head;
            head = newNode;
            head->setNextNode(oldHead);
            oldHead = NULL;
        } else {
            if (head->getNextNode() == NULL) {
                head->setNextNode(newNode);
            } else {
                SLNode* nodeMark = head->getNextNode();
                SLNode* lagMark = NULL;
                for (unsigned int i = 0; i < size; i++) {
                    if (nodeMark->getContents() < contents && nodeMark->getNextNode() != NULL) {
                        lagMark = nodeMark;
                        nodeMark = nodeMark->getNextNode();
                    }
                }
                if (lagMark == NULL) {
                    head->setNextNode(newNode);
                    newNode->setNextNode(nodeMark);
                } else if (nodeMark->getNextNode() == NULL) {
                    nodeMark->setNextNode(newNode);
                } else {
                    lagMark->setNextNode(newNode);
                    newNode->setNextNode(nodeMark);
                }
                nodeMark = NULL;
                lagMark = NULL;
            }
        }
        newNode = NULL;
    }
}
Ejemplo n.º 23
0
			void update(const SurfacePatch& co)
			{
				typedef std::list<iterator> iterator_list;
			    iterator_list merged;

			    // make a copy of the surfacepatch as it may get updated in the merge
			    SurfacePatch o( co );

			    for (iterator it = begin(); it != end(); it++)
			    {
					// merge the patches and remember the ones which where merged 
					if (merge(*it, o))
				    	merged.push_back(it);
			    }

			    if (merged.empty())
			    {
					// insert the patch since we didn't merge it with any other
					insertHead(o);
			    } else {
					// if there is more than one affected patch, merge them until 
					// there is only one left
					while (!merged.empty())
					{
					    iterator_list::iterator it = ++merged.begin();
					    while( it != merged.end() ) 
					    {
							if(merge(**merged.begin(), **it))
							{
							    erase(*it);
							    it = merged.erase(it);
							}
							else
							    it++;
					    }
					    merged.pop_front();
					}
			    }				
			}	
 ListNode* reverseBetween(ListNode* head, int m, int n) {
     if (head == NULL) {
         return head;
     }
     int cnt = 0;
     ListNode *h1, *t1, *h2, *t2, *h3, *t3;
     ListNode *p1, *p2;
     
     h1 = t1 = NULL;
     h2 = t2 = NULL;
     h3 = t3 = NULL;
     p1 = head;
     while (p1 != NULL) {
         ++cnt;
         p2 = p1;
         p1 = p1->next;
         p2->next = NULL;
         if (cnt < m) {
             insertTail(h1, t1, p2);
         } else if (cnt <= n) {
             insertHead(h2, t2, p2);
         } else {
             insertTail(h3, t3, p2);
         }
     }
     if (h1 != NULL) {
         t1->next = h2;
         t1 = t2;
     } else {
         h1 = h2;
         t1 = t2;
     }
     if (h3 != NULL) {
         t1->next = h3;
         t1 = t3;
     }
     return h1;
 }
Ejemplo n.º 25
0
void addHeapSpace(size_t allocate_size){
	uintptr_t *header_address = (((uintptr_t *)end) + 1);
	uintptr_t *temp_footer = header_address +( allocate_size / 8) + 1;

	int increments = 0;
	while((void *)temp_footer >= end){
		inc_heapSpace();
		increments ++;
	}
	uintptr_t *footer_address = (((uintptr_t *)end) - 2);

	uintptr_t init_info = createHeader(STD_SBRK_REQ, increments * STD_SBRK_REQ - 16, MARK_FREE);
	*header_address = init_info;
	*footer_address = init_info;

	if((uintptr_t) head != NULL_POINTER){
		insertHead(header_address);

	}else{
		*(header_address + 1) = (uintptr_t) NULL_POINTER;
		*(header_address + 2) = (uintptr_t) NULL_POINTER;
		head = (void *) header_address;
	}
}
Ejemplo n.º 26
0
int SLInsert(SortedListPtr list, int index){
	if(list==NULL){
		return 0;

	}
	node temp=(node)malloc(sizeof(struct node_list));
	/*if malloc somehow fails, we return 0*/
	if(temp==NULL){
		return 0;
	}
	node prev;
	node curr;
	temp->next=NULL;
	temp->prev=NULL;
	temp->index=index;
	curr=list->head;
	if((list->head==NULL)){
		list->head=temp;
		list->tail=list->head;
		return 1;
	}

	/*already in the list, no need to call insert head, free it*/
	if(curr->index==temp->index){
		free(temp);
		return 1;
	}
	/*used this for only one node in list and it's not the head. We simply
	*make the newly added node the new head and the curr the tail
	*for traversing the list easier in while loop*/
	if(curr->next==NULL &&curr->index!=temp->index){
		/*printf("\ndid I come here and break");*/
		curr->prev=temp;
		temp->next=curr;
		temp->prev=NULL;
		list->tail=curr;
		list->head=temp;
		return 1;
	}
	prev=curr;
	curr=curr->next;
	while(curr!=NULL){
		if(curr->index==temp->index){ /*if it equals curr, and curr next is null, delink everything, insert*/
			if(curr->next==NULL){
				free(temp);
				prev->next=NULL;
				curr->next=NULL;
				curr->prev=NULL;
				list->tail=prev;
				insertHead(list, curr, index);
				return 1;
			
			}
			/*found our target, make it delink with everything, insert at head*/
			free(temp);
			prev->next=curr->next;
			curr->next->prev=prev;
			curr->next=NULL;
			curr->prev=NULL;
			insertHead(list, curr, index);
			return 1;
		}
		prev=curr;							/*else we simply move along the list*/
		curr=curr->next;
	}
	/*I can assume it will be inserted in the front if curr is null*/
	list->tail=prev;
	insertHead(list, temp, index);
	return 1;
}
void main(){
	int input;  // 사용자 입력 코드
	Dlist D = createDlist(); // 사용자 입력으로 아이템이 입력되는 리스트
    
	Dlist D2 = createDlist();   // join용 리스트
	insertHead(D2,10);
	insertHead(D2,13);
	insertHead(D2,17);
	insertHead(D2,19);
	insertHead(D2,21);
	insertHead(D2,26);

	Dlist newList = createDlist();
	printf("==============140529 201100646 김영태 과제==============\n\n");
	printf("주의!! : movebefore시 기준 노드의 유일성을 보장하기 위하여 중복된 값 입력은 허용되지 않습니다. \n");
	printf("주의!! : Join시 합칠 리스트는 코드상에 미리 정의되어있습니다. \n");
	printf("주의!! : 때문에 Join은 한번밖에 실행되지 않습니다.(그 이상 실행할시 에러)\n\n");
	printf("=====================================================\n\n");

	while(1){
		int val;    // 사용자입력에 따른 변수들
		int node_val;
		dnode d;
		dnode d1;
		dnode d2;
		Dlist splitD;
		Dlist temp;
		char* result = "결과 리스트";

		printf("원하시는 동작에 따라서 숫자를 입력해주세요\n");    // 사용자 인터페이스
		printf("(1: insert Head, 2: insert After, 3: insert before, 4: move before, ");
		printf("5: search, 6: split, 7:join) \n");
		scanf("%d", &input);

		switch(input){
			case 1 :    //  insert Head
				printf("삽입을 원하시는 값을 입력해주세요 : ");
				scanf("%d", &val);
				if(search(D,val)){
					printf("중복된 값입니다\n");
					break;
				}
				insertHead(D,val);
				printList(D,result);
				break;
				
				
			case 2 :  // insert After
				printf("삽입을 원하시는 값을 입력해주세요 : ");
				scanf("%d", &val);
				if(search(D,val)){
					printf("중복된 값입니다\n");
					break;
				}
				printf("어느 값의 노드 다음에 삽입을 원하시는지 말씀해주세요\n");
				d = findNode(D);
				if(d == NULL){
					printf("찾으시는 값이 없습니다\n");
					break;
				}else{
					insertAfter(D,d,val);
					printList(D,result);
					break;
				}
			case 3 :    // insert Before
				printf("삽입을 원하시는 값을 입력해주세요 : ");
				scanf("%d", &val);
				if(search(D,val)){
					printf("중복된 값입니다\n");
					break;
				}
				printf("어느 값의 노드 이전에 삽입을 원하시는지 말씀해주세요\n");
				d = findNode(D);
				if(d == NULL){
					printf("찾으시는 값이 없습니다\n");
					break;
				}else{
					insertBefore(D,d,val);
					printList(D,result);
					break;
				}
			case 4 :    //move Before
				printList(D,"현재 리스트");
				printf("옮길 첫번째 노드를 선택해 주세요 \n");
				d1 = findNode(D);
				if(d1 == NULL){
					printf("찾으시는 값이 없습니다\n");
					break;
				}
				printf("옮길 마지막 노드를 선택해 주세요 \n");
				d2 = findNode(D);
				if(d2 == NULL){
					printf("찾으시는 값이 없습니다\n");
					break;
				}
				printf("어느 노드 전으로 옮기실 건가요?(겹치지 않게 주의해주세요.) \n");
				d = findNode(D);
				if(d == NULL){
					printf("찾으시는 값이 없습니다\n");
					break;
				}
				moveBefore(D,d1,d2,d);
				printList(D,result);
				break;

			case 5 :    // search
				if(findNode(D)){
					printList(D,"현재 리스트");
					printf("찾으신 값이 리스트내에 존재합니다.\n");
					break;
				}else{
					printList(D,"현재 리스트");
					printf("찾으신 값이 리스트내에 존재하지 않습니다.\n");
					break;
				}
			case 6 :    // split
				printList(D,"현재 리스트");
				printf("나눌 기준이 되는 노드의 값을 입력하세요 : ");
				scanf("%d",&val);
				d = search(D,val);
				if(d==NULL){
					printf("찾으시는 값이 없습니다\n");
					break;
				}

				splitD = split(D,d);

				printList(D,"기존리스트");
				printList(splitD,"나눠진 리스트");
				break;

			case 7 :    //join
				printList(D,"현재 리스트");
				while(1){

				}
				printList(D2,"조인할 리스트");
				join(D,D2);
				printList(D,"결과");
				break;
			default :   // default input
				printf("잘못된 입력입니다.\n");
				break;
		}

	}

}
Ejemplo n.º 28
0
void sf_free(void *ptr){
	/* ptr is not a valid address within dynamic memory space */
	if(ptr < start || ptr >= end) return;

	/* address provided is already a free region of memory */
	if((*(((uintptr_t*)ptr) - 1) & EXTRACT_MARKER_BIT) == MARK_FREE)
		return;

	uintptr_t *cursor = (uintptr_t*) (((uintptr_t*) ptr) - 1);
	
	uintptr_t ptr_as_int = (uintptr_t) cursor;
	ptr_as_int = ptr_as_int & 0xF;
	if(ptr_as_int != 8){	
		errno = ENOMEM;
		return;
	}

	uintptr_t cursorSize = get_loadSize(cursor);
	uintptr_t *cursorFooter = cursor + (cursorSize / 8) + 1;
	uintptr_t *newBlockHeader = (uintptr_t*) NULL_POINTER;
	uintptr_t *newBlockFooter = (uintptr_t*) NULL_POINTER;
	uintptr_t *previousFooter = cursor - 1;
	if((void*) previousFooter > start){
		uintptr_t previousSize = get_loadSize(previousFooter);
		uintptr_t perviousMarker = (uintptr_t) (*previousFooter & EXTRACT_MARKER_BIT);
		uintptr_t *previousHeader = previousFooter - (previousSize / 8) - 1;
		if((void*) previousHeader > start && perviousMarker == MARK_FREE){
			uintptr_t *previousNext = (uintptr_t *) *(previousHeader + 1);
			uintptr_t *previousPrevious = (uintptr_t*) *(previousHeader + 2);

			if((uintptr_t) previousNext != NULL_POINTER && (uintptr_t) previousPrevious != NULL_POINTER){
				*(previousNext + 2) = (uintptr_t) previousPrevious;
				*(previousPrevious + 1) = (uintptr_t) previousNext;

				newBlockHeader = previousHeader;

			}else{
				newBlockHeader = cursor;
			} 
		}else{
			newBlockHeader = cursor;
		}
	}else{
		newBlockHeader = cursor;
	} 

	uintptr_t *nextHeader = cursorFooter + 1;
	if((void*) nextHeader < end){
		uintptr_t nextSize = get_loadSize(nextHeader);
		uintptr_t nextMarker = (uintptr_t) (*nextHeader & EXTRACT_MARKER_BIT);
		uintptr_t *nextFooter = nextHeader + (nextSize / 8) + 1;
		if((void*) nextFooter < end - 1 && nextMarker == MARK_FREE){
			uintptr_t *nextNext = (uintptr_t*) *(nextHeader + 1);
			uintptr_t *nextPrevious = (uintptr_t*) *(nextHeader + 2);
	
			if((uintptr_t) nextNext != NULL_POINTER && (uintptr_t) nextPrevious != NULL_POINTER){
				*(nextNext + 2) = (uintptr_t) nextPrevious;
				*(nextPrevious + 1) = (uintptr_t) nextNext;

				newBlockFooter = nextFooter;
			}else{
				newBlockFooter = cursorFooter;
			}
		}else{
			newBlockFooter = cursorFooter;
		}
	}else{
		newBlockFooter = cursorFooter;
	}

	uintptr_t newBlockSize = get_Size(newBlockHeader, newBlockFooter) + 8;
	uintptr_t newBlockInfo = createHeader(0, newBlockSize, MARK_FREE);

	*newBlockHeader = newBlockInfo;
	*newBlockFooter = newBlockInfo;

	insertHead(newBlockHeader);
}
Ejemplo n.º 29
0
List* insertWordST(List *l, Word *w) {
	l = insertHead(l, w);

	return l;
}