code improvement before i go further!!

This is a discussion on code improvement before i go further!! within the C Programming forums, part of the General Programming Boards category; Hi everybody, i was performing some linked list operations, the code below adds node to the end of the list ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    code improvement before i go further!!

    Hi everybody,

    i was performing some linked list operations,
    the code below adds node to the end of the list by using function
    add_node().

    first i created a node in main() and then the program asks the user
    if he needs any more. now i am going to add other link list operations like delete etc.

    Kindly guide if lacking any good programming style or the code below can hav improvements.

    Thanks

    Code:
    #include<stdio.h>
    #include<conio.h>
    struct node{
                   int key;
                   struct node* next;
               };
               
    void add_node(struct node* start) { 
       struct node* temp;
       char ch;
       temp=start;
      
       while(temp->next!=NULL)
          temp=temp->next;
      
      struct node* nd;
      nd=(struct node*)malloc(sizeof(struct node));
      printf("Enter node value\n");
      scanf("%d",&nd->key);
      
      temp->next=nd;
      nd->next=NULL;
      
      printf("Do you want to add another node(y/n)\n");
      ch=getch();
      if(ch=='y')
         add_node(start);
      else
         return; 
        
    }
    
    int main()  
             
    { 
      struct node* start,*p,*q;
      
      start=(struct node*)malloc(sizeof(struct node));
      printf("Enter first node value\n");
      scanf("%d",&start->key);
      start->next=NULL;
      
      add_node(start);
      
      for(p=start;p!=NULL;p=p->next)
      printf("%d\n",p->key);
      
      for(p=start;p!=NULL;p=q) {
          q=p->next;
          free(p);
        }  
      
      getch();
      return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Why, are you going to pay attention this time?
    urgent regarding malloc()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    hey salem,

    friend i am ready put in the effort as required because i want to improve each day.
    Thats all i can say. now its upto u if u can help me.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21