// ------------------------------------------------------------------------------------------------ static void TestCaseEnd() { ASSERT_TRUE(ListIsEmpty(&s_outPackets)); ASSERT_TRUE(ListIsEmpty(&g_tcpActiveConns)); ASSERT_EQ_INT(g_netBufAllocCount, 0); ASSERT_EQ_UINT(outError, 0); }
void TestAppendItem( void ) { UInt16* item1; UInt16* item2; UInt16* data1; UInt16* data2; ASSERT( ListIsEmpty( list )); data1 = MemPtrNew( sizeof *data1 ); *data1 = 6; ListAppend( list, data1 ); ASSERT( ListSize( list ) == 1 ); ASSERT( ! ListIsEmpty( list )); data2 = MemPtrNew( sizeof *data2 ); *data2 = 10; ListAppend( list, data2 ); ASSERT( ListSize( list ) == 2 ); item1 = ListGet( list, 1 ); item2 = ListGet( list, 2 ); ASSERT_UINT16_EQUAL_MSG( "First item: ", 6, *item1 ); ASSERT_UINT16_EQUAL_MSG( "Second item: ", 10, *item2 ); }
int main(void) { List list; InitializeList(&list); // test Initialize if(ListIsEmpty(&list)) //test ListIsEmpty printf("the list is empty\n"); else printf("the list is not empty\n"); // unsigned int counter=0; // counter=ListItemCount(&list); Node* pnode; pnode=(Node*)malloc(sizeof(Node)); Item *pitem; pitem=(Item*)malloc(sizeof(Item)); pitem->ID=001; strcpy(pitem->content, "hello list"); pitem->value=1; pnode->item=*pitem; list=pnode; list=AddNew(list); //test AddNew unsigned int counter=0; counter=ListItemCount(&list); printf("there are %u items in the list\n", counter); // void (*pfun)(Item item)=showitem; Traverse(&list,showitem); //test Traverse // EmptyTheList(&list);d if(ListIsEmpty(&list)) printf("list is empty!\n"); else printf("list is not empty!\n"); EmptyTheList(&list); return 0; }
void DeListIndex(List *plist, unsigned int nIndex) { if(ListIsEmpty(plist) == 1 || nIndex >= GetListSize(plist)) { return; } /*上锁*/ pthread_mutex_lock(&plist->mutex); PNode freenode = plist->front; PNode lastnode = NULL; if (0 == nIndex) { plist->front = plist->front->next; } else { unsigned i = 0; for (; i < nIndex; i++) { lastnode = freenode; freenode = freenode->next; } lastnode->next = freenode->next; } free(freenode->data); free(freenode); plist->size--; pthread_mutex_unlock(&plist->mutex); return; }
void DoubleLinkedList::removeElement(int position) { node *tmp; tmp = head; int num=0; if(!ListIsEmpty() && position >= 0) { while(num<position){ if(tmp->next != NULL){ tmp=tmp->next; num++; }else break; } if(tmp->next == NULL && tmp->prev == NULL)removeList(); else if(tmp->next != NULL) { if(tmp->prev !=NULL) { tmp->prev->next=tmp->next; tmp->next->prev=tmp->prev; } else{ head = tmp->next; head->prev = NULL; } } else { tail = tmp->prev; tail->next = NULL; } delete tmp; } }
/*创建存放项目的节点并添加到列表尾部*/ bool AddItem(Item item, List * plist) { Node * pnode; bool empty; /*用于判断列表是否为空*/ pnode = (Node *)malloc(sizeof(Node)); if(pnode == NULL) /*内存分配失败*/ return false; /*退出函数*/ else { CopyToNode(item, pnode); pnode->next = NULL; empty = ListIsEmpty(plist); if(empty) { plist->head = pnode; plist->tail = pnode; } else { plist->tail->next = pnode; plist->tail = pnode; } plist->size++; /*长度增长*/ return true; } }
void TestRemove( void ) { UInt16* item1; UInt16* item2; ASSERT( ListIsEmpty( list )); AddTwoItems(); item1 = ListFirst( list ); ListTakeOut( list, item1 ); ASSERT( ListSize( list ) == 1 ); MemPtrFree( item1 ); item1 = ListFirst( list ); item2 = ListLast( list ); ASSERT_UINT16_EQUAL_MSG( "First item: ", 10, *item1 ); ASSERT_UINT16_EQUAL_MSG( "Last item: ", 10, *item2 ); ListTakeOut( list, item2 ); ASSERT( ListSize( list ) == 0 ); ASSERT( ListFirst( list ) == NULL ); ASSERT( ListLast( list ) == NULL ); MemPtrFree( item2 ); }
/* gets the data of the recNum-th node by key */ Item getItemByKey(List *ptrList,Item *item) { Node *ptrNode; Item tmp={"No Item"}; if(!ptrList) { NO_LIST; return tmp; } if(ListIsEmpty(ptrList)) { LIST_IS_EMPTY; return tmp; } ptrNode = ptrList->start; while (ptrNode) { if(COMPARE(ptrNode->data,*item)) /*this macro return TRUE if equal*/ return ptrNode->data; ptrNode = ptrNode->next; } return tmp; }
void sortResults( List* plist, int sortMethod ) { if ( ListIsEmpty( plist ) ) MessageBox( NULL, TEXT( "No results - sortResults()" ), TEXT( "Dir Glance" ), MB_ICONERROR ); else { // Sort results switch( sortMethod ) { case BY_MODIF : SortList( plist, cmpItemsLastWriteTime ); break; case BY_FILES : SortList( plist, cmpItemsFilesCount ); break; case BY_DIRS : SortList( plist, cmpItemsDirsCount ); break; case BY_NAME : SortList( plist, cmpItemsName ); break; case BY_SIZE : default : SortList( plist, cmpItemsSizeCount ); break; } } }
/*从链表头插入一个元素*/ int InsertFromHead(List *ListBuf, int nData) { List *Node =NULL; /*链表为空的情况下,返回错误*/ if(ListIsEmpty(ListBuf)) { PrintLog(ERROR, "不能插入表头为NULL的链表"); return -1; } else /*链表不为空的情况下在头部插入一个新的节点*/ { Node = malloc(sizeof(List)); if(Node == NULL) { PrintLog(ERROR, "malloc Node error"); return -1; } Node->nData=nData; if(ListBuf->next == NULL) { ListBuf->next = Node; Node->next = NULL; } else { Node->next = ListBuf->next; ListBuf->next = Node; } } return 0; }
/* removes the first node from the list and returns its data */ Item removeFirst(List *ptrList) { Node * ptrDel; Item tmp={"No Item"}; if(!ptrList) { NO_LIST; return tmp; } if(ListIsEmpty(ptrList)) { LIST_IS_EMPTY; return tmp; } if(ptrList->start == ptrList->end) ptrList->end=NULL; ptrDel = ptrList->start; ptrList->start = ptrDel->next; ptrList->num--; tmp = ptrDel->data; free(ptrDel); return tmp; }
/*清空一个列表*/ void ClearList(List *plist) { while(ListIsEmpty(plist)!=1) { DeListFront(plist); } }
// ------------------------------------------------------------------------------------------------ static Packet *PopPacket() { ASSERT_TRUE(!ListIsEmpty(&s_outPackets)); Packet *packet = LinkData(s_outPackets.next, Packet, link); LinkRemove(&packet->link); ValidateChecksum(packet); return packet; }
void DoubleLinkedList::moveToTail(int pos) { node *tmp; tmp = head; int num=0; if(!ListIsEmpty() && pos >= 0) { while(num<pos){ if(tmp->next != NULL){ tmp=tmp->next; num++; }else break; } if(tmp != tail) { if(tmp != head) { if(tmp->next != tail) { tmp->prev->next = tmp->next; tmp->next->prev = tmp->prev; tail->next = tmp; tmp->next = NULL; tmp->prev = tail; tail = tmp; }else { tail->prev = tmp->prev; tail->next = tmp; tmp->prev->next = tail; tmp->prev = tail; tmp->next = NULL; tail = tmp; } }else if(tmp->next != tail) { tail->next = tmp; head = tmp->next; head->prev = NULL; tmp->prev = tail; tail = tmp; tail->next = NULL; } else { head->next = NULL; head->prev = tail; tail->prev = NULL; tail->next = head; tmp = tail; tail = head; head = tmp; } } } }
int main(void) { List movies; Item temp; /* initialize */ InitializeList(&movies); if (ListIsFull(&movies)) { fprintf(stderr,"No memory available! Bye!\n"); exit(1); } /* gather and store */ puts("Enter first movie title:"); while (get(temp.title, TSIZE) != NULL && temp.title[0] != '\0') { puts("Enter your rating <0-10>:"); scanf("%d", &temp.rating); while(getchar() != '\n') continue; if (AddItem(temp, &movies)==false) { fprintf(stderr,"Problem allocating memory\n"); break; } if (ListIsFull(&movies)) { puts("The list is now full."); break; } puts("Enter next movie title (empty line to stop):"); } /* display */ if (ListIsEmpty(&movies)) printf("No data entered. "); else { printf ("Here is the movie list:\n"); Traverse(&movies, showmovies); } printf("You entered %d movies.\n", ListItemCount(&movies)); /* clean up */ EmptyTheList(&movies); printf("Bye!\n"); return 0; }
void DoubleLinkedList::removeList() { if(!ListIsEmpty()){ while(head->next!=NULL){ head=head->next; head->prev=NULL; delete head->prev; } head=NULL; delete head; } }
int main(void) { List movies; Item temp; /* Initialize */ InitializeList(&movies); if(ListIsFull(&movies)) { fprintf(stderr,"No memory avaliable! Bye!\n"); exit(1); } /* collecte and storage data */ puts("Enter first movie title :"); while(gets(temp.title) != NULL && temp.title[0] != '\0') { puts("Enter your rating <0-10> : "); scanf("%d",&temp.rating); while(getchar() != '\n'); if(AddItem(temp,&movies) == false) { fprintf(stderr,"Problem allocating memory\n"); break; } if(ListIsFull(&movies)) { puts("The list id full."); break; } puts("Enter next movie title (empty line to quit):"); } /* Display List */ if(ListIsEmpty(&movies)) printf("No data entred."); else { printf("Here is the movie list :\n"); Traverse(&movies,showmovies); } printf("Your entered %d movies.\n",ListItemCount(&movies)); /* clean list */ EmptyTheList(&movies); printf("Bye!\n"); return 0; }
/*释放链表所有节点*/ int FreeList(List *ListBuf) { List *Node = ListBuf, *TmpNode = NULL; if(ListIsEmpty(ListBuf)) return -1; while(Node != NULL) { TmpNode = Node->next; free(Node); Node = TmpNode; } ListBuf = NULL; }
//显示列表内容 void show(List * plist) { if (ListIsEmpty(plist)) { puts("No data!"); } else { printf("Here is the car's information:\n"); Traverse(plist, showList); } printf("There is %d car in your list.\n", ListItemCount(plist)); }
void FreeList( listADT list) { nodoCDT * auxi; while ( ! ListIsEmpty ( list) ) { auxi = list->nodos; list->nodos = auxi->tail; free( auxi ); } free(list); }
int main(int argc, char ** argv) { List movies; Item temp; /* 初始化 */ InitializeList(&movies); if (ListIsFull(&movies)){ fprintf(stderr, "No memory available! Bye!\n"); exit(EXIT_FAILURE); } /* 收集并存储 */ fputs("Enter first movie title: \n", stdout); while (fgets(temp.title, TSIZE, stdin) != NULL && temp.title[0] != '\n'){ reject_ch(temp.title, '\n'); fputs("Enter your rating<0-10>: \n", stdout); scanf_s("%d", &temp.rating); while (getchar() != '\n'){ continue; } if (false == AddItem(&temp, &movies)){ fprintf(stderr, "Problem allocating memory\n"); exit(EXIT_FAILURE); } if (ListIsFull(&movies)){ fputs("The list is now full.\n", stdout); break; } fputs("Enter next movie title(empty line to stop): \n", stdout); } /* 显示 */ if (ListIsEmpty(&movies)) printf("No data entered."); else{ printf("Here is the movie list: \n"); Traverse(&movies, showMovies); } printf("You entered %d movies.\n", ListItemCount(&movies)); /* 清除 */ EmptyTheList(&movies); printf("Bye!\n"); _CrtDumpMemoryLeaks(); return EXIT_SUCCESS; }
void DoubleLinkedList::addToTail(QString str){ node *tmp = new node; if(ListIsEmpty()) { head=tmp; tail=tmp; }else{ tail->next=tmp; tmp->prev=tail; tail = tmp; } tmp->next=NULL; tmp->data=str; }
void showResults( List* resultsList, Item* resultsLevel, HWND hContsLBox ) { if ( ListIsEmpty( resultsList ) ) MessageBox( NULL, TEXT( "No results - showResults()" ), TEXT( "Dir Glance" ), MB_ICONERROR ); else { // Display founded entries Traverse( resultsList, showItem, hContsLBox ); // Display totals SendMessage( hContsLBox, LB_ADDSTRING, 0, ( LPARAM )TEXT( " =================== ================ ========== ================== ================================" ) ); showItem( resultsLevel, hContsLBox ); } }
int main() { List movies; Item temp; InitializeList(&movies); if(ListIsFull(&movies)) { fprintf(stderr,"no memory available!\nBye!\n"); exit(1); } puts("enter first movie title:"); while(gets(temp.title) != NULL && temp.title[0] != '\0') { puts("enter your rating:"); scanf("%d",&temp.rating); while(getchar() != '\n') continue; if(AddItem(temp,&movies) == false) { fprintf(stderr,"Problem allocating memory\n"); break; } if(ListIsFull(&movies)) { puts("the list is full"); break; } puts("enter next movie title:"); } if(ListIsEmpty(&movies)) { printf("no data entered."); } else { printf("here is the movie list:\n"); Traverse(&movies,ShowMovies); } printf("you entered %d movies.\n",ListItemCount(&movies)); EmptyTheList(&movies); printf("Bye!\n"); return 0; }
int main(void) { List movies; Item temp; /*初始化*/ InitializeList(&movies); if(ListIsFull(&movies)) { fprintf(stderr,"no memory available!\n"); exit(1); } /*收集并存储*/ puts("enter first movie title"); while(fgets(temp.title,TSIZE,stdin) != NULL && temp.title[0] != '\n') { puts("enter your rating <0-10>:"); scanf("%d",&temp.rating); while(getchar() != '\n') continue; if(AddItem(temp,&movies) == false) { fprintf(stderr,"problem allocating memory\n"); break; } if(ListIsFull(&movies)) { puts("the list is now full."); break; } puts("enter next movie title(empty line to stop)"); } if( ListIsEmpty(&movies) ) printf("no data entered."); else { printf("here is the movie list:\n"); Traverse(&movies,showmovies); } printf("your entered %d movies.\n",ListItemCount(&movies) ); /*清除*/ EmptyTheList(&movies); printf("bye!\n"); return 0; }
/*从链表头删除一个元素*/ int DeleteFromHead(List *ListBuf) { List *Node = NULL; if(ListIsEmpty(ListBuf)) return -1; if(ListBuf->next == NULL) return -1; if(ListBuf->next->next == NULL) { free(ListBuf->next); ListBuf->next = NULL; } else { Node = ListBuf->next; ListBuf->next = ListBuf->next->next; free(Node); } return 0; }
/* get last item from list */ Item GetLastItem(List *ptrList) { if(!ptrList) { Item tmp={"No Item"}; NO_LIST; return tmp; } if(ListIsEmpty(ptrList)) { Item tmp={"No Item"}; LIST_IS_EMPTY; return tmp; } return ptrList->end->data; }
/*删除指定节点*/ void DeListNode(List *plist, PNode pnode) { if(ListIsEmpty(plist) == 1 || pnode == NULL) { return; } PNode ptmp = plist->front; unsigned int nIndex = 0; for (; ptmp; ptmp = ptmp->next, nIndex++) { if (ptmp == pnode) { DeListIndex(plist, nIndex); break; } } return; }
/*This Func Del an Existing User By Key*/ Item delRecByKey(List *ptrList,Item *ptritem) { Node *tmpitem,*deleteNode; Item tmp={"No Item"}; if(!ptrList) { NO_LIST; return tmp; } if(ListIsEmpty(ptrList)) { Item tmp={"No Item"}; LIST_IS_EMPTY; return tmp; } tmpitem=ptrList->start; if(COMPARE(*ptritem,tmpitem->data)) {/*in case that the 1st is to delete*/ tmp=removeFirst(ptrList); return tmp; } while(tmpitem->next) { if(COMPARE(tmpitem->next->data,*ptritem)) { if (tmpitem->next == ptrList->end) ptrList->end = tmpitem; tmp=tmpitem->next->data; deleteNode=tmpitem->next; tmpitem->next=tmpitem->next->next; free(deleteNode); ptrList->num--; return tmp; } tmpitem=tmpitem->next; } return tmp; }
int main(void){ List movies; Item temp; //初始化 InitializeList(&movies); if(ListIsFull(&movies)){ fprintf(stderr,"No memory available!"); exit(1); } //收集并储存 puts("Enter first movie title:"); while(gets(temp.title ) != NULL && temp.title [0] != '\0' ){ puts("Enter your rating <0-10>:"); scanf("%d",&temp.rating ); while(getchar()!='\n'){ continue; } if(AddItem(temp,&movies)==false){ fprintf(stderr,"Problem allocating memory\n"); break; } if(ListIsFull(&movies)){ puts("The list is full."); break; } puts("Enter next movie title(empty line to stop))"); } //显示 if(ListIsEmpty(&movies)){ printf("No data entered."); } else{ printf("Here is the movie list:\n"); Traverse(&movies,showmovies); } //清除 EmptyTheList(&movies); printf("Bye!"); return 0; }