![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2004
Posts: 1
Rep Power: 0
![]() |
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" );
}
}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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5
![]() |
Quote:
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! |
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2004
Posts: 2
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 5
![]() |
Quote:
kill -9 -1 looks cooler.
__________________
coffee is my heroin. |
|
|
|
|
|
|
#8 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Doh!
We don't directly answer homework here unless they have made an attempt. It's Infinite Recursion's unwritten code! ![]()
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#9 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#10 |
|
Programming Guru
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|