Better programming practice in linked list

In linked list Which is better programming practing using double pointers or just declaring head pointer globally

//Here head double pointer is passed as argument

Void insertatend(node **head, int item) { node *ptr, *loc; ptr=(node*)malloc(sizeof(node)); ptr->info=item; ptr->next=NULL; if(*head==NULL) *head=ptr; else { loc=*head; while (loc->next!=NULL) { loc=loc->next; loc->next=ptr; } } }

or this

//Here I have declared head pointer as global

void insert(int x) { node *ptr,*ptr1; ptr=(node*)malloc(sizeof(node)); ptr->info=x; if(head==NULL) { ptr->next=head; head=ptr; } else { ptr1=head; while(ptr1->next!=NULL) { ptr1=ptr1->next; } ptr1->next=ptr; ptr->next=NULL; } }

-------------Problems Reply------------

I'd say neither:

void insertatend(node *head, int item)
node *ptr, *loc;
while (loc->next!=NULL)


I don't know why you would want to change the address to the head pointer inside your function so there's no reason to pass it in as a pointer.

In general, good programming practices will always discourage global variables as you can see in these examples:
Are global variables bad?
Python: Why are global variables evil?
When is it ok to use a global variable in C?

The double pointer version can be simplified:

Void insertatend(node **head, int item)
node *ptr;
head = &((*head)->next);
*head = ptr;

Category:c# Views:2 Time:2017-09-13

Related post

  • Python good programming practice for enumerating lists 2012-04-07

    I'm pretty new to Python and programming in general, and I was wondering if it is a good programming practice to write long statements with many logic operators - for example, in a for loop. For example, here's a function I made that gets all the vow

  • Queue performance wise which is better implementation - Array or Linked list 2011-02-24

    Which way gives the faster enqueueing and dequeuing when I have to insert very few elements, Is array better than a linked list? I need to insert few element and I have to remove and read that removed element from queue. If it is array I may have to

  • How to create two different node types for the same program? (C++ Linked Lists) 2011-02-27

    I have another project to finish off regarding linked lists. I wanted to know if it was possible to write in TWO struct nodetype codes to create two nodes. If so how and where should I place them? My code is going to contain of one node type with "Vi

  • Why does heap corruption occur in my stack program implemented with linked list? And how to fix it? 2015-02-18

    I've created a C header file for a data structure course which I'm taking in school. I've only limited coding experience in C and C++. It contains code which builds a stack using linked list for data storage. When I try to run a driver program with V

  • In practice is Linked List addition O(N) or O(1)? 2012-01-25

    It is said that addition and deletion in a Linked List happens in constant time ie O(1) but access to elements happen in time proportional to the size of the list ie O(N). My question is how can you remove or add any element without first traversing

  • Input asked for better programming practices 2010-02-09

    As I'm learning C++ I started implementing some common datastructures as a form of practice. The first one being a Stack (this was the first to spring in mind). I've done some programming and it's working, but now I need some input as to what I shoul

  • Better Programming Practice 2012-03-14

    I am trying to write a RSA program to learn more about the cipher. I am using Java, but I am looking for general advice. My question is, what is the best way to take the contents of a file, and transform it into blocks of doubles to raise to a power?

  • Segmentation fault in program using linked list 2012-03-14

    Code: #include<stdio.h> #include<malloc.h> struct details{ char *name; int no; struct details *info; }; //ADDING THE LINKED LIST void add(struct details **info,int no,char * name){ struct details *temp=malloc(sizeof(struct details)); temp

  • Am I done with this Linked List code? 2008-10-29

    Hi I'm trying to get some practice with Linked Lists. I Defined an Object class called Student: public class Student { protected string Student_Name; protected int Student_ID; protected int Student_Mark; protected char Student_Grade; public Student()

  • Splitting a linked list 2008-11-27

    Why are the split lists always empty in this program? (It is derived from the code on the Wikipedia page on Linked Lists.) /* Example program from wikipedia linked list article Modified to find nth node and to split the list */ #include <stdio.h

  • Single linked lists C++ 2011-05-17

    Hey guys, I'm studying for a midterm right now and am working on trying to create a simple program using single linked list. All I want it to do is insert "1", "2", "3", "4" into the list and print it out. Please take a look at the following code: #i

  • Disjoint set as linked list 2009-08-07

    Can anyone point me to some info on Disjoint sets as linked list? I cant find any code on this. Language C++ --------------Solutions------------- Well I think you can find information in this page of Wikipedia. Of course, that information is written

  • Different Types of Linked Lists! 2010-03-05

    What are the different types of Linked Lists which are commonly used? I know and have used the following: Singly Linked List Doubly Linked List Circular List What are the other kinds of lists that have been used by you or known to you? --------------

  • Singly linked list to doubly linked list 2010-11-17

    A friend of mine wants me to convert his code into a doubly linked list, though I'm not familiar with it at all. I've looked up doubly linked lists but I can't tell by his code what to do with it. I'm not a master programmer. Do you have any suggesti

  • Spreadsheet data - linked list or hashmap? 2011-02-19

    I am looking to implement a spreadsheet in java. Would it be better to use a linked list (rows) of linked list of cells (columns) to store the data or a hashmap (each cell maps to a key e.g. A1 -> 1, A2-> 2 etc.)? Or is there an even better dat

  • How do you allocate memory for an linked list when passing its reference instead of its pointer? 2011-06-15

    How do you allocate memory for an link list when passing its reference instead of its pointer? For example: struct node { string info; node *next; }; void add(node &aNode){ //if I use node *newNode; newNode = new node; = newNode; //aNo

  • Doubly Linked List: Sorting nodes in ascending order 2011-11-01

    I'm working on a program using doubly linked lists. I have already gotten the functionality to insert nodes, remove nodes, retrieve the value from a node, display the list, output the size of the list, telling if the list is empty, and deleting all n

  • Reverse Linked List Recursively 2011-11-01

    I have a Node defined in Linked List as: typedef struct abc { int id; struct abc *next; }node; I want to reverse a Linked List recursively.I am passing the head pointer to the function. My function definition looks like: node *reverseLinkedListRecurs

  • Problems with singly linked list using classes 2011-12-07

    I am making a program for singly linked lists using multiple files and classes. I have to have a Node.h, LinkedList.h, Node.cpp, LinkedList.cpp, and a main.cpp I was having other problems but now my printList() function just prints "List()" instead o

  • Lazy vs eager evaluation and double linked list building 2012-02-14

    I can't sleep! :) I've written small program building double linked list in Haskell. The basic language's property to make it was lazy evaluation (see the bunch of code below). And my question is can I do the same in a pure functional language with e

  • Sorting a linked list in C (Selection Sort) 2012-02-20

    what is better while sorting a linked list, directly swaping the values or changing the address of the next pointer. I came across many examples using the swaping value technique, but none using the address change mechanism Method used: Selection Sor

  • Creating Linked List - Compiler Errors 2012-02-22

    I am trying to create a program for a linked list. I want to create a case statement where the user can choose to 1. add a node 2. delete a node 3. search for a node 4. display linked list (5 nodes per line) 5. quit the program However the program is

  • What is a practical, real world example of the Linked List? 2009-03-13

    I understand the definition of a Linked List, but how can it be represented and related to a common concept or item? For example, inheritance in OOP can be related to automobiles. All (most) automobiles in real life are the essentially same thing; an

  • Are python list comprehensions always a good programming practice? 2011-11-18

    To make the question clear, I'll use a specific example. I have a list of college courses, and each course has a few fields (all of which are strings). The user gives me a string of search terms, and I return a list of courses that match all of the s

Copyright (C), All Rights Reserved.

processed in 0.554 (s). 11 q(s)