C Program of singly Linked List.


This is a simple program to create singly linked list of adding, deleting, and displaying the nodes from front.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
//Header files
#include <stdio.h>
#include <conio.h>
#include <math.h>

//structure for a node
struct node
{
int data;
struct node *next;
}*first,*temp,*dis;

/*first is the first node,temp is the temporary new node and display is a temporary node for displaying linked list*/

//this function creates OR adds a new node to the linked list
void create()
 {
  int d;               //Variable for storing the data in the node
  temp=(struct node*)malloc(sizeof(struct node));        

/*using memory allocation function to allocate memory for that node from availiblity stack(memory)*/

  printf("\nInsert the data:");
  scanf("%d",&d);
  temp->data=d;       //Storing data into the node
 if (first==NULL)   //if the list does not exist i.e 'First=NULL' than create first node
  {
  first=temp;
  first->next=NULL;
  }
  else                                         //else add that node to the front
  {
   temp->link=first;
   first=temp;
  }
  }

//Funciton to delete the first node
void listdel()
 {
  struct node *tem;    //temporary node
  int t;

  if(first==NULL)     //check for empty list
  {
  printf("\n\t\tLinked list empty");
  }

 else
   {
     tem=first;                           //allocating first node as tem node
     first=tem->next;//deleting the first node      
     t=tem->data;          
     free(tem);                 //freeing the memory allocated to that node
     printf("\ndata deleted is %d\n",t);
   } 
 }

//Function to display link list

void display()
 {
 struct node *tem;
 tem=first;
 printf("\n\nThe LINKED LIST IS AS BELOW:\n");
 while(tem!=NULL)
  {
  printf("%d-->",tem->data);
  tem=tem->next;
  }
 printf("NULL\n");
 }

//MAIN PROGRAM

void main()
 {
 int p;
 char c;
 clrscr();
 do
  {
  printf("\nWhat do u want to do:\n" );
  printf("\n1)create a node\n2)displaylinked list\n3)delete a node\n");
  scanf("%d",&p);
  switch(p)
   {
   case 1:create();
    break;
   case 2:display();
    break;
   case 3: listdel();
    break;
   default: printf("\n\t\tWrong choice\n");
   }
  printf("\nDo u wanty to continue?(y/n)\n");
  c=getch();
  }
 while(c=='y'||c=='Y');
}



Comments

Popular posts from this blog

Windows phone Wifi connectivity problem.

Create Excel file using Java: Apache POI - PART - 2: Line Chart