int main(){ myNode* head=NULL; myNode* newNode = malloc(sizeof(myNode*)); newNode->data = 44; newNode->next = NULL; add(&head,82); add(&head,75); add(&head,65); add(&head,57); add(&head,48); add(&head,44); add(&head,42); add(&head,40); print_list(head); SortedInsert(&head,newNode); print_list(head); return 0; }
void Sort(STACKPTR s) { if(!isEmpty(s)) { int temp = pop(s); Sort(s); SortedInsert(s, temp); } }
int main() { struct node *head = NULL; struct node *ten = (struct node *)malloc(sizeof(struct node)); ten->data = 10; SortedInsert(&head, ten); struct node *thirteen = (struct node *)malloc(sizeof(struct node)); thirteen->data = 13; SortedInsert(&head, thirteen); struct node *eleven = (struct node *)malloc(sizeof(struct node)); eleven->data = 11; SortedInsert(&head, eleven); struct node *one = (struct node *)malloc(sizeof(struct node)); one->data = 1; SortedInsert(&head, one); struct node *two = (struct node *)malloc(sizeof(struct node)); two->data = 2; SortedInsert(&head, two); struct node *one_hundred = (struct node *)malloc(sizeof(struct node)); one_hundred->data = 100; SortedInsert(&head, one_hundred); struct node *two_two = (struct node *)malloc(sizeof(struct node)); two_two->data = 2; SortedInsert(&head, two_two); PrintList(head); struct node *head2 = NULL; struct node *head2_ten = (struct node *)malloc(sizeof(struct node)); head2_ten->data = 10; SortedInsert2(&head2, head2_ten); struct node *head2_thirteen = (struct node *)malloc(sizeof(struct node)); head2_thirteen->data = 13; SortedInsert2(&head2, head2_thirteen); struct node *head2_eleven = (struct node *)malloc(sizeof(struct node)); head2_eleven->data = 11; SortedInsert2(&head2, head2_eleven); struct node *head2_one = (struct node *)malloc(sizeof(struct node)); head2_one->data = 1; SortedInsert2(&head2, head2_one); struct node *head2_two = (struct node *)malloc(sizeof(struct node)); head2_two->data = 2; SortedInsert2(&head2, head2_two); struct node *head2_one_hundred = (struct node *)malloc(sizeof(struct node)); head2_one_hundred->data = 100; SortedInsert2(&head2, head2_one_hundred); struct node *head2_two_two = (struct node *)malloc(sizeof(struct node)); head2_two_two->data = 2; SortedInsert2(&head2, head2_two_two); PrintList(head2); return 0; }
// 7 — InsertSort() // Given a list, change it to be in sorted order (using SortedInsert()). void InsertSort(struct node** headRef) { struct node* result = NULL; // build the answer here struct node* current = *headRef; // iterate over the original list struct node* next; while (current!=NULL) { next = current->next; // tricky - note the next pointer before we change it SortedInsert(&result, current); current = next; } *headRef = result; }
void SortedInsert(STACKPTR s, int item) { if(isEmpty(s) || item > top(s)) { push(s, item); return; } int temp = pop(s); SortedInsert(s, item); push(s, temp); }
/*-----------InsertSort()-------------*/ void InsertSort(struct node** headRef){ struct node* sortedList = NULL; struct node *head = *headRef; while(head != NULL) { struct node* insert = (struct node*)malloc(sizeof(struct node)); insert->data = head->data; insert->next = NULL; SortedInsert(&sortedList,insert); head = head->next; } (*headRef) = sortedList; }
/* * Begin code I did not write. * This code is partly derived from http://merlot.usc.edu/cs570-f11/homeworks/prog-faq/#cpp_process * If the source code requires you to include copyright, put copyright here. */ void Process(istream& in) { int count = 0; char* buf = new char[MAXBUFSIZE]; in.getline(buf,MAXBUFSIZE); if(strlen(buf)==0) { fprintf(stderr,"Input/File is empty\n"); exit(-1); } My570List *list=new My570List(); ///< CREATING LIST to push all the struct elems while (!in.eof()) { count++; ///< Count all records struct record *temp_str; ///< Create record pointer temp_str = isRecordOk(buf,count); ///< Validate BUFFER and get back record //(void)list->Append((void*)temp_str); ///< Push records into the LIST unsorted SortedInsert(list,temp_str); ///< push records into the list in a sorted manner #ifdef DEBUG1 fprintf(stdout,"%s %d %0.2f %s\n",temp_str->sign,temp_str->timestamp,temp_str->amount,temp_str->description); ///< display record #endif #ifdef DEBUG1 fprintf(stdout,"%s\n",buf); ///< PRINT THE RAW BUFFER #endif in.getline(buf,MAXBUFSIZE); ///< GET THE NEXT LINE } #ifdef DEBUG0 fprintf(stdout,"*******************SORTED DISPLAY*******************\n"); displayRecords(list); ///< DISPLAY THE RECORDS from list #endif tableDisplay(list); ///< DISPLAY FORMATTED OUTPUT!!!! THIS WILL GIVE ME MARKS !!!! :--/ @#@!#! #ifdef DEBUG1 fprintf(stdout,"EOF REACHED\n"); #endif #ifdef DEBUG0 fprintf(stdout,"total number of records processed = %d\n",count); #endif }
UINT BuildStrList(LPSTR lpList, LPSTR *alpList) { LPSTR lp; TCHAR cLastChar = 1; UINT uCount = 0; lp = lpList; while ((cLastChar) || (*lp)) { if ((*lp == 0) && (lp != lpList)) { uCount++; } if ((lpList == lp) || (cLastChar == 0)) { if ((*lp) && (alpList)) { SortedInsert(lp, alpList); } } cLastChar = *lp++; } return uCount; }
void mogedImportClipsDlg::OnAddClips( wxCommandEvent& event ) { (void)event; // TODO: Implement OnAddClips wxFileDialog dlg(this, _("Open AMC Files"), wxString(m_ctx->GetBaseFolder(), wxConvUTF8), _(""), _("Acclaim clips (*.amc)|*.amc"), (wxOPEN|wxFILE_MUST_EXIST|wxMULTIPLE)); if(dlg.ShowModal() == wxID_OK) { wxArrayString files ; dlg.GetPaths(files); for(int i = 0; i < (int)files.Count(); ++i) { SortedInsert( m_clip_list, files[i] ); } } }
int InsertSort (node_t** headRef) /* takes the list pointed by *headRef and sorts it in increasing order returns 0 if success, -1 otherwise */ { node_t* curr = *headRef; node_t* result = NULL; node_t* next = NULL; if (!curr) { fprintf (stderr, "InsertSort : *headRef not initialized\n"); return -1; } while (curr) { next = curr->next; SortedInsert (&result, curr); curr = next; } *headRef = result; return 0; }
int main(void) { //First Linked List struct node *first=NULL; int arr1[]={1,1,3,5,7,9}; int size1=sizeof(arr1)/sizeof(arr1[0]); int j; for(j=0;j<size1;j++) { first=append(first,arr1[j]); } printf("First Linked List contains : \n"); print(first); printf("\n\n"); //Second Linked List struct node *second=NULL; int arr2[]={2,4,5,6,6,7,9}; int size2=sizeof(arr2)/sizeof(arr2[0]); int k; for(k=0;k<size2;k++) { second=append(second,arr2[k]); } printf("\nSecond Linked List contains :\n"); print(second); printf("\n\n"); //Merge Linked lists first and second struct node *merge=NULL; merge=mergeLists(first,second); printf("\nMerged Linked List contains :\n"); print(merge); printf("\n\n"); //Reverse the merged list struct node *reverse=NULL; reverse=reverseList(merge); printf("\nReverse of Merged Linked List contains :\n"); print(reverse); printf("\n\n"); //Inserting into a doubly linked list struct dnode *dlist=NULL; int arr3[]={2,4,5,6,6,7,9}; int size3=sizeof(arr3)/sizeof(arr3[0]); int l; for(l=0;l<size3;l++) { dlist=SortedInsert(dlist,arr3[l]); } printf("\nDoubly Linked List contains :\n"); printDoublyList(dlist); printf("\n\n"); //Reversing the doubly linked list struct dnode *reverseDoubleList=NULL; reverseDoubleList=reverseDoublyList(dlist); printf("\nReverse of Doubly Linked List contains :\n"); printDoublyList(reverseDoubleList); printf("\n\n"); return 0; }