View Single Post
Old 28-09-2007, 11:40 PM   #247 (permalink)
ayush_chh
In The Zone
 
ayush_chh's Avatar
 
Join Date: Nov 2005
Location: Bangalore
Posts: 487
Default Re: Post ur C/C++ Programs Here

Here is program to insert a node in a singly linked list. The program works fine but it gives a warning 'the code is unusable' and if i remove this(bolded part) then the program doesn't work........

Code:
/* Inserting a node into a singly Linked List*/

#include<stdio.h>
#include<conio.h>
struct list
{
 int info;
 struct list *link;
};
typedef struct list node;
// create the list
void createlist(node *n)
{
 char ch;
 printf("\n\nEnter the item (number only) to be inserted\n");
 scanf("%d",&n->info);
 printf("\nDo you want to continue(Y/N)\n");
 ch= getche();
 getch();
 if(ch=='y'|| ch=='Y')
 {
  n->link=(node *)malloc(sizeof(node));
  createlist(n->link);
 }
 else
 n->link=NULL;
}
void displaylist(node *n)
{
 node *n1;
 n1=n;
 if(n1!=NULL)
 {
  printf("%d\n",n->info);
  displaylist(n1->link);
 }
}
void insnode(node *n,int desti)
{
 int cnt=1;
 node *n1, *temp;
 n1=n;
 while(n1!=NULL)
 {
  cnt++;
  if(desti==1)
  {
   temp->link=(node *)malloc(sizeof(node));
   printf("\nEnter the item to be inserted\n");
   scanf("%d",&temp->info);
   temp->link=n1;
   n=temp;
   n1->link;
   printf("\nNode Inserted Successfully\n");
   printf("\nThe new Linked List elements are\n");
   break;
  }
  else if(desti==cnt)
  {
   temp->link=(node *)malloc(sizeof(node));
   printf("\nEnter the item to be inserted\n");
   scanf("%d",&temp->info);
   temp->link=n1->link;
   n1->link=temp;
   printf("\nNode Inserted Successfully\n");
   printf("\nThe new Linked List elements are\n");
   break;
  }
  n1=n1->link;
 }
 displaylist(n);
}
void main()
{
 node *start;
 int loc;
 clrscr();
 start->link=(node *)malloc(sizeof(node));
 printf("\nCreate a linked list :");
 createlist(start);
 printf("\n\nCreated linked list elements are:\n");
 displaylist(start);
 printf("\nEnter the position at which you want to add new node\n");
 scanf("%d",&loc);
 insnode(start,loc);
 getch();
}
__________________
eXPerience is what a MAN learn's fROM.....

Last edited by ayush_chh; 28-09-2007 at 11:48 PM.
ayush_chh is offline