//Run & Compiled on MS Visual Studio Express 2013
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node{
int data;
struct node* link;
}*start=NULL;
int count = 0;//Counter to count no. of nodes
void insrtbeg(int num)
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
if (start==NULL)
{
newnode->data = num;
newnode->link=NULL;
start = newnode;
printf("\n\tNumber inserted Successfully\t\t(:(:(:");
}
else{
newnode->data = num;
newnode->link = start;
start = newnode;
printf("\n\tNumber inserted Successfully\t\t(:(:(:");
}
count++;
}
void delbeg()
{
struct node* p;
if (start == NULL)
{
printf("\n\tDeletion not possible");
exit(0);
}
else{
p = start;
start = p->link;
printf("\n\tDeleted no. is\t %d", p->data);
free(p);
}
count--;
}
static void reverse()
{
struct node* prev = NULL;
struct node* current = start;
struct node* next;
while (current != NULL)
{
next = current->link;
current->link = prev;
prev = current;
current = next;
}
start = prev;
}
void display()
{
struct node* p;
p = start;
if (start == NULL)
{
printf("\n\tList is empty !!!");
printf("\n\tTry Again ):\n");
return;
}
else{
while (p != NULL)
{
printf("\t%d", p->data);
p = p->link;
}
}
}
void xcount()
{
printf("\n\t No. of NODES are \t %d", count);
}
void insrtany(int num)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("\n\tOverflow ):):): \t Aborting!!!");
exit(0);
}
struct node*t =start;//initializing *t
while (t->data != num && t != NULL)
{
t = t->link;
}
if (t==NULL)
{
printf("\n\t\tNUMBER is not available in the list");
exit(0);
}
else{
temp->link = t->link;
temp->data = num;
t->link = temp;
printf("\n\tNumber inserted Successfully\t\t(:(:(:");
}
count++;
}
void main()
{
int ch,num;
while (1)
{
printf("\n\t1. Insertion\n\t2.Deletion\n\t3. Reverse\n\t4. Display\n\t5. No. of NODES\n\t6. Insert at any position\n\t7. Exit\n\t");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("Enter the no. to insert\n\t");
scanf("%d", &num);
insrtbeg(num);
break;
case 2: delbeg();
break;
case 3:reverse();
break;
case 4:display();
break;
case 5:xcount();
break;
case 6: printf("Enter the value to insert after the no. you want \n\t");
scanf("%d", &num);
insrtany(num);
break;
case 7: exit(0);
}
}
}