/*
This function first finds the node with the given value and then deletes that node from the list
*/
int DeleteList(List l, int val)
{
    struct Node *remove, *temp;

    if (!IsEmptyList(l))
    {
         temp = temp->next;
         while ((temp->next != NULL) && (temp->next->val != val))
              temp = temp->next;

         if (temp->next == NULL)
            printf("Element %d not found in linked list\n",8);
         else
         {
             remove = temp->next;
             temp->next = temp->next->next;
             free(remove);
             l->size--;
         }

         if (l->size == 0)
         l->tail = l->head;
    }
    else
    {
        printf("The linked list is empty\n");
    }

}
Пример #2
0
Файл: list.c Проект: 3mao/com
/***********2链表插入函数***********************
函数名称:InsertPosList
参    数:
			链表头结点地址		pHead			
			插入链表中位置		Pos
			插入结点地址		pVal
说	  明:
			pHead头结点不包含数据
			Pos取值: 1.....n
			Pval为插入结点地址
创建时间:	
			2013年8月17日9:25:03
作    者:	
			罗明刚
************************************************/
 int InsertPosList(pNode pHead,int Pos, pNode pVal)
 {
	pNode p =NULL;
	int i = 0;

	if(IsEmptyList(pHead))
	{
		return -1;
	}

	for(p = pHead; NULL != p->pNext && i< (Pos-1); i++,p = p->pNext );

	while(i > (Pos-1) || NULL == p->pNext )
	{
		return -1;
	}

	pNode pNew;

	if(NULL == (pNew = (pNode)malloc( sizeof(Node) )))
	{
		return -1;
	}

	memcpy(pNew,pVal,sizeof(Node));

	pNode pTemp = p->pNext;
	p->pNext = pNew;
	pNew->pNext = pTemp;

	return 0;
 }
/*
This function returns the head of the list
*/
int HeadOfList(List l)
{
    if (!IsEmptyList(l))
		return l->head->next->val;
    else
    {
        printf("The linked list is empty\n");
            return -1;
    }
}
/*
This function returns the tail of the list
*/
int TailOfList(List l)
{
	if (!IsEmptyList(l))
	    return l->tail->val;
     else
     {
         printf("The linked list is empty\n");
         return -1;
     }
}
Пример #5
0
/* remove from top of DLL
 * @linkedList: linkedList to remove from
 * Returns pointer of the WebPage that was removed, NULL if fail
 */
WebPage *removeTop(List *linkedList){
	if(IsEmptyList(linkedList)) return NULL;
	WebPage *webTemp = linkedList->head->page; 
	ListNode *toFreeNode = linkedList->head; // need to keep this since this node needs to be freed
	linkedList->head = linkedList->head->next; // make the new head the one after current head
	if(linkedList->head) linkedList->head->prev = NULL; // make the new heads previous to NULL

	free(toFreeNode); // free memory of ListNode struct.

	return webTemp;
}
Пример #6
0
SReference& SReference::ChangeListEnd(const SReference &new_last)
{
    if(GetPtr() && (*this)->TermType() == SExpressionCons::TypeId) {
        (((SExpressionCons*)GetPtr())->Cdr()).ChangeListEnd(new_last);
    } else
    if(!GetPtr() || IsEmptyList()) {
        (*this) = new_last;
    } else {
        throw IntelibX_not_a_list(*this);
    }
    return *this;
}
Пример #7
0
SReference& SReference::AddAnotherItemToList(const SReference &right)
{
    if(GetPtr() && (*this)->TermType() == SExpressionCons::TypeId) {
        (((SExpressionCons*)GetPtr())->Cdr()).AddAnotherItemToList(right);
    } else
    if(!GetPtr() || IsEmptyList()) {
        (*this) = new SExpressionCons(right, *PTheEmptyList);
    } else {
        throw IntelibX_not_a_list(*this);
    }
    return *this;
}
void PrntList(NodeType *phead)
{
	NodeType *pcur=phead;
	if(!IsEmptyList(phead))
	{
		printf("--ID--\n");
		do
		{
			printf("%3d\n",pcur->id);
			pcur=pcur->next;
		}while(pcur!=phead);
	}
}
void InsertLast(List *L, addressList P)
/* I.S. Sembarang, P sudah dialokasi */
/* F.S. P ditambahkan sebagai elemen terakhir yang baru */
{
    if (IsEmptyList(*L))
    {
        InsertFirst(L, P);
    }
    else
    {
        addressList Last = First(*L);
        while (Next(Last) != NULL)
            Last = Next(Last);
        InsertAfter(L, P, Last);
    }
}
Пример #10
0
Файл: list.c Проект: 3mao/com
int  TraverseSingleList(pNode pHead)
{
	pNode p;

	if( IsEmptyList(pHead) )
	{
		return 0;
	}

	for(p = pHead->pNext; NULL != p; p = p->pNext)
	{
		printf("(%d)\n",p->data.x);
	}

	return 1;
}
Пример #11
0
/****************** PENCARIAN SEBUAH ELEMEN LIST ******************/
addressList Search(List L, infotypeList X)
/* Mencari apakah ada elemen list dengan Info(P)=X */
/* Jika ada, mengirimkan addressList elemen tersebut. */
/* Jika tidak ada, mengirimkan NULL */
{
    if (IsEmptyList(L))
    {
        return NULL;
    }
    else
    {
        addressList P = First(L);
        while (Next(P) != NULL && X != Info(P))
            P = Next(P);
        return X == Info(P) ? P : NULL;
    }
}
Пример #12
0
/****************** PROSES SEMUA ELEMEN LIST ******************/
void PrintInfo(List L)
/* I.S. List mungkin kosong */
/* F.S. Jika list tidak kosong, isi list dicetak ke kanan: [e1,e2,...,en] */
/* Contoh : jika ada tiga elemen bernilai 1, 20, 30 akan dicetak: [1,20,30] */
/* Jika list kosong : menulis [] */
/* Tidak ada tambahan karakter apa pun di awal, akhir, atau di tengah */
{
    printf("[");
    if (!IsEmptyList(L))
    {
        addressList P = First(L);
        printf("%d", Info(P));
        P = Next(P);
        while (P != NULL)
        {
            printf(",%d", Info(P));
            P = Next(P);
        }
    }
    printf("]");
}
Пример #13
0
/* append to a WebPage to the linked list
 * @webPage: WebPage to add to the linked list
 * @linkedList: linkedList to append the WebPage
 * Returns 1 on successful append, 0 if fail.
 */
int appendDLL(WebPage *webPage, List *linkedList){
	// allocate memory for the node to append.
	ListNode *addNode;
	addNode = calloc(1, sizeof(ListNode));
	if(!addNode) return 0;
	addNode->page = webPage; 
	
	// if both head and tail is NULL then this is an empty list, so set head and tail to the
	// added node, and set the previous and next of the added node to NULL as it points to nothing
	// yet.
	if(IsEmptyList(linkedList)){
		linkedList->head = addNode;
		linkedList->tail = addNode;
		return 1; // return 1 on success
	}
	else{ // if not the above case, the list is not empty
		addNode->prev = linkedList->tail; // the previous of the adding node is the tail node
		addNode->prev->next = addNode;   // link the tail element with the adding node 
		linkedList->tail = addNode;		    // the adding node is the new tail
		return 1;
	}
}
Пример #14
0
void Konkat1(List *L1, List *L2, List *L3)
/* I.S. L1 dan L2 sembarang */
/* F.S. L1 dan L2 kosong, L3 adalah hasil konkatenasi L1 & L2 */
/* Konkatenasi dua buah list : L1 dan L2 */
/* menghasilkan L3 yang baru (dengan elemen list L1 dan L2) */
/* dan L1 serta L2 menjadi list kosong. */
/* Tidak ada alokasi/dealokasi pada prosedur ini */
{
    CreateEmptyList(L3);
    if (IsEmptyList(*L1))
    {
        First(*L3) = First(*L2);
    }
    else
    {
        First(*L3) = First(*L1);
        addressList Last1 = First(*L1);
        while (Next(Last1) != NULL)
            Last1 = Next(Last1);
        Next(Last1) = First(*L2);
    }
    First(*L1) = First(*L2) = NULL;
}
Пример #15
0
Файл: list.c Проект: 3mao/com
int DeletePosList(pNode pHead,int Pos)
{
	pNode p = NULL;
	pNode pDelete = NULL;
	int i = 0;

	if( IsEmptyList(pHead) )
	{
		return 0;
	}

	for(p = pHead, i = 0; NULL != p->pNext && i<(Pos-1);p = p->pNext,i++);
	while( NULL==p->pNext&&Pos-1>i )
	{
		return 0;
	}
	pDelete = p->pNext;
	p->pNext = p->pNext->pNext;
	free(pDelete);
	pDelete = NULL;

	return 1;
}
Пример #16
0
addressList SearchPrec (List L, infotypeList X)
/* Mengirimkan addressList elemen sebelum elemen yang nilainya=X */
/* Mencari apakah ada elemen list dengan Info(P)=X */
/* Jika ada, mengirimkan addressList Prec, dengan Next(Prec)=P dan Info(P)=X. */
/* Jika tidak ada, mengirimkan NULL */
/* Jika P adalah elemen pertama, maka Prec=NULL */
/* Search dengan spesifikasi seperti ini menghindari */
/* traversal ulang jika setelah Search akan dilakukan operasi lain */
{
	if (IsEmptyList(L))
    {
        return NULL;
    }
    else
    {
        addressList P = First(L), Prec = NULL;
        while (Next(P) != NULL && X != Info(P))
        {
			Prec = P;
            P = Next(P);
		}
        return X == Info(P) ? Prec : NULL;
    }
}
Пример #17
0
Файл: list.c Проект: 3mao/com
/*
------------------------------------------------
函数名称:ReversePosList
------------------------------------------------
*/
pNode ReversePosList(pNode pHead)  //链表翻转
{
	pNode p = pHead->pNext;
	pNode q = p->pNext;
	pNode t = NULL;

	if(IsEmptyList(pHead))
	{
		return NULL;
	}

	for(; NULL != q; )
	{
		t = q->pNext;  //将q节点后面的节后保存起来
		q->pNext = p;  //前面是q在p后面,现在让q在p前面实现翻转
		p = q;			//向为节点方向移动一个节点
		q = t;          //向尾节点方向移动一个节点
	}

	pHead->pNext->pNext = NULL;
	pHead->pNext = p;

	return pHead;
}
Пример #18
0
 //! Is the queue currently empty
 bool IsEmpty() const {
     return IsEmptyList();
 }
Пример #19
0
     //! Either the list is empty or we stepped past the last item
 bool Exhausted() const { return !GetPtr() || IsEmptyList(); }