Single-Linked Lists

Newbie Member
23Mar2007,09:20   #1
husslela2's Avatar
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
Light Poster
24Mar2007,00:22   #2
chandrou_k's Avatar
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 shabbir; 24Mar2007 at 09:42.. Reason: Code formating.
Go4Expert Founder
24Mar2007,09:52   #3
shabbir's Avatar
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#
Go4Expert Founder
24Mar2007,10:44   #4
shabbir's Avatar
After posting I realised that you have posted in Assembly language programming section and don't tell me you are looking that in ALP.
Team Leader
29Mar2007,02:38   #5
DaWei's Avatar
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.
Team Leader
30Mar2007,05:42   #7
DaWei's Avatar
Good links, and if the OP is going to use assembly language, the overall procedures are the same.

Last edited by DaWei; 30Mar2007 at 05:45..
Go4Expert Founder
30Mar2007,07:18   #8
shabbir's Avatar
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
Light Poster
4Apr2007,20:45   #9
chandrou_k's Avatar
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...
Team Leader
6Apr2007,05:35   #10
DaWei's Avatar
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.