int main() { struct NODE *head = malloc(sizeof(struct NODE)); // 머리 노드 생성 // 머리 노드는 데이터를 저장하지 않음 head->next = NULL; addFirst(head, 10); // 머리 노드 뒤에 새 노드 추가 addFirst(head, 20); // 머리 노드 뒤에 새 노드 추가 addFirst(head, 30); // 머리 노드 뒤에 새 노드 추가 struct NODE *curr = head->next; // 연결 리스트 순회용 포인터에 첫 번째 노드의 주소 저장 while (curr != NULL) // 포인터가 NULL이 아닐 때 계속 반복 { printf("%d\n", curr->data); // 현재 노드의 데이터 출력 curr = curr->next; // 포인터에 다음 노드의 주소 저장 } curr = head->next; // 연결 리스트 순회용 포인터에 첫 번째 노드의 주소 저장 while (curr != NULL) // 포인터가 NULL이 아닐 때 계속 반복 { struct NODE *next = curr->next; // 현재 노드의 다음 노드 주소를 임시로 저장 free(curr); // 현재 노드 메모리 해제 curr = next; // 포인터에 다음 노드의 주소 저장 } free(head); // 머리 노드 메모리 해제 return 0; }
void main(int argc, char** argv) { addFirst(5); addFirst(2); addFirst(3); addLast(4); displayFrom(head); }
int main() { Sant* santi=(Sant*)malloc(sizeof(Sant)); initializeSent(santi); //santi->head=NULL; // santi->tail=NULL; char s[30]; int x; out=fopen("out.txt","w"); in=fopen("in.txt","r"); int c; c=fscanf(in,"%s %d",s,&x); while(c>0) { //printf("%s %d\n",s,x); if(!strcmp(s,"AL"))addLast(santi,x); if(!strcmp(s,"AF"))addFirst(santi,x); if(!strcmp(s,"DE"))delete_element(santi,x); if(!strcmp(s,"PRINT_F"))print_first(santi,x); if(!strcmp(s,"PRINT_L"))print_last(santi,x); if(!strcmp(s,"PRINT_ALL"))printlist(santi); if(!strcmp(s,"DOOM_THE_LIST"))doom_the_list(santi); if(!strcmp(s,"DF"))delete_first(santi); if(!strcmp(s,"DL"))delete_last(santi); strcpy(s,""); c=fscanf(in,"%s %d",s,&x); } return 0; }
void insrt(unsigned int p,int x) { if (p==0) addFirst(x); else if (p>=lngth) addLast(x); else { int i; node *n=(node*)malloc(sizeof(node)); node *t; if (p<=lngth/2) for (i=0, t=f;i+1<p;i++)///p is unsigned=> can't use p-1 t=t->next; else for (i=lngth,t=l;i>p;i--) t=t->prev; n->val=x; n->prev=t; n->next=t->next; (t->next)->prev=n; t->next=n; } lngth++; }
/* add sorted */ t_list * addSorted(t_list *list, void * data , int (*compareFunc)(void *a, void *b)) { t_list *current_element; void *current_data; int counter; /* preconditions */ if (list == NULL) return addFirst(list, data); counter = 0; current_element = list; while(current_element != NULL) { /* get the current interval informations */ current_data = LDATA(current_element); assert(current_data != NULL); if (compareFunc(current_data, data) >= 0) { list = addElement(list, data, counter); return list; } /* retrieve the next element */ current_element = LNEXT(current_element); /* update the value of counter */ counter++; } return addElement(list, data, -1); }
bool Sequence<TYPE>::add(const TYPE &value, int pos) { Node *aux = &list; Node *aux7 = &list; while (aux->next != NULL) { if(pos < 0) return addFirst(value); else { if(pos == 0) { if(aux->next != NULL) { Node *aux5 = new Node; aux5->data=value; aux5->next=aux->next; aux5->prev=aux->prev; aux->next = aux5; return true; } else break; } } pos--; aux = aux->next; } if(aux7->next != NULL) addLast(value); return false; }
int append(linkedlist* ll, node* add){ if(ll->size == 0) return addFirst(ll, add); ll->tail->next = add; ll->tail = add; ll->size += 1; return 1; }
ossimRefPtr<ossimCacheTileSource> ossimSingleImageChain::addCache() { ossimRefPtr<ossimCacheTileSource> cache = new ossimCacheTileSource(); // Add to the end of the chain. addFirst(cache.get()); return cache; }
int append(linkedlist* ll, void* ptr){ if(ll->size == 0) return addFirst(ll, ptr); node* add = malloc(sizeof(node)); add->ptr = ptr; ll->tail->next = add; ll->tail = add; ll->size += 1; return 1; }
void ossimSingleImageChain::addBandSelector() { if (!m_bandSelector) { m_bandSelector = new ossimBandSelector(); // Add to the end of the chain. addFirst(m_bandSelector.get()); } }
void ossimSingleImageChain::addSharpen() { if ( !m_sharpen ) { m_sharpen = new ossimImageSharpenFilter(); // Add to the end of the chain. addFirst( m_sharpen.get() ); } }
void ossimSingleImageChain::addBrightnessContrast() { if ( !m_brightnessContrast ) { m_brightnessContrast = new ossimBrightnessContrastSource(); // Add to the end of the chain. addFirst( m_brightnessContrast.get() ); } }
void ossimSingleImageChain::addResampler() { if ( !m_resampler ) { m_resampler = new ossimImageRenderer(); // Add to the end of the chain. addFirst(m_resampler.get()); } }
int main() { sent = (list*)malloc(sizeof(list)); sent->head = NULL; sent->tail = NULL; sent->len = 0; FILE* fin= fopen("input.txt", "r"); char caz[20]; int n; while(fscanf(fin, "%s", caz)>0) { if(strcmp(caz, "AF")==0) { fscanf(fin, "%d", &n); addFirst(n); } else if(strcmp(caz, "AL")==0) { fscanf(fin, "%d", &n); addLast(n); } else if(strcmp(caz, "DF")==0) deleteFirst(); else if(strcmp(caz, "DL")==0) deleteLast(); else if(strcmp(caz, "DOOM_THE_LIST")==0) doomTheList(); else if(strcmp(caz, "DE")==0) { fscanf(fin, "%d", &n); deleteX(n); } if(strcmp(caz, "PRINT_ALL")==0) printList(); else if(strcmp(caz, "PRINT_F")==0) { fscanf(fin, "%d", &n); printFirstX(n); } else if(strcmp(caz, "PRINT_L")==0) { fscanf(fin, "%d", &n); printLastX(n); } } fclose(fin); return 0; }
struct node* digitList( int n ) { struct node* root = (struct node*) malloc(sizeof(struct node)); root-> x = -1; root-> next = 0; while ( n ) { root = addFirst( n%10, root ); // builds the list from the bottom up n /= 10; } return root; }
inline bool DLFifoListImpl<P,T,U>::seizeFirst(Ptr<T> & p) { if (likely(thePool.seize(p))) { addFirst(p); return true; } p.p = NULL; return false; }
//implements the radix sorting algo //O(n*k) n being the number of items to be sorted and k being the number of significant digits of the largest number void radixSort(DEQUE *maindeque, DEQUE **radixarray, int max){ int sigdigits, number, i, j, m, radixindex, mainitems, arritems, power = 1; mainitems = numItems(maindeque); sigdigits = ceil(log(max + 1)/log(RADIXSIZE)); for(i = 0; i < sigdigits; i++){ for(j = 0; j< mainitems; j++){ number = removeLast(maindeque); radixindex = ((number)/(power)) % RADIXSIZE; addFirst(radixarray[radixindex], number); } power *= RADIXSIZE; for(j = 0; j < RADIXSIZE; j++){ arritems = numItems(radixarray[j]); for(m = 0; m < arritems; m++){ number = removeLast(radixarray[j]); addFirst(maindeque, number); } } } }
void ossimSingleImageChain::addHistogramRemapper() { if (!m_histogramRemapper) { m_histogramRemapper = new ossimHistogramRemapper(); m_histogramRemapper->setEnableFlag(false); // Add to the end of the chain. addFirst(m_histogramRemapper.get()); } }
void addLast(Node* t, Node** head) { Node* cur=*head; if(cur==NULL) addFirst(t, head); else { while(cur->next!=NULL) cur=cur->next; cur->next=t; t->next=NULL; } }
static void buildMaze(int y, int x) { int numOffsets, offset, offsets[4]; while (1) { numOffsets = 0; maze[y][x].visited = true; if (y > 0 && !maze[y - 1][x].visited) offsets[numOffsets ++] = -width; if (y < height - 1 && !maze[y + 1][x].visited) offsets[numOffsets ++] = width; if (x > 0 && !maze[y][x - 1].visited) offsets[numOffsets ++] = -1; if (x < width - 1 && !maze[y][x + 1].visited) offsets[numOffsets ++] = 1; if (numOffsets > 0) { offset = offsets[rand() % numOffsets]; addFirst(dp, offset(x, y)); if (offset == -width) { maze[y - 1][x].bottom = false; buildMaze(y - 1, x); } else if (offset == width) { maze[y][x].bottom = false; buildMaze(y + 1, x); } else if (offset == -1) { maze[y][x - 1].right = false; buildMaze(y, x - 1); } else if (offset == 1) { maze[y][x].right = false; buildMaze(y, x + 1); } else abort(); } else if (numItems(dp) > 0) { offset = removeFirst(dp); x = xcoord(offset); y = ycoord(offset); } else break; } maze[height - 1][width - 1].right = false; }
void ossimSingleImageChain::addBandSelector(const ossimSrcRecord& src) { if (!m_bandSelector) { m_bandSelector = new ossimBandSelector(); // Add to the end of the chain. addFirst(m_bandSelector.get()); } if ( src.getBands().size() ) { m_bandSelector->setOutputBandList( src.getBands() ); } }
int main(int argc, const char * argv[]) { //static array first student temp[Num] = {{7,"able"}, {1,"band"}, {4,"cat"}, {3,"dog"}, {2,"ele"}, {5, "find"}, {6, "good"}, {0, "hello"}}; student *data[Num]; int i = 0; student *cur; //copy data into dynamic array //set up for tests for(; i < Num; i ++) { cur = (student *) malloc(sizeof(student)); //each student object takes a piece of mem cur->id = temp[i].id; cur->name = (char *) malloc(Size * sizeof(char)); strcpy(cur->name, temp[i].name); data[i] = cur; } List slist; init(&slist, compStudent, printStudent, freeStudent); for(i = 0; i < Num; i ++ ) { slist = addFirst(slist, (void *) data[i]); } printf("------Original List:------\n"); // print out the list printList(&slist); //sort slist = sort(slist); printf("------After sorted:------\n"); //print again printList(&slist); removeNode(&slist, (void *)&temp[4]); printf("------After removed student with id = 2:------\n"); printList(&slist); printf("------Now start to free all memory!\n"); //free the whole list freeList(&slist); return 0; }
int main() { srand(time(NULL)); unsigned char arrCharDevice[MAX_DEVICE][DEVICE_SIZE]; unsigned char chDevice[] = {0xb,0x0,'F','S','o','f','\0',0x0,0x1,0x0,0x0}; int i,j; device **ppd; device *tmp; linkedlist ll; initializeLL(&ll); //initialize linkedlist initArrayDeviceData(arrCharDevice, MAX_DEVICE, DEVICE_SIZE); //create data array of device ppd = initArrayDevice(arrCharDevice, MAX_DEVICE, DEVICE_SIZE); //create array of device for(i=0; i<MAX_DEVICE; i++) { printDevice(*(ppd+i)); addFirst(&ll, *(ppd+i)); //insert device into linkedlist } FL; printLL(&ll); tmp = (device*)chDevice; FL;printDevice(tmp); insertElement(&ll, tmp, 5); //insert above device into linkedlist FL;printLL(&ll); rmElement(&ll,4); //remove element from linkedlist FL;printLL(&ll); free(tmp); freeLL(&ll); freeArrayDevice(&ppd, MAX_DEVICE); unsigned short asdf = 0xF0AA; unsigned char *asda = (char*)malloc(sizeof(asdf)); FL; PRINT_HEX(asdf); EL; printf("%X", asdf&0xff); EL; convertToChar(asda, &asdf, 0, sizeof(short)); FL; PRINT_HEX(*asda); SP; PRINT_HEX(*(asda+1)); EL; PRINT_UINT(sizeof(aaa)); EL; return 0; }
/*menambahkan di akhir list*/ void addLast(char amal[], list *L){ if((*L).first == NULL){ /*jika list adalah list kosong*/ addFirst(amal, L); }else{ /*jika list tidak kosong*/ /*mencari elemen terakhir list*/ elemen *prev= (*L).first; while(prev->next != NULL){ /*iterasi*/ prev= prev->next; } addAfter(prev, amal, L); } }
/* Insert a new label. The label must be initialized externally */ int insertLabel(t_translation_infos *infos, t_asm_label *label) { /* preconditions */ if (infos == NULL) return ASM_NOT_INITIALIZED_INFO; if (label == NULL) return ASM_INVALID_LABEL_FOUND; /* update the list of labels */ infos->labels = addFirst(infos->labels, label); /* notify that everything went correctly */ return ASM_OK; }
void ossimSingleImageChain::addScalarRemapper() { if ( !m_scalarRemapper ) { m_scalarRemapper = new ossimScalarRemapper(); if ( m_resamplerCache.valid() ) { // Add to the left of the resampler cache. insertLeft(m_scalarRemapper.get(), m_resamplerCache.get()); } else { // Add to the end of the chain. addFirst(m_scalarRemapper.get()); } } }
int enqueue(list* listPtr,int priority,void* data){ node* nodePtr = calloc(sizeof(node),1); node *temp=calloc(sizeof(node),1); node *temp2=calloc(sizeof(node),1); if(listPtr->length == 0){ return addFirstNode(nodePtr,listPtr,data,priority); }; createNode(nodePtr,data,priority); temp = listPtr->head; if(temp->priority > nodePtr->priority) return addFirst(nodePtr,listPtr,data,priority,temp); while(temp->next!=NULL) temp= temp->next; if(temp->priority < nodePtr->priority) return addInLast(nodePtr,listPtr,data,priority,temp); return addInMiddle(nodePtr,listPtr,data,priority,temp,temp2); };
void ossimSingleImageChain::addGeoPolyCutter() { if(!m_geoPolyCutter.valid()) { m_geoPolyCutter = new ossimGeoPolyCutter(); //--- // ossimGeoPolyCutter requires a view geometry to transform the points. // So set prior to adding to end of chain if valid. If not, user is // responsible for setting. //--- ossimRefPtr<ossimImageGeometry> geom = getImageGeometry(); if ( geom.valid() ) { m_geoPolyCutter->setView( geom.get() ); } addFirst( m_geoPolyCutter.get() ); } }
int main(int argc, char *argv[]) { if (argc<2) return 0; // FILE *f = fopen(argv[1],"r"); // if (f==NULL) return 0; struct List *l; newList(&l); char* x = argv[1]; char* tmp; tmp = strtok(x," "); struct Entry* node; struct Entry* n1 ; newNode(&n1); n1->element = "N1"; struct Entry* n2 ; newNode(&n2); n2->element = "N2"; struct Entry* n3 ; newNode(&n3); n3->element = "N3"; struct Entry* n4 ; newNode(&n4); n4->element = "N4"; int status = -1; struct Entry* e = l->head; char* rm= (void*) 0; while (tmp != (void*)0) { if (tmp[0] == '"') continue; if (status == -1) { rm = (char*) malloc(sizeof (char) * (strlen(tmp)+1)); strcpy(rm,tmp); status =0; tmp = strtok((void*)0," "); continue; } if (strcmp(tmp,"N1")==0) node = n1; if (strcmp(tmp,"N2")==0) node = n2; if (strcmp(tmp,"N3")==0) node = n3; if (strcmp(tmp,"N4")==0) node = n4; if (strcmp(tmp,"H")==0) node = l->head; addFirst(&l,&node); tmp = strtok((void*)0," "); } if (hasLoopNext(l)==0 ){ printf("%s","HAS LOOP"); return 0; } if (hasLoopPrev(l)==0 ){ printf("%s","HAS LOOP"); return 0; } printf("%d ",removeEntry(&l,rm)); struct Entry* n = l->head->next; while (n != (l->head)) { printf("%s ", n->element); printf("%s ", n->previous->element); printf("%s ", n->next->element); n = n->next; } printf(" %d",l->size); return 0; }
T& addFirst (const T& item) { T& slot = addFirst(); slot = item; return slot; }