hey everyone I am creating a program that creates a linked list and the program has to have a menu to let the user 1.add to the beginning of the list. 2. add to the end of the list. 3. delete a particular character from the list 4. prints the list and 5. exits. I have the whole structure of the program done but I cannot get the adding to the beginning or the end of the list part down. No matter what you do if there is already an "A" or a "C" in the list, if you add "B" it will put the character right in between the "A" and the "C". Please help!

/* display program instructions to user */
void instructions( void )
{
printf( "Enter your choice:\n"
" 1 to insert an element into the beginning of the list.\n"
" 2 to insert an element into the end of the list.\n"
" 3 to delete an element from the list.\n"
" 4 to print the list.\n"
" 5 to quit.\n");
}
/***************************************************************************/void insertbegin( ListNodePtr *sPtr, char value ) /* Insert a new value into the list in sorted order */
{
ListNodePtr newPtr; /*pointer to new node*/
ListNodePtr previousPtr; /*pointer to previous node in list*/
ListNodePtr currentPtr; /*pointer to current node in list*/

Wow. You're not quite getting the idea of a linked list, though.
The idea is that you have pointers to every node. Not some "normal" node placed on the stack they you're reassigning.
I don't get the idea with insertbegin and insertend if you're just going to sort the list anyway.

First error:

Code:

previousPtr - *sPtr;

In delete.
Illegal since you're dereferencing sPtr. Even so, you're not reassigning it, so it's useless. Even if not illegal, it will probably not do what you intend.
previousPtr is not even a pointer, so that's even more weirdness. Perhaps you intended to do =?

Code:

if ( value == ( *sPtr )->data ) {

In delete, you are dereferencing the pointer AND using ->. sPtr is not **, so that's kinda illegal. Stupid C allows this kind of crap

12-11-2007

lostmyshadow

so what does that mean for the code, the delete function works fine its just adding to the beginning and the end im having trouble with

12-11-2007

tabstop

Quote:

Code:

void insertbegin( ListNodePtr *sPtr, char value ) /* Insert a new value into the list in sorted order */

<sings> One of these things is not like the others... </sings>
Although I do congratulate you on having code that matches your comments.
If you want to insert at the beginning of the list, why walk the list to find the correct place to put it? Just put it right there.
Same for end; just go until you can't go any more, then put a new entry in the list.

12-11-2007

iMalc

Given that inserting an item into a list in order is always more work than simply placing it at either end, you obviously copied the insertion routine from elsewhere and don't understand it.
Try to understand how the insert routine works. It has everything you need for inserting at either end.

12-12-2007

Elysia

Quote:

Originally Posted by lostmyshadow

so what does that mean for the code, the delete function works fine its just adding to the beginning and the end im having trouble with

Your code is flawed. Most likely you're just no giving the right scenario to execute that flawed code, but when it does, be sure it will crash!
* and -> both DEREFERENCES the pointer (eg accesses the value stored at the address), so in essence you're doing in twice. And what happens if you try to dereference a value that is not a pointer? Exactly. Crash!

//----------------------------------------------------------
// Adding a node to the beginning of a list.
//----------------------------------------------------------
void add_begin(nodeptr *head, // head of the list
course_t item) // data of the list
{
nodeptr newptr; // pointer for a new node

newptr = getnode(); // get a new node
newptr->item = item; // store data into new node
newptr->next = *head; // newnode will point to whatever head was pointing
*head = newptr; // new node will be the head
} // end add_begin

//----------------------------------------------------------
// Adding a node to the end of a list:
//----------------------------------------------------------
void add_end(nodeptr *head, // head of the list
course_t item ) // data of the list
{
nodeptr newptr, ptr;

newptr = getnode(); // Get a new node
newptr->item = item; // Store data into new node
newptr->next = NULL; // Set next of new node to NULL

if (*head == NULL)
{ // Linked list was empty
*head = newptr; // add to beginning
}
else
{ // Linked List was not empty. Find last node.
ptr = *head; // point to beginning of list
while(ptr->next != NULL) // while it is not the last node,
{ ptr = ptr->next; } // pass to the next node
ptr->next = newptr; // old last node is followed by new node
}

} // end add_end

//----------------------------------------------------------
// Delete a node with a given data
// Returns 1 if a deletion is performed, 0 if no deletion is performed.
//----------------------------------------------------------
int del_data(nodeptr *head, // head of the list
char course_code[]) // course code to be deleted
{
nodeptr ptr, // Ptr to current node
prevptr = NULL; // Ptr to previous node
int ret_val = 0; // Value to be returned.