Single-Linked Lists

Discussion in 'Assembly Language Programming (ALP) Forum' started by husslela2, Mar 23, 2007.

  1. husslela2

    husslela2 New Member

    Joined:
    Mar 23, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    How do you create a single linked list that creates a node and inserts it into an already existing list and sorts the list in order (if you have a list of integers), and prints the new list?

    Thanks,
    Husslela :)
     
  2. chandrou_k

    chandrou_k New Member

    Joined:
    Feb 19, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    hai, i have attached linked list program in 'c' along this post.... if u compile it will show 2 warning message, just ignore it and execute the program....
    take care...
    Code:
    #include<conio.h>
    #include<stdio.h>
    void creation(int);
    void display();
    void insertion(int,int);
    void deletion(int);
    
    struct list
    {
      int num;
      struct list *next;
    }*head, *l;
    
    void main()
    {
      int n1,ch,pos,val;
      clrscr();
      printf("enter the no of nodes to be entered : ");
      scanf("%d",&n1);
    
      do
      {
         clrscr();
         printf("\n1.creation\n2.insertion\n3.deletion\n4.display\n5.exit\n");
         printf("\nenter ur choice : ");
         scanf("%d",&ch);
         switch(ch)
         {
    	case 1:
    		creation(n1);
    		break;
    	case 2:
    		printf("\n\nenter the postion in which to be inserted : ");
    		scanf("%d",&pos);
    		printf("\n\nenter the value : ");
    		scanf("%d",&val);
    		insertion(pos,val);
    		break;
    	case 3:
    		printf("\n\nenter the position to be deleted : ");
    		scanf("%d",&pos);
    		deletion(pos);
    		break;
    	case 4:
    		display();
    		getch();
    		break;
    	case 5:
    		exit(0);
         }
      }while(ch!=5);
    
      getch();
    }
    
    /*  CREATION   */
    /*  --------   */
    
    
    void creation(int n1)
    {
      int i,n;
      head=((struct list *)malloc(sizeof(struct list)));
      l=head;
      for(i=0;i<n1;i++)
      {
         printf("enter the %d node : ",i+1);
         scanf("%d",&n);
         l->num=n;
         l->next=((struct list *)malloc(sizeof(struct list)));
         l=l->next;
      }
      l->next=0;
    
    }
    
    /*  DISPLAY   */
    /*  -------   */
    
    
    void display()
    {
      l=head;
      printf("\nthe nodes entered are : ");
    
      while(l->next>0)
      {
        printf("%d\t",l->num);
        l=l->next;
      }
      printf("null");
    }
    
    
    /*  INSERTION   */
    /*  ---------   */
    
    void insertion(pos,val)
    {
      int i;
      struct list *x,*y;
      l=head;
      i=2;
      if(pos==1)
      {
        x=((struct list *)malloc(sizeof(struct list)));
        x->num=val;
        x->next=l;
        head=x;
      }
      else
      {
    	while(l->next>0)
    	{
    	    if(pos==i-1)
    	    {
    	      x=((struct list *)malloc(sizeof(struct list)));
    	      x->num=val;
    	      x->next=l;
    	      y->next=x;
    	    }
    	    y=l;
    	    l=l->next;
    	    i++;
    	}
      }
    }
    
    /*  DELETION   */
    /*  --------   */
    
    
    void deletion(pos)
    {
      int i;
      struct list *y;
      l=head;
      i=1;
      if(pos==1)
      {   head=l->next;  }
      else
      {
        while(l->next>0)
        {
          if(pos==i)
          { l=l->next;
    	y->next=l;
    	break;
          }
          y=l;
          l=l->next;
          i++;
        }
      }
    }
     
    Last edited by a moderator: Mar 24, 2007
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    After posting I realised that you have posted in Assembly language programming section and don't tell me you are looking that in ALP.
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Regarding the C code, just what warnings would you advise a user to ignore? Most warnings are indicative of an error on the part of the coder, even it it happens, from time to time, to not be fatal. Would you be referring to warnings regarding your use of 'malloc', because you forgot to #include stdlib.h or malloc.h?

    Further, you have a serious error that the compiler won't flag. You don't test the return from malloc to see if it's NULL. The system is perfectly within its rights to refuse to allocate the memory and return a NULL indicating that. Trying to dereference that NULL will send your program off into the weeds to barf on its shoes.

    Given all that, and the use of non-portable things like conio.h (whose functions actually add virtually nothing to your program), this is not a robust example of a linked-list implementation.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Good links, and if the OP is going to use assembly language, the overall procedures are the same.
     
    Last edited: Mar 30, 2007
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    [Comment]I probably did not aim at posting the same links again in this window but somehow missed the thread I wanted to put and posted here again[/comment]
     
  9. chandrou_k

    chandrou_k New Member

    Joined:
    Feb 19, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    hello DaWei,
    thanx for ur valueable note.... it been a practise for me to include both header files in every program.... and thanx again for ur note.... i have planned to do a project, in that i need to get a part of input from an electronic counter..... i have an intermediate knowledge in c and vb. can u suggest me which would be preferable for this...
     
  10. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You should start a separate thread for a different question. Getting input from an external device will depend entirely upon your system implementation and its operating system. Access to various ports is not as readily available from desktop systems as it once was (that's a protective benefit). However, each system will probably provide some methods for serial and parallel port I/O. Again, they will vary depending upon what you have.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice