for loop with sort issues
i can get teh data but an having issues with my sort any ideas please
Code:
/************************************************************
FILE NAME: Lab3
DESCRIPTION: Calculate loan amortization
DATE: Oct 8, 2006
AUTHOR: Cheryl B Jensen
ASSIGNMENT: Lab 2
FUNCTIONS: main, Linked_List
/***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
typedef struct listNode /* struct definition for linked list */
{
int data;
struct listNode *priorPtr;
struct listNode *nextPtr;
}ListNode;
struct listNode *start = NULL; /* initialize first node */
struct listNode *last = NULL; /* initialize last node */
/*int Linked_List (int, *ptr); /*prototype for function */
/*******************************************************************
FUNCTION HEADER: Main
FUNCTION: Main
DESCRIPTION: Ask for user input from keyboard up to 50 integers,
store the data in an array, sort the array, create a linked
list, and display the data from the linked list
INPUT:
PARAMETERS: input up to 50 integers (Max) - use Count to determine
actual size of array
KEYBOARD: Read user's input listed above
FILE: No input file for this program
OUTPUT:
RETURN VALUE: Display linked list of the sorted array in ascending order
PARAMETERS: 1 to Count (size of array)
DISPLAY: display contents of array within the linked list
FILE: No DATA WRITTEN TO FILE
CALLS TO: Linked_List
*******************************************************************/
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define Max 50
int main (void)
{
/* max size of array input; array will end when it reaches
max or when user quits by hitting enter (null entry) */
int ArrayInput[Max]; /* declared, defined, & initialized array */
int Count = 0; /* actual size of array, defined by user or Max */
int InputNum; /* int entered by user */
int Index = 0; /* location of current array cell in first for statement*/
int Index2 = 0; /* location of current array cell in 2nd for statement */
int hold = 0; /* temporary holding location for swap */
int Pass = 0; /* loop counter */
char More; /* to continue or end loop by user input of null entry */
struct listNode *start = NULL; /* initialize start pointer structure */
struct listNode *last = NULL; /* initialize last pointer structure */
struct listNode *data = NULL; /* initialize data pointer structure */
struct listNode *tempnode = NULL; /* initialize temporary pointer */
printf ("Welcome to your Sort program.\n");
printf ("This program will sort up to 50 integers in ascending order.\n");
do {
printf("\nEnter an integer: ", stdout);
scanf ("\n%d", &ArrayInput[Index]);
while (ArrayInput[Index] <= 0)
{
printf ("Invalid entry. Try again:\n\n");
scanf ("\n%d", &ArrayInput[Index]);
} /* end of if statement */
printf ("Do another? Y or N?\n");
scanf ("\n%c", &More);
} /* end of the body of do..while statement */
while (((More == 'Y') || (More == 'y')) && (Index < Max));
Count = Index;
printf ("%d\t", ArrayInput[Index]);
for (Pass = 1; Pass < Count; Pass++) {
printf("in doloop");
for (Index2 = 1; Index2 , Count - 1; Index2++) {
if (ArrayInput[Index2] > ArrayInput[Index2 + 1]) {
hold = ArrayInput[Index2];
ArrayInput[Index2] = ArrayInput[Index2 +1];
ArrayInput[Index2 +1] = hold;
printf ("%d\t", ArrayInput[Index2]);
} /* end of if */
} /* end of inner for */
data = (struct ListNode *) malloc(sizeof(ListNode));
if (!data)
return -1;
data->data= ArrayInput[Index];
Linked_List (data, &start, &last);
} /* end of outer for */
tempnode = start;
printf ("Your data displayed in ascending order:\n\n");
while (tempnode) {
printf ("&d\n",tempnode->data);
tempnode = tempnode->nextPtr;
} /* end of while statement */
system ("PAUSE");
return 0;
} /* end of main function */
/*****************************************************************************
FUNCTION HEADER: Linked_List
FUNCTION: Linked_Array
DESCRIPTION: Create a linked list from the sorted array
INPUT:
PARAMETERS: sorted array
KEYBOARD: no keyboard input
FILE: No input file for this program
OUTPUT:
RETURN VALUE: Return linked list
PARAMETERS: size of actual sorted array
DISPLAY: no display yet
FILE: No DATA WRITTEN TO FILE
CALLS TO: no functions; called by main
*******************************************************************/
int Linked_List ( struct listNode *newnode,
struct listNode **start,
struct listNode **last
)
{
struct listNode *oldptr, *ptr;
if (*last == NULL)
{
newnode->nextPtr = NULL;
newnode->priorPtr = NULL;
*last = newnode;
*start = newnode;
return;
}
ptr = *start;
oldptr = NULL;
while (ptr)
{
if(ptr->data < newnode->data) {
oldptr = ptr;
ptr = ptr->nextPtr;
}
else
{
if (ptr->priorPtr) {
ptr->priorPtr->nextPtr = newnode;
newnode->nextPtr = ptr;
newnode->priorPtr = ptr->priorPtr;
ptr->priorPtr = newnode;
return;
}
newnode->nextPtr = ptr;
newnode->priorPtr = NULL;
ptr->priorPtr = newnode;
*start = newnode;
return;
} /* end of else statement */
} /* end of while statement */
oldptr->nextPtr = newnode;
newnode->nextPtr = NULL;
newnode->priorPtr = oldptr;
*last = newnode;
} /* end of function */