Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 9th, 2004, 9:50 PM   #1
ahhjun
Newbie
 
Join Date: Oct 2004
Posts: 1
Rep Power: 0 ahhjun is on a distinguished road
Source Code:

#include <stdio.h>
 #include <stdlib.h>
 
 struct listNode {  
    char data;
    struct listNode *nextPtr;
 };
 
 typedef struct listNode ListNode;
 typedef ListNode *ListNodePtr;
 
 void insert( ListNodePtr *, char );
 char delete( ListNodePtr *, char );
 int isEmpty( ListNodePtr );
 void printList( ListNodePtr );
 void instructions( void );
 
 int main()
 { 
    ListNodePtr startPtr = NULL;
    int choice;
    char item;
 
    instructions();  
    printf( "? " );
    scanf( "%d", &choice );
 
    while ( choice != 3 ) { 
 
       switch ( choice ) { 
          case 1:
             printf( "Enter a character: " );
             scanf( "\n%c", &item );
             insert( &startPtr, item );
             printList( startPtr );
             break;
          case 2:
             if ( !isEmpty( startPtr ) ) { 
                printf( "Enter character to be deleted: " );
                scanf( "\n%c", &item );
 
                if ( delete( &startPtr, item ) ) { 
                   printf( "%c deleted.\n", item );
                   printList( startPtr );
                }
                else
                   printf( "%c not found.\n\n", item );
             }
             else
                printf( "List is empty.\n\n" );
 
             break;
          default:
             printf( "Invalid choice.\n\n" );
             instructions();
             break;
       }
 
       printf( "? " );
       scanf( "%d", &choice );
    }
 
    printf( "End of run.\n" );
    return 0;
 }
 
 void instructions( void )
 { 
    printf( "Enter your choice:\n"
           "   1 to insert an element into the list.\n"
           "   2 to delete an element from the list.\n"
           "   3 to end.\n" );
 }
 
 void insert( ListNodePtr  sPtr, char value )
 { 
    ListNodePtr newPtr, previousPtr, currentPtr;
 
    newPtr = malloc( sizeof( ListNode ) );
 
    if ( newPtr != NULL ) {     
       newPtr->data = value;
       newPtr->nextPtr = NULL;
 
       previousPtr = NULL;
       currentPtr = *sPtr;
 
       while ( currentPtr != NULL && value > currentPtr->data ) { 
          previousPtr = currentPtr;          /* walk to ...   */
          currentPtr = currentPtr->nextPtr;  /* ... next node */
       }
 
       if ( previousPtr == NULL ) { 
          newPtr->nextPtr = *sPtr;
          *sPtr = newPtr;
       }
       else { 
          previousPtr->nextPtr = newPtr;
          newPtr->nextPtr = currentPtr;
       }
    }
    else
       printf( "%c not inserted. No memory available.\n", value );
 }
 
 char delete( ListNodePtr *sPtr,  value )
 { 
    ListNodePtr previousPtr, currentPtr, tempPtr;
 
    if ( value == ( *sPtr )->data ) { 
       tempPtr = *sPtr;
       *sPtr = ( *sPtr )->nextPtr;  /* de-thread the node */
       free( tempPtr );             /* free the de-threaded node */
       return value;
    }
    else { 
       previousPtr = *sPtr;
       currentPtr = ( *sPtr )->nextPtr;
 
       while ( currentPtr != NULL && currentPtr->data != value ) { 
          previousPtr = currentPtr;          /* walk to ...   */
          currentPtr = currentPtr->nextPtr;  /* ... next node */
       }
 
       if ( currentPtr != NULL ) { 
          tempPtr = currentPtr;
          previousPtr->nextPtr = currentPtr->nextPtr;
          free( tempPtr );
          return value;
       }                                                        
    }
 
    return '\0';
 }
 
 /* Return 1 if the list is empty, 0 otherwise */
 int isEmpty( ListNodePtr sPtr )
 { 
    return sPtr == NULL;
 }
 
 /* Print the list */
 void printList( ListNodePtr currentPtr )
 { 
    if ( currentPtr == NULL )
       printf( "List is empty.\n\n" );
    else { 
       printf( "The list is:\n" );
 
       while ( currentPtr != NULL ) { 
          printf( "%c --> ", currentPtr->data );
          currentPtr = currentPtr->nextPtr;
       }
 
       printf( "NULL\n\n" );
    }
 }
Question:

1. Explain each function in the above programme.
2. Identify the error and compile the programme.

Last edited by big_k105; Apr 29th, 2005 at 1:49 PM.
ahhjun is offline   Reply With Quote
Old Oct 10th, 2004, 2:31 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Answers:

1. Do you're own homework.
2. Tell your instructor that you are trying to cheat.

Do yourself a favor and try to answer them yourself first. Then let us know what you think and we can tell you if you are right or wrong and if wrong, we can tell you how you go about finding the answer. The first step is to actually try.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Oct 10th, 2004, 5:33 AM   #3
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5 Ravilj is on a distinguished road
Quote:
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

void insert( ListNodePtr *, char );
char delete( ListNodePtr *, char );
int isEmpty( ListNodePtr );
void printList( ListNodePtr );
This gives the answer away for question 1! Come on!

Question 2 requires you copy and paste it into an IDE and run it to see what errors come up.
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005!
Ravilj is offline   Reply With Quote
Old Oct 10th, 2004, 8:00 AM   #4
devMonk
Newbie
 
Join Date: Oct 2004
Posts: 2
Rep Power: 0 devMonk is on a distinguished road
im just getting started with C++ too...I tried compiling his code but got this error message when using the win32 console application in Visual C++ 6.0.

--------------------Configuration: test - Win32 Debug--------------------
Build : warning : failed to (or don't know how to) build 'C:\Program Files\Microsoft Visual Studio\MyProjects\test\test.cpp'
Compiling...
test.cpp
c:\program files\microsoft visual studio\myprojects\test\test.cpp(162) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.

test.obj - 1 error(s), 1 warning(s)

Ive tried other source codes and get the same problem. I dont think the code is in error, im just screwing something up or not doing something correctly. I would really appreciate it if someone could set me on the right path.
Thanks!
devMonk is offline   Reply With Quote
Old Oct 12th, 2004, 8:11 PM   #5
mici
Programmer
 
Join Date: Oct 2004
Posts: 67
Rep Power: 5 mici is on a distinguished road
Ok this one goes out to devMonk:


if you're gonna use MVS win32 console application you need to include stdafx.h otherwise it wont compile right.

so jus try adding :

#include "stdafx.h"
at the begining of the code and then the error output won't be the same...


as for the code ... won't be fair if we solved your problems... but here is a little hint...
delete is a protected keyword so don't use it ...
try using:

char my_delete( ListNodePtr *, char );

instead.
__________________
coffee is my heroin.
mici is offline   Reply With Quote
Old Oct 12th, 2004, 8:22 PM   #6
mici
Programmer
 
Join Date: Oct 2004
Posts: 67
Rep Power: 5 mici is on a distinguished road
Ah what tha hell.


// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

struct listNode {
char data;
struct listNode *nextPtr;
};

typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

void insert( ListNodePtr *, char );
char delet( ListNodePtr *, char );
int isEmpty( ListNodePtr );
void printList( ListNodePtr );
void instructions( void );

int main()
{
ListNodePtr startPtr = NULL;
int choice;
char item;

instructions();
printf( "? " );
scanf( "%d", &choice );

while ( choice != 3 ) {

switch ( choice ) {
case 1:
printf( "Enter a character: " );
scanf( "\n%c", &item );
insert( &startPtr, item );
printList( startPtr );
break;
case 2:
if ( !isEmpty( startPtr ) ) {
printf( "Enter character to be deleted: " );
scanf( "\n%c", &item );

if ( delet( &startPtr, item ) ) {
printf( "%c deleted.\n", item );
printList( startPtr );
}
else
printf( "%c not found.\n\n", item );
}
else
printf( "List is empty.\n\n" );

break;
default:
printf( "Invalid choice.\n\n" );
instructions();
break;
}

printf( "? " );
scanf( "%d", &choice );
}

printf( "End of run.\n" );
return 0;
}

void instructions( void )
{
printf( "Enter your choice:\n"
" 1 to insert an element into the list.\n"
" 2 to delete an element from the list.\n"
" 3 to end.\n" );
}

void insert( ListNodePtr *sPtr, char value )
{
ListNodePtr newPtr, previousPtr, currentPtr;

newPtr = (ListNode *)malloc( sizeof( ListNode ) );

if ( newPtr != NULL ) {
newPtr->data = value;
newPtr->nextPtr = NULL;

previousPtr = NULL;
currentPtr = *sPtr;

while ( currentPtr != NULL && value > currentPtr->data ) {
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
}

if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else
printf( "%c not inserted. No memory available.\n", value );
}

char delet( ListNodePtr *sPtr, char value )
{
ListNodePtr previousPtr, currentPtr, tempPtr;

if ( value == ( *sPtr )->data ) {
tempPtr = *sPtr;
*sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
free( tempPtr ); /* free the de-threaded node */
return value;
}
else {
previousPtr = *sPtr;
currentPtr = ( *sPtr )->nextPtr;

while ( currentPtr != NULL && currentPtr->data != value ) {
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
}

if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
return value;
}
}

return '\0';
}

/* Return 1 if the list is empty, 0 otherwise */
int isEmpty( ListNodePtr sPtr )
{
return sPtr == NULL;
}

/* Print the list */
void printList( ListNodePtr currentPtr )
{
if ( currentPtr == NULL )
printf( "List is empty.\n\n" );
else {
printf( "The list is:\n" );

while ( currentPtr != NULL ) {
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}

printf( "NULL\n\n" );
}
}


there i couldn't resist the temptation,
now at least you can observe the changes and perhaps learn a thing or two...
__________________
coffee is my heroin.
mici is offline   Reply With Quote
Old Oct 12th, 2004, 8:24 PM   #7
mici
Programmer
 
Join Date: Oct 2004
Posts: 67
Rep Power: 5 mici is on a distinguished road
Quote:
Originally posted by Infinite Recursion@Oct 10 2004, 06:31 AM
Answers:

1. Do you're own homework.
2. Tell your instructor that you are trying to cheat.

Do yourself a favor and try to answer them yourself first. Then let us know what you think and we can tell you if you are right or wrong and if wrong, we can tell you how you go about finding the answer. The first step is to actually try.
i prefer

kill -9 -1

looks cooler.
__________________
coffee is my heroin.
mici is offline   Reply With Quote
Old Oct 12th, 2004, 8:25 PM   #8
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Doh!

We don't directly answer homework here unless they have made an attempt. It's Infinite Recursion's unwritten code!
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 13th, 2004, 12:01 AM   #9
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Its the law! *flashes badge*

Nah, you guys can answer posts how you want. I just don't particular think its in the student's best interests if we do their homework form the without them making a valid effort.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Oct 13th, 2004, 3:14 AM   #10
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
whats the point in homework if your not going to learn anything?

true some homework in say, philisophy aint gonna help me complktete my comp sci exam but still.,
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC