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 7179069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:03:57+00:00 2026-05-28T17:03:57+00:00

I’m trying to understand this code from Deitels 5th edition c book. If I’m

  • 0

I’m trying to understand this code from Deitels 5th edition c book.

If I’m an inserting a new node in a linked list, why am I setting a nextPtr = *sPtr. Shouldn’t the first node have a nextPtr of NULL?

Also what does *sPtr = newPtr mean?

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


void insert(ListNodePtr *sPtr, char value)
{

ListNodePtr newPtr; /* pointer to a new node */
ListNodePtr previousPtr;  /* pointer to a previous a node in list */
ListNodePtr currentPtr;  /* pointer to current node in list */

newPtr = malloc(sizeof(ListNode));  /* create node */

if (newPtr != NULL){ /* is space available */
    newPtr->data = value;
    newPtr->nextPtr = NULL;

    previousPtr = NULL;
    currentPtr = *sPtr;

    while (currentPtr != NULL && value > currentPtr->data){
        previousPtr = currentPtr;
        currentPtr = currentPtr->nextPtr;
    }

    /* insert new node at beginning of list */
    if (previousPtr == NULL){
        newPtr->nextPtr = *sPtr;
        *sPtr = newPtr;
    } else { /* insert new node between previosuPtr and currentPtr */
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    }

}
}
  • 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-05-28T17:03:58+00:00Added an answer on May 28, 2026 at 5:03 pm

    The first node shouldn’t have it’s nextPtr set to NULL, that would be the last node (afterall you need to be able to get from the first to the second node). The nextPtr of the first node points to the second node, which has a pointer to the third node and so on. So when you insert at the beginning of the list you let the nextPtr of the new node point to the old beginning of the list, which is *sPtr. Afterwards you set the pointer which points to the beginning of the list to point to the new node by doing *sPtr=newPtr.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I have this code to decode numeric html entities to the UTF8 equivalent character.
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.