Thread: algorithms to creating list

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    algorithms to creating list

    I am having trouble to create linked list I made a rough algorithm to create link list to understand basics

    Step 1 : Create a New node
    Step 2 : assign data to data member of the new node
    Step 3 : if head is empty go to step 4 else else go to step 4
    step 4 : make new node as head node and goto step 5
    Step 5 : End

    step 1 : code for step 1 : Create a New node

    Code:
    struct Node{
      int Number;
      struct Node *next;
    }
    Step 2 : code for step 2 : assign data to data member of the new node
    Code:
    // assign data to Node 
    Node->Number = 2;
    Step 3 : Code for step 3 : if head is empty go to step 4 else else go to step 4


    Code:
    head=NULL;
    if (head ==NULL)
       End ;
    Hope anyone who is familiar with this can help me to understand basics
    Last edited by vajra11; 10-29-2019 at 11:25 PM.

  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
    Step 0, declare a Node structure
    Code:
    struct Node{
      int Number;
      struct Node *next;
    }
    Step 1, create a Node
    Code:
    struct Node* newNode(int number, struct node *next) {
        struct Node *new = malloc(sizeof(*new));
        new->Number = number;
        new->next = next;
    }
    Step 2, an empty list
    Code:
    int main ( ) {
        struct Node *list = NULL;
    }
    Step 3, adding a node
    Code:
    int main ( ) {
        struct Node *list = NULL;
        list = newNode(1,list);
    }
    Step 4, add another node (at the front)
    Code:
    int main ( ) {
        struct Node *list = NULL;
        list = newNode(1,list);
        list = newNode(2,list);
    }
    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
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Step 0, declare a Node structure

    Step 1, create a Node
    Hi Salem

    It was nice explanations but I do not understand step 1

    I do not return type in the below function. I haven't seen this type of function deceleration in c language
    Code:
    struct Node* newNode(int number, struct node *next) {
        struct Node *new = malloc(sizeof(*new));
        new->Number = number;
        new->next = next;
    }
    as per my understand I have tried to write in one program

    Code:
    #include<stdio.h>#include<stdlib.h>
    
    
    struct Node{
      int Number;
      struct Node *next;
    }
    // does it valid function in c  
    struct Node* newNode(int number, struct node *next) {
        struct Node *new = malloc(sizeof(*new));
        new->Number = number;
        new->next = next;
    }
    
    
    int main ( ) {
        struct Node *list = NULL;
        list = newNode(1,list);
        list = newNode(2,list);
        
        return 0;
    }
    Last edited by vajra11; 10-30-2019 at 11:46 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to return the pointer to the new node.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well sure, you can mush it all together in main if you want.

    But learning about functions, how to pass parameters and how to return results should be HIGH on your list of things to do next.

    Here, if only to show you how tedious and painful it is to keep copy/pasting.
    Code:
    int main ( ) {
        struct Node *list = NULL;
    
        struct Node *new1 = malloc(sizeof(*new1));
        new1->Number = 1;
        new1->next = list;
        list = new1;
    
        struct Node *new2 = malloc(sizeof(*new2));
        new2->Number = 2;
        new2->next = list;
        list = new2;
    }
    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.

  6. #6
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by laserlight View Post
    You need to return the pointer to the new node.
    I am doing

    Code:
    struct Node* newNode(int number, struct node *next) {    struct Node *new = malloc(sizeof(*new));
        new->Number = number;
        new->next = next;
    	return new;
    }

  7. #7
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Well sure, you can mush it all together in main if you want.

    But learning about functions, how to pass parameters and how to return results should be HIGH on your list of things to do next.
    I am agree with you. I have studied easy example of functions to pass the parameter and return result. I would work on something complex such as passing the structure and I feel I am lacking somewhere in structure

    But I am on my point I have never seen this type of declaration

    Code:
    structNode* newNode(intnumber, structnode *next) {    struct Node *new = malloc(sizeof(*new));
        new->Number = number;
        new->next = next;
    }
    If you don't mind How would you convert your steps made in post 2 into program.

    I tried as per my best but I couldn't get result because of my bad understanding
    Last edited by vajra11; 10-31-2019 at 12:52 AM.

  8. #8
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by laserlight View Post
    You need to return the pointer to the new node.
    I am sure this function do what you suggested
    Code:
    structNode* newNode(intnumber, structnode *next) {    structNode *new= malloc(sizeof(*new));    new->Number = number;
        new->next = next;
        return new;
    }
    Does it function return pointer to new node

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Great, so what is your current code and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by laserlight View Post
    Great, so what is your current code and how does it not work?
    laserlight Thanks for watching this post

    I need to spend more time to learning and I am trying it.

    I want to display list and I do not understand how to print each node
    Code:
    #include<stdio.h>#include<stdlib.h> 
    struct Node{
      int Number;
      struct Node *next;
    }
      
    struct Node* newNode(int number, struct node *next) {
        struct Node *new = malloc(sizeof(*new));
        new->Number = number;
        new->next = next;
        return new;
    }
      
    int main ( ) {
        
        struct Node *list = NULL;
        list = newNode(1,list);
        printf ("list : %d ", list);
        list = newNode(2,list);
        list = newNode(3,list); 
        
        return 0;
    }


    Test.c:9:1: error: expected ';', identifier or '(' before 'struct'
    struct Node* newNode(int number, struct node *next) {
    ^~~~~~
    Test.c:9:41: warning: 'struct node' declared inside parameter list will not be visible outside of this definition or declaration
    struct Node* newNode(int number, struct node *next) {
    ^~~~
    Test.c: In function 'newNode':
    Test.c:12:15: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
    new->next = next;
    ^
    Test.c: In function 'main':
    Test.c:19:22: warning: passing argument 2 of 'newNode' from incompatible pointer type [-Wincompatible-pointer-types]
    list = newNode(1,list);
    ^~~~
    Test.c:9:14: note: expected 'struct node *' but argument is of type 'struct Node *'
    struct Node* newNode(int number, struct node *next) {
    ^~~~~~~
    Test.c:23:22: warning: passing argument 2 of 'newNode' from incompatible pointer type [-Wincompatible-pointer-types]
    list = newNode(2,list);
    ^~~~
    Test.c:9:14: note: expected 'struct node *' but argument is of type 'struct Node *'
    struct Node* newNode(int number, struct node *next) {
    ^~~~~~~
    Test.c:24:22: warning: passing argument 2 of 'newNode' from incompatible pointer type [-Wincompatible-pointer-types]
    list = newNode(3,list);
    ^~~~
    Test.c:9:14: note: expected 'struct node *' but argument is of type 'struct Node *'
    struct Node* newNode(int number, struct node *next) {
    Last edited by vajra11; 11-02-2019 at 05:53 AM.

  11. #11
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Apparently you don't understand the concept of a pointer, think of pointers as a phone and the data you want to pass as the phone numbers, when you want to pass a number you don't draw the phone and pass that you read the number on the display what you did is use the pointer which the compiler saw as unfit for what you attempted to use it for and thus refused to continue just as a person would refuse to take a drawing of a phone as a phone number (unless they're from stone age or tremendously stupid)

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or a pointer is like your address on a letter, not your whole house.
    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.

  13. #13
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Or a pointer is like your address on a letter, not your whole house.
    I have corrected errors so How to call the function in main
    Code:
    #include<stdio.h>#include<stdlib.h> 
    struct Node{
      int Number;
      struct Node *next;
    };
       
    struct Node *newNode(int number, struct node *next) {
        struct Node *new = malloc(sizeof(*new));
        new->Number = number;
        new->next = next;
        return new;
    }
       
    int main ( ) {
    
       
         
        return 0;
    }

  14. #14
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    That's not correction, that's removal

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I have corrected errors so How to call the function in main
    Round and round we go,
    algorithms to creating list
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a List-View
    By Lionel in forum Windows Programming
    Replies: 6
    Last Post: 09-20-2010, 08:13 PM
  2. Creating list view
    By Hani in forum Windows Programming
    Replies: 8
    Last Post: 04-24-2008, 08:49 AM
  3. Creating a list of words
    By pete212 in forum C Programming
    Replies: 4
    Last Post: 04-22-2008, 12:50 PM
  4. creating a sorted list
    By S15_88 in forum C Programming
    Replies: 3
    Last Post: 03-20-2008, 01:14 AM
  5. Creating Linked list in .NET
    By axp in forum Windows Programming
    Replies: 1
    Last Post: 05-15-2004, 12:41 AM

Tags for this Thread