Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:40:04+00:00 2026-06-15T19:40:04+00:00

I have created a list in C using a self-referential structure, I figured how

  • 0

I have created a list in C using a self-referential structure, I figured how to do every thing possible with the list but delete the last element, this is puzzling me for a few weeks now, please help me
this is what I did.
To get the concept of this program you only have to look at functions main,and Delete, maybe insert. Please note that tis code was compiled on linux using gcc in ubuntu.

/* self-referencial structure */
struct ListNode
{
    char data;
struct ListNode *nextNode ;
};


typedef struct ListNode ListNode;
typedef ListNode *ListNodePtr ;

/* function protypes */
void insert(ListNodePtr *sptr, char value); // insert element in list in alphabet order
void Delete (ListNodePtr *sptr);// PROBLEM HERE; remove last element from list
void printList(ListNodePtr);// output the contents of the list
void instructions(); // print instruction on screen

int main(void)
{
ListNodePtr startPtr = NULL ; // there are initially no nodes
int choice;
char item ;// char to be placed in list

/* Give instruction s and allow user to make a choice
 * insert , print, delete.
 */
instructions();
printf("?");
scanf("%d", &choice);

/* loop until sentinel value 3 */
while (!(choice == 4) )
{
    switch(choice)
    {
    case 1:
        printf("Enter a character");
        scanf("\n%c",&item);
        insert(&startPtr, item);
        printList(startPtr);
        break ;

    case 2:
        /* If the list is not empty */
        Delete(&startPtr);
        printList(startPtr);
        // if it is printf("the list is empty");
        break;
    case 3:
        printList(startPtr);
        break;
    default:
        printf("Invalid choice");
        break;
    }// end switch
    printf("?");
    scanf("%d",&choice);
}// end while
return 0 ;
}// END FUNCTION MAIN

/* display program choices/instruction to user */
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 print the list\n"
    "4 to end.\n" );
 }

void insert(ListNodePtr *sptr, char value)
{
ListNodePtr currentPtr;
ListNodePtr previousPtr;
ListNodePtr newNode ;


/* create an area in memory the size of ListNode and
 *allocate to newPtr */
newNode = malloc(sizeof(ListNode)); // create new node

// if space is available
if (newNode != NULL)
{
    newNode->data = value;
    newNode->nextNode = NULL;

    previousPtr = NULL ;
    currentPtr = *sptr ;

    /* walk to correct location in the list */
    while (currentPtr != NULL && value > currentPtr->data)
    {
        previousPtr = currentPtr;
        currentPtr = currentPtr->nextNode;
    }// end while

    /* insert node at beginning of the list */
    if(previousPtr == NULL)
    {
        newNode->nextNode = *sptr;
        *sptr = newNode;
    }// end if

    else
    {
        previousPtr->nextNode = newNode;
        newNode->nextNode = currentPtr ;
    }
}// end if
else
{
    printf("%c not inserted , memory not available", value);
}

 }

void Delete(ListNodePtr *sptr)
{
ListNodePtr previousPtr;
ListNodePtr currentPtr;
ListNodePtr tempPtr;

// if there is only  one element
if((*sptr)->nextNode == NULL)
{
    tempPtr = *sptr; // hold on to Node being removed
    *sptr = (*sptr)->nextNode;
    free(tempPtr);
}
else
{
    previousPtr = *sptr ;
    currentPtr = (*sptr)->nextNode;

    // walk to end of the list
    while(currentPtr->nextNode != NULL)
    {
        previousPtr = *sptr;
        currentPtr = currentPtr->nextNode;
    }

    if(currentPtr != NULL)
    {
        // WHAT DO I DO HERE ??!!??
        // if the list has more than one two elements, 
        // all but the first element is deleted

        previousPtr->nextNode = currentPtr->nextNode;
        currentPtr = NULL ;
        free(currentPtr);
    }// end if
}// end else 


 }// END FUNCTION DELETE

 void printList(ListNodePtr currentPtr)
{
if(currentPtr == NULL)
printf("List is empty");
else
{
    while(currentPtr != NULL)
    {
        printf( "%c --> ", currentPtr->data );
        currentPtr = currentPtr->nextNode;
    } /* end while */
        printf( "NULL\n\n" );
    /* end else */


}
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:40:05+00:00Added an answer on June 15, 2026 at 7:40 pm

    may be this helps

    void Delete(ListNodePtr *sptr)
    {
     ListNodePtr previousPtr;
     ListNodePtr currentPtr; 
     ListNodePtr tempPtr;
    
     if((*sptr)->nextNode == NULL)
     {
       tempPtr = *sptr; // hold on to Node being removed
       *sptr = (*sptr)->nextNode;
       free(tempPtr);
     }
    else  
    {
    previousPtr = *sptr ;
    currentPtr = (*sptr)->nextNode;
    
    // walk to end of the list
    while(currentPtr->nextNode != NULL)
    {
        previousPtr=previousPtr->nextNode;//this always points to the previous node 
        currentPtr = currentPtr->nextNode;
    }
    
    //after loop ends currentPtr points to the last node
    
    if(currentPtr != NULL)
    {
        previousPtr->nextNode=NULL;//This deletes the currentPtr from the list
        free(currentPtr);
    }// end if
    }// end else 
    }// END FUNCTION DELETE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a dynamic list picker script using Jquery 1.3 and PHP that
I'm trying to access list data using SSRS 2008. I have created an XML
I have created navigaton view using Sencha touch 2. Navigation view has list component
I want to create a form using an unordered list ul must have only
I have created a list view that displays the names and dates of items
I have created a list form that gets attached to a main form in
I have created a custom list as a feature in sharepoint. i need to
I have created a custom list view, each row has it's own custom selector,
I have dynamically created some list of RadioButtons with some values. pro grammatically I
I have a dynamically created list. Items from the list should be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.