ListNode *insertionSortList(ListNode *head) {
		ListNode phead(0);//set a node always poins the head
		phead.next=head;
		if(!head) return NULL;
		ListNode *p=head->next, *tail=head;
		while(p) {
			if(tail->val <= p->val) { //p is larger than the largestrsorted list element
				tail=p;
				p=p->next;
				break;
			}
			ListNode *q=phead.next, *preq=NULL;
			while(q!=p) { //there must can find the post q > p
				if(q->val > p->val)
					break;
				else preq=q;
				q=q->next;
			}
			tail->next=p->next;
			p->next=q;
			if(!preq) preq=&phead; //p is smallest in the sorted list
			preq->next=p;
			p=tail->next;
		}
		return phead.next;
	}
Exemple #2
0
// put: put out line with proper spacing and indenting
void put(Char * buf)
{
    int i, nextra;
    Char ch, ulflag, boflag;

    // Print out the header
    if (lineno == 0 || lineno > bottom)
	phead();

    // Centering is done by setting a temporary indent value
    if (ceval) {
	i= Strlen(buf);		// XXX Why can't I put this below?
				// Because the f'n call tromps on temps
	tival = (rmval + tival - i) / 2;
	if (tival < 0)
	    tival = 0;
	ceval--;
    }

    // Print out the temporary indent spaces
    // and reset back to the usual indent value
    for (i = 0; i < tival; i++)
	putchar(' ');
    tival = inval;

    // Now process each character one at a time
    while (1) {

	// Get the actual ASCII value and the underline flag
	ch = *buf & ASCIIMASK;
	ulflag = *buf & ULMASK;
	boflag = *buf & BOLDMASK;
	if (ch == '\0')
	    break;

	// If this is a space, print out any extra spaces
	// by getting the extra space count in the upper half
	// of the word
	if (ch == ' ') {
	    nextra = *buf >> 9;
	    for (i = 0; i != nextra; i++)
		putchar(' ');
	}
	// Underline the character if the flag is set
	// Do the same for bold characters
	if (ulflag) {
	    putchar('_');
	    putchar(8);		// 8 is ASCII backspace
	}
	if (boflag) {
	    putchar(ch);
	    putchar(8);		// 8 is ASCII backspace
	}
	putchar(ch);
	buf++;
    }