help: write a program that concatenates two linked lists of characters

[b][red]This message was edited by angela28 at 2003-11-30 16:9:33[/red][/b][hr]
write a program that concatenates two linked lists of characters. The program should include function concatenate that takes pointers to both lists as arguments and concatenates the second lists to the first list.

I'm not sure what i didn't wrong because it not like it should be.


#include
#include

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( "
%c", &item );
insert( &startPtr, item );
printList( startPtr );
break;
case 2:
if ( !isEmpty( startPtr ) ) {
printf( "Enter character to be deleted: " );
scanf( "
%c", &item );

if ( delete( &startPtr, item ) ) {
printf( "%c deleted.
", item );
printList( startPtr );
}
else
printf( "%c not found.

", item );
}
else
printf( "List is empty.

" );

break;
default:
printf( "Invalid choice.

" );
instructions();
break;
}

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

printf( "End of run.
" );
return 0;
}
void instructions( void )
{
printf( "Enter your choice:
"
" 1 to insert an element into the list.
"
" 2 to delete an element from the list.
"
" 3 to end.
"

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

newPtr = malloc( sizeof( ListNode ) );

if ( newPtr != NULL ) { /* is space available */
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.
", value );
}

char delete( 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 '';
}

int isEmpty( ListNodePtr sPtr )
{
return sPtr == NULL;
}


void printList( ListNodePtr currentPtr )
{
if ( currentPtr == NULL )
printf( "List is empty.

" );
else {
printf( "The list is:
" );

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

printf( "NULL

" );
}
}


Comments

  • : [b][red]This message was edited by angela28 at 2003-11-30 16:9:33[/red][/b][hr]
    : write a program that concatenates two linked lists of characters. The program should include function concatenate that takes pointers to both lists as arguments and concatenates the second lists to the first list.
    :
    : I'm not sure what i didn't wrong because it not like it should be.
    :
    [code]
    : #include
    : #include
    :
    : 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( "
    %c", &item );
    : insert( &startPtr, item );
    : printList( startPtr );
    : break;
    : case 2:
    : if ( !isEmpty( startPtr ) ) {
    : printf( "Enter character to be deleted: " );
    : scanf( "
    %c", &item );
    :
    : if ( delete( &startPtr, item ) ) {
    : printf( "%c deleted.
    ", item );
    : printList( startPtr );
    : }
    : else
    : printf( "%c not found.

    ", item );
    : }
    : else
    : printf( "List is empty.

    " );
    :
    : break;
    : default:
    : printf( "Invalid choice.

    " );
    : instructions();
    : break;
    : }
    :
    : printf( "? " );
    : scanf( "%d", &choice );
    : }
    :
    : printf( "End of run.
    " );
    : return 0;
    : }
    : void instructions( void )
    : {
    : printf( "Enter your choice:
    "
    : " 1 to insert an element into the list.
    "
    : " 2 to delete an element from the list.
    "
    : " 3 to end.
    "[red] );
    }[/red]
    :
    : void insert( ListNodePtr *sPtr, char value )
    : {
    : ListNodePtr newPtr, previousPtr, currentPtr;
    :
    : newPtr = malloc( sizeof( ListNode ) );
    :
    : if ( newPtr != NULL ) { /* is space available */
    : 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.
    ", value );
    : }
    :
    : char delete( 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 '';
    : }
    :
    : int isEmpty( ListNodePtr sPtr )
    : {
    : return sPtr == NULL;
    : }
    :
    :
    : void printList( ListNodePtr currentPtr )
    : {
    : if ( currentPtr == NULL )
    : printf( "List is empty.

    " );
    : else {
    : printf( "The list is:
    " );
    :
    : while ( currentPtr != NULL ) {
    : printf( "%c --> ", currentPtr->data );
    : currentPtr = currentPtr->nextPtr;
    : }
    :
    : printf( "NULL

    " );
    : }
    : }
    :
    [/code]

    There were some parenthesis (and stuff) missing.
    Because 'delete' is reserved in c++, you can get strange errors if you compiler assumes this is c++ ('parse error in line 13' for example), although it is perfectly good c code.
  • [code]

    check your code at the 'bolds' points,


    #include
    #include

    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( "
    %c", &item );
    insert( &startPtr, item );
    printList( startPtr );
    break;
    case 2:
    if ( !isEmpty( startPtr ) ) {
    printf( "Enter character to be deleted: " );
    scanf( "
    %c", &item );
    if ( delete( &startPtr, item ) ) {
    printf( "%c deleted.
    ", item );
    printList( startPtr );
    }
    else printf( "%c not found.

    ", item );
    }
    else printf( "List is empty.

    " );
    break;
    default:
    printf( "Invalid choice.

    " );
    instructions();
    break;
    }
    printf( "? " );
    scanf( "%d", &choice );
    }
    printf( "End of run.
    " );
    return 0;
    }

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

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

    newPtr = malloc( sizeof( ListNode ) );
    if(newPtr != NULL ) { /* is space available */
    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.
    ", value );
    }

    char delete( 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 '';
    }

    int isEmpty( ListNodePtr sPtr ){
    return sPtr == NULL;
    }

    void printList( ListNodePtr currentPtr ) {
    if(currentPtr==NULL)
    printf( "List is empty.

    " );
    else{
    printf( "The list is:
    " );
    while(currentPtr!=NULL){
    printf( "%c --> ",currentPtr->data );
    currentPtr=currentPtr->nextPtr;
    }
    printf( "NULL

    " );
    }
    }
    [/code]

    check theese two functions too :
    [code]
    void insert(ListNodePtr *sPtr,char value){
    ListNodePtr newPtr,tempPtr=*sPtr;
    newPtr=malloc( sizeof( ListNode ) );
    if(newPtr==NULL){printf( "%c not inserted. No memory available.
    ", value );return;}
    /* is space available */
    newPtr->data=value;
    newPtr->nextPtr=NULL;
    while(1){
    if(tempPtr.nextPtr==NULL || value > tempPtr.nextPtr.data) break;
    tempPtr=tempPtr.nextPtr;
    }
    tempPtr.nextPtr=newPtr;
    }

    char delete(ListNodePtr *sPtr,char value){
    ListNodePtr delPtr=NULL,tempPtr=*sPtr;
    while(tempPtr.data!=value && tempPtr!=NULL)
    {
    if(tempPtr.nextPtr!=NULL && tempPtr.nextPtr.data==value) break;
    else tempPtr=tempPtr.nextPtr;
    }
    //check if 'value' found
    if(!tempPtr) return(0);//Not Found
    //the 'value' is at tempPtr.nextPtr
    delPtr=tempPtr.nextPtr;
    tempPtr.nextPtr=tempPtr.nextPtr.nextPtr;
    free(delPtr);
    return(value);
    }
    [/code]

    to make your link lists more flexible, try to add a previusPtr in your struct
    when you'll program under this model you'll see that yor code
    will be both much readable and small

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories