Thread: Function to add node between first and last node

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    81

    Function to add node between first and last node

    How to make function that can add the new node between fist and last node

    for example

    20 40
    add new value 30

    list 20 30 40

    add new value 50

    list 20 30 50 40

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
     
     
    struct Node
    {
        int Data;
        struct Node * Next;
    };
     
     
    struct Node * Add_New ( int n, struct Node * Pointer)
    {
     
     
        struct Node *New = malloc(sizeof(*New));
         
         
        // return Pointer;
    }
                      
    int main ()
    {
        
        struct Node * Head = NULL;
        struct Node * Tail = NULL;
        struct Node * Head = malloc(sizeof(*Head));
        struct Node * Tail = malloc(sizeof(*Tail));
        
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You've already been shown how to add to the head and tail of a list.
    You therefore ought to be able to at least make an ATTEMPT for yourself now.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by Salem View Post
    You've already been shown how to add to the head and tail of a list.
    You therefore ought to be able to at least make an ATTEMPT for yourself now.
    My ATTEMPT to solve problem

    Function to add node between first and last node-insert-node-jpg

    I have already shown my code. I have only problem with code

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OK, so you understand pictorially what you need to do.

    How about some code to go with it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by Salem View Post
    OK, so you understand pictorially what you need to do.

    How about some code to go with it?
    I have tried to write code, see post 1 But having trouble making full code So I asked how to make a function for list
    Last edited by gajya; 02-24-2020 at 11:21 PM.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Before you write code, write down detailed step-by-step instructions in English (or whatever language you're most comfortable with) on how you would accomplish the task. Think of it like a recipe: 1. do this, 2. do that, etc. Make sure each step is a simple task that even a simple-minded person can follow (a computer is a simple-minded, but very fast, person). This is often the "hard" part.

    Once you do that, then convert the instructions, line by line, into code. This is the "easy" part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Other function, Node at the end of a List
    By letthem in forum C Programming
    Replies: 4
    Last Post: 06-25-2019, 02:15 PM
  2. Quadtree Node -> pointers to the children node
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 06:38 PM
  3. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  4. NODE *RemoveDuplicates(NODE *p, NODE *q);
    By xnicky in forum C Programming
    Replies: 3
    Last Post: 12-03-2006, 05:30 PM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

Tags for this Thread