Thread: code improvement before i go further!!

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why, are you going to pay attention this time?
    http://cboard.cprogramming.com/showthread.php?t=60566
    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 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, 06:05 PM