Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Assembly Language Programming (ALP) Forum (http://www.go4expert.com/forums/assembly-language-programming-forum/)
-   -   Single-Linked Lists (http://www.go4expert.com/forums/single-linked-lists-t3573/)

husslela2 23Mar2007 09:20

Single-Linked Lists
 
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 :)

chandrou_k 24Mar2007 00:22

Re: Single-Linked Lists
 
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++;
    }
  }
}


shabbir 24Mar2007 09:52

Re: Single-Linked Lists
 
chandrou_k, please use the bbcode when having code snippets in the posts.

husslela2, apart from the code by chandrou_k there are some other linked list related programs here as well.

Basic operations in Linked List
Swap two nodes of a linked list
Move forward a node in linked list
Insert a node in a linked list
Double linked list
Double circular linked list
Circular linked list
Priority Queue implementation using Linked List
Queue implementation through linked list
Doubly Linked List Implementation in C#

shabbir 24Mar2007 10:44

Re: Single-Linked Lists
 
After posting I realised that you have posted in Assembly language programming section and don't tell me you are looking that in ALP.

DaWei 29Mar2007 02:38

Re: Single-Linked Lists
 
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.

shabbir 29Mar2007 08:23

Re: Single-Linked Lists
 
Sorry to be jumping in between but there are quite a few programs and source code for your reference.

Basic operations in Linked List
Swap two nodes of a linked list
Move forward a node in linked list
Insert a node in a linked list
Double linked list
Double circular linked list
Circular linked list
Priority Queue implementation using Linked List
Queue implementation through linked list
Doubly Linked List Implementation in C#

DaWei 30Mar2007 05:42

Re: Single-Linked Lists
 
Good links, and if the OP is going to use assembly language, the overall procedures are the same.

shabbir 30Mar2007 07:18

Re: Single-Linked Lists
 
Quote:

Originally Posted by DaWei
Good links, and if the OP is going to use assembly language, the overall procedures are the same.

Offtopic 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

chandrou_k 4Apr2007 20:45

Re: Single-Linked Lists
 
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...

DaWei 6Apr2007 05:35

Re: Single-Linked Lists
 
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.


All times are GMT +5.5. The time now is 19:51.