i can enter in the data. But the do while statment is not working. I'm still not able to put the data into the array. Any ideas. what I do is enter in the data. And the Index stays the same ever changes.
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");
printf ("Ready to start program? Y or N\n");
scanf ("\n%c", &More);
while (((More == 'Y') || (More == 'y')) && (Index < Max))
{
printf ("\nEnter an integer: ");
scanf ("\n%d", &ArrayInput[Index]);
if (ArrayInput[Index] <= 0)
{
printf ("\nInvalid entry. Try again:\n\n");
scanf ("\n%d", &ArrayInput[Index]);
} /* end of if statement */
else
{
Index++;
printf ("\nDo another? Y or N?\n");
scanf ("\n%c", &More);
} /* end of else statement */
} /* end of while statement */
printf ("%d", ArrayInput[Index]);
Count = Index;
for (Pass = 1; Pass < Count; Pass++) {
for (Index = 0; Index, Count - 1; Index++) {
if (ArrayInput[Index] > ArrayInput[Index + 1]) {
hold = ArrayInput[Index];
ArrayInput[Index] = ArrayInput[Index +1];
ArrayInput[Index +1] = hold;
} /* 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 */
/* Index++;
printf ("\nDo another? Y or N?\n");
scanf ("\n%c", &More);
} /* end of while statement */
tempnode = start;
printf ("Your data displayed in ascending order: \n\n");
while (tempnode) {
printf ("\n%d\n",tempnode->data);
tempnode = tempnode->nextPtr;
} /* end of while statement */
system ("PAUSE");
getchar ();
getchar ();
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 */