Вы находитесь на странице: 1из 52

/* Creation and Display of the Single Linked List*/

#include <stdio.h>

#include<conio.h>

#include<alloc.h>

#define NULL 0

struct node

int info;

struct node *add;

};

typedef struct node NP;

NP *first,*temp,*prev,*last;

void main()

int x;

char c;

clrscr();
while(1)

printf("\n M E N U\n");

printf("*************\n\n");

printf("1.Creation.\n");

printf("2.Display.\n");

printf("3.Exit.\n\n");

printf("Enter your choice..\n");

scanf("%d",&x);

switch(x)

case 1:

crea();

break;

case 2:

disp();

break;

case 3:

exit();

}
crea()

char y;

first=temp=NULL; // Asigning null to the structure pointers

do

// below statement is type casted as Node Pointer

temp=(NP *)malloc(sizeof(struct node));

temp->add=NULL; // Null value to the address part of temp node

printf("\nEnter the data to the node..");

scanf("%d",&temp->info);

if(first==NULL)

first=temp;

prev=temp;

else

prev->add=temp;

prev=temp;

printf("\nDo you want any more..");

flushall();

y=getch();
}while(y=='y' || y=='Y');

return;

disp()

clrscr();

prev=first;

printf("Your Single Linked list is \n");

printf("FIRST ->");

do

printf("%d ->",prev->info);

prev=prev->add;

}while(prev!=NULL);

printf("LAST");

return;

/* Creation , Display and Searching of Nodes*/


#include <stdio.h>

#include<conio.h>

#include<alloc.h>

#define NULL 0

struct node

int info;

struct node *add;

};

typedef struct node NP;

NP *first,*temp,*prev,*last;

void main()

int x;

char c;

clrscr();

while(1)

{
printf("\n M E N U\n");

printf("*************\n\n");

printf("1.Creation.\n");

printf("2.Display.\n");

printf("3.Searching\n");

printf("4.Exit.\n\n");

printf("Enter your choice..\n");

scanf("%d",&x);

switch(x)

case 1:

crea();

break;

case 2:

disp();

break;

case 3:

search();

break;

case 4:

exit();

}
crea()

char y;

first=temp=NULL; // Asigning null to the structure pointers

do

// below statement is type casted as Node Pointer

temp=(NP *)malloc(sizeof(struct node));

temp->add=NULL; // Null value to the address part of temp node

printf("\nEnter the data to the node..");

scanf("%d",&temp->info);

if(first==NULL)

first=temp;

prev=temp;

else

prev->add=temp;

prev=temp;

printf("\nDo you want any more..");

flushall();
y=getch();

}while(y=='y' || y=='Y');

return;

search()

int s;

int p=0;

int fl=0;

printf("Enter the info. to search..");

scanf("%d",&s);

prev=first;

do

p=p+1;

if(prev->info==s)

fl=1;

break;

else

prev=prev->add;

}
}while(prev!=NULL);

if(fl==1)

printf("\nMsg : Milgaya Milgaya at position %d!\n\n",p);

else

printf("\nMsg : Nahin mila yaar\n\n");

return;

disp()

clrscr();

prev=first;

printf("Your Single Linked list is \n");

printf("FIRST ->");

do

printf("%d ->",prev->info);

prev=prev->add;

}while(prev!=NULL);
printf("LAST");

return;

/* Creation , Display and Searching of Nodes, Find Sum of Info.*/

#include <stdio.h>

#include<conio.h>

#include<alloc.h>

#define NULL 0

struct node

int info;

struct node *add;

};

typedef struct node NP;

NP *first,*temp,*prev,*last;

void main()
{

int x;

char c;

clrscr();

while(1)

printf("\n M E N U\n");

printf("*************\n\n");

printf("1.Creation.\n");

printf("2.Display.\n");

printf("3.Searching\n");

printf("4.Find Sum\n");

printf("5.Exit.\n\n");

printf("Enter your choice..\n");

scanf("%d",&x);

switch(x)

case 1:

crea();

break;

case 2:

disp();

break;
case 3:

search();

break;

case 4 :

find_sum();

break;

case 5:

exit();

find_sum()

int s=0;

prev=first;

do

s=s+prev->info;

prev=prev->add;

}while(prev!=NULL);

printf("\n Result : Sum of all Info. is %d\n\n",s);

return;

crea()
{

char y;

first=temp=NULL; // Asigning null to the structure pointers

do

// below statement is type casted as Node Pointer

temp=(NP *)malloc(sizeof(struct node));

temp->add=NULL; // Null value to the address part of temp node

printf("\nEnter the data to the node..");

scanf("%d",&temp->info);

if(first==NULL)

first=temp;

prev=temp;

else

prev->add=temp;

prev=temp;

printf("\nDo you want any more..");

flushall();

y=getch();

}while(y=='y' || y=='Y');
return;

search()

int s;

int p=0;

int fl=0;

printf("Enter the info. to search..");

scanf("%d",&s);

prev=first;

do

p=p+1;

if(prev->info==s)

fl=1;

break;

else

prev=prev->add;

}while(prev!=NULL);
if(fl==1)

printf("\nMsg : Milgaya Milgaya at position %d!\n\n",p);

else

printf("\nMsg : Nahin mila yaar\n\n");

return;

disp()

clrscr();

prev=first;

printf("Your Single Linked list is \n");

printf("FIRST ->");

do

printf("%d ->",prev->info);

prev=prev->add;

}while(prev!=NULL);

printf("LAST");
return;

/* Creation , Display and Searching of Nodes, Find Sum of Info.*/

#include <stdio.h>

#include<conio.h>

#include<alloc.h>

#define NULL 0

struct node

int info;

struct node *add;

};

typedef struct node NP;

NP *first,*temp,*prev,*last;

void main()

int x;
char c;

clrscr();

while(1)

printf("\n M E N U\n");

printf("*************\n\n");

printf("1.Creation.\n");

printf("2.Display.\n");

printf("3.Searching\n");

printf("4.Find Sum\n");

printf("5.Exit.\n\n");

printf("Enter your choice..\n");

scanf("%d",&x);

switch(x)

case 1:

crea();

break;

case 2:

disp();

break;

case 3:

search();
break;

case 4 :

find_sum();

break;

case 5:

exit();

find_sum()

int s=0;

prev=first;

do

s=s+prev->info;

prev=prev->add;

}while(prev!=NULL);

printf("\n Result : Sum of all Info. is %d\n\n",s);

return;

crea()

char y;
first=temp=NULL; // Asigning null to the structure pointers

do

// below statement is type casted as Node Pointer

temp=(NP *)malloc(sizeof(struct node));

temp->add=NULL; // Null value to the address part of temp node

printf("\nEnter the data to the node..");

scanf("%d",&temp->info);

if(first==NULL)

first=temp;

prev=temp;

else

prev->add=temp;

prev=temp;

printf("\nDo you want any more..");

flushall();

y=getch();

}while(y=='y' || y=='Y');

return;

}
search()

int s;

int p=0;

int fl=0;

printf("Enter the info. to search..");

scanf("%d",&s);

prev=first;

do

p=p+1;

if(prev->info==s)

fl=1;

break;

else

prev=prev->add;

}while(prev!=NULL);

if(fl==1)

{
printf("\nMsg : Milgaya Milgaya at position %d!\n\n",p);

else

printf("\nMsg : Nahin mila yaar\n\n");

return;

disp()

clrscr();

prev=first;

printf("Your Single Linked list is \n");

printf("FIRST ->");

do

printf("%d ->",prev->info);

prev=prev->add;

}while(prev!=NULL);

printf("LAST");

return;

}
/* Counting repeation of the nodes of same info.*/

#include <stdio.h>

#include<conio.h>

#include<alloc.h>

#define NULL 0

struct node

int info;

struct node *add;

};

typedef struct node NP;

NP *first,*temp,*prev,*last;

void main()

int x;

char c;

clrscr();
while(1)

printf("\n M E N U\n");

printf("*************\n\n");

printf("1.Creation.\n");

printf("2.Display.\n");

printf("3.Searching\n");

printf("4.Repeat Count\n");

printf("5.Exit.\n\n");

printf("Enter your choice..\n");

scanf("%d",&x);

switch(x)

case 1:

crea();

break;

case 2:

disp();

break;

case 3:

search();

break;

case 4 :
repeat_count();

break;

case 5:

exit();

repeat_count()

int r=0;

int s;

printf("Ente the search element..");

scanf("%d",&s);

prev=first;

do

if(prev->info==s)

r++;

prev=prev->add;

}while(prev!=NULL);
clrscr();

printf("\n Result : There are %d times the element found in the


list\n\n",r);

crea()

char y;

first=temp=NULL; // Asigning null to the structure pointers

do

// below statement is type casted as Node Pointer

temp=(NP *)malloc(sizeof(struct node));

temp->add=NULL; // Null value to the address part of temp node

printf("\nEnter the data to the node..");

scanf("%d",&temp->info);

if(first==NULL)

first=temp;

prev=temp;

else

prev->add=temp;

prev=temp;
}

printf("\nDo you want any more..");

flushall();

y=getch();

}while(y=='y' || y=='Y');

return;

search()

int s;

int p=0;

int fl=0;

printf("Enter the info. to search..");

scanf("%d",&s);

prev=first;

do

p=p+1;

if(prev->info==s)

fl=1;

break;

else
{

prev=prev->add;

}while(prev!=NULL);

if(fl==1)

printf("\nMsg : Milgaya Milgaya at position %d!\n\n",p);

else

printf("\nMsg : Nahin mila yaar\n\n");

return;

disp()

clrscr();

prev=first;

printf("Your Single Linked list is \n");

printf("FIRST ->");

do

printf("%d ->",prev->info);
prev=prev->add;

}while(prev!=NULL);

printf("LAST");

return;

/* Inserting an node */

#include <stdio.h>

#include<conio.h>

#include<alloc.h>

#define NULL 0

struct node

int info;

struct node *add;

};

typedef struct node NP;


NP *first,*temp,*prev,*last;

void main()

int x;

char c;

clrscr();

while(1)

printf("\n M E N U\n");

printf("*************\n\n");

printf("1.Creation.\n");

printf("2.Display.\n");

printf("3.Searching\n");

printf("4.Inserting\n");

printf("5.Exit.\n\n");

printf("Enter your choice..\n");

scanf("%d",&x);

switch(x)

case 1:

crea();
break;

case 2:

disp();

break;

case 3:

search();

break;

case 4 :

ins();

break;

case 5:

exit();

ins()

int p;

int pos;

int i=0;

temp=NULL;

printf("SUB MENU (INS)\n\n");

printf("1.At First Position.\n");

printf("2.At Last Position.\n");


printf("3.At any internal position.");

printf("\nEnter your choice..");

scanf("%d",&p);

temp=(NP *)malloc(sizeof(struct node));

temp->add=NULL;

printf("Enter the Info. of the node..");

scanf("%d",&temp->info);

switch(p)

case 1:

temp->add=first;

first=temp;

break;

case 2:

prev=first;

do

prev=prev->add;

}while(prev->add!=NULL);

prev->add=temp;

prev=temp;

break;
case 3:

printf("\n Enter the position..");

scanf("%d",&pos);

prev=first;

while(i<pos-2)

i++;

prev=prev->add;

};

temp->add=prev->add;

prev->add=temp;

break;

return;

crea()

char y;

first=temp=NULL; // Asigning null to the structure pointers

do

// below statement is type casted as Node Pointer

temp=(NP *)malloc(sizeof(struct node));


temp->add=NULL; // Null value to the address part of temp node

printf("\nEnter the data to the node..");

scanf("%d",&temp->info);

if(first==NULL)

first=temp;

prev=temp;

else

prev->add=temp;

prev=temp;

printf("\nDo you want any more..");

flushall();

y=getch();

}while(y=='y' || y=='Y');

return;

search()

int s;

int p=0;

int fl=0;
printf("Enter the info. to search..");

scanf("%d",&s);

prev=first;

do

p=p+1;

if(prev->info==s)

fl=1;

break;

else

prev=prev->add;

}while(prev!=NULL);

if(fl==1)

printf("\nMsg : Milgaya Milgaya at position %d!\n\n",p);

else

printf("\nMsg : Nahin mila yaar\n\n");


}

return;

disp()

clrscr();

prev=first;

printf("Your Single Linked list is \n");

printf("FIRST ->");

do

printf("%d ->",prev->info);

prev=prev->add;

}while(prev!=NULL);

printf("LAST");

return;

/* Inserting and deletion of a node */

#include <stdio.h>

#include<conio.h>
#include<alloc.h>

#define NULL 0

struct node

int info;

struct node *add;

};

typedef struct node NP;

NP *first,*temp,*prev,*last;

void main()

int x;

char c;

clrscr();

while(1)

printf("\n M E N U\n");

printf("*************\n\n");
printf("1.Creation.\n");

printf("2.Display.\n");

printf("3.Searching\n");

printf("4.Inserting\n");

printf("5.Deletion\n");

printf("6.Exit.\n\n");

printf("Enter your choice..\n");

scanf("%d",&x);

switch(x)

case 1:

crea();

break;

case 2:

disp();

break;

case 3:

search();

break;

case 4 :

ins();

break;

case 5:

dele();
break;

case 6:

exit();

ins()

int p;

int pos;

int i=0;

temp=NULL;

printf("SUB MENU (INS)\n\n");

printf("1.At First Position.\n");

printf("2.At Last Position.\n");

printf("3.At any internal position.");

printf("\nEnter your choice..");

scanf("%d",&p);

temp=(NP *)malloc(sizeof(struct node));

temp->add=NULL;

printf("Enter the Info. of the node..");

scanf("%d",&temp->info);
switch(p)

case 1:

temp->add=first;

first=temp;

break;

case 2:

prev=first;

do

prev=prev->add;

}while(prev->add!=NULL);

prev->add=temp;

prev=temp;

break;

case 3:

printf("\n Enter the position..");

scanf("%d",&pos);

prev=first;

while(i<pos-2)

i++;

prev=prev->add;

};
temp->add=prev->add;

prev->add=temp;

break;

return;

dele()

int ch;

int nn;

int i=1;

int ninfo;

printf("\nDELETION SUB-MENU\n");

printf("*****************\n\n");

printf("1. First Node\n");

printf("2. Last Node\n");

printf("3. From any Position\n");

printf("4. As per node Info\n\n");

printf("Enter your choice..");

scanf("%d",&ch);

switch(ch)

case 1:
first=first->add;

break;

case 2:

prev=first;

while(prev->add->add!=NULL)

prev=prev->add;

prev->add=NULL;

break;

case 3:

printf("Which node you want to delete..");

scanf("%d",&nn);

prev=first;

while(i<nn-1)

prev=prev->add;

i++;

prev->add=prev->add->add;

break;

case 4:

printf("Enter the Info to which you want to delete..");

scanf("%d",&ninfo);
prev=first;

while(prev->add!=NULL)

if(prev->add->info==ninfo)

prev->add=prev->add->add;

break;

else

prev=prev->add;

break;

return;

crea()

char y;

first=temp=NULL; // Asigning null to the structure pointers

do
{

// below statement is type casted as Node Pointer

temp=(NP *)malloc(sizeof(struct node));

temp->add=NULL; // Null value to the address part of temp node

printf("\nEnter the data to the node..");

scanf("%d",&temp->info);

if(first==NULL)

first=temp;

prev=temp;

else

prev->add=temp;

prev=temp;

printf("\nDo you want any more..");

flushall();

y=getch();

}while(y=='y' || y=='Y');

return;

search()

{
int s;

int p=0;

int fl=0;

printf("Enter the info. to search..");

scanf("%d",&s);

prev=first;

do

p=p+1;

if(prev->info==s)

fl=1;

break;

else

prev=prev->add;

}while(prev!=NULL);

if(fl==1)

printf("\nMsg : Milgaya Milgaya at position %d!\n\n",p);

}
else

printf("\nMsg : Nahin mila yaar\n\n");

return;

disp()

clrscr();

prev=first;

printf("Your Single Linked list is \n");

printf("FIRST ->");

do

printf("%d ->",prev->info);

prev=prev->add;

}while(prev!=NULL);

printf("LAST");

return;

/* Sum and Avrage of the elements in a Linked List*/


#include<stdio.h>

#include<conio.h>

struct node

int info;

struct node *next;

};

typedef struct node NP;

NP *first,*prev,*temp;

void main()

int x;

int pos;

clrscr();

while(1)

printf("\nMAIN MENU\n");

printf("**********\n\n");

printf("1.Creation\n");
printf("2.Display\n");

printf("3.Info Sum\n");

printf("4.Info. Avg.\n");

printf("5.Node Count.\n");

printf("6.Search Node.\n");

printf("7.Exit\n\n");

printf("\nEnter the option");

scanf("%d",&x);

switch(x)

case 1:

creat();

break;

case 2:

disp();

break;

case 3:

inf_sum();

break;

case 4:

inf_avg();

break;

case 5:

node_count();

break;
case 6:

if((pos=search_node())>0)

printf("First occurance of the Node is at %d th. position",pos);

else

printf("Node nahin mila..");

break;

case 7:

exit(1);

inf_sum()

int s=0;

prev=first;

while(prev->next!=NULL)

s=s+prev->info;

prev=prev->next;

printf("\n\n Sum of all node is %d",s);


return;

inf_avg()

int s=0;

float a;

int cnt=1;

prev=first;

while(prev->next!=NULL)

s=s+prev->info;

prev=prev->next;

cnt++;

a=(float)s/cnt;

printf("\n\n Avrage of all node is %f",a);

return;

node_count()

int i=1;

prev=first;

while(prev->next!=NULL)

i++;
prev=prev->next;

printf("\n\n There are %d nodes in total.",i);

return;

search_node()

int s;

int i=1;

printf("\nEnter the info of the node to be searched..");

scanf("%d",&s);

prev=first;

while(prev->next!=NULL)

if(prev->info==s)

return i;

i++;

prev=prev->next;

return 0;

creat()

{
char y;

clrscr();

first=prev=temp=NULL;

do

temp=(NP*)malloc(sizeof(struct node));

temp ->next=NULL;

printf("\nEnter the no... ");

scanf("%d",&temp->info);

if (first==NULL)

first=temp;

prev=temp;

else

prev->next=temp;

prev=temp;

printf("Do you want any more node [Y/N].... ");

y=getch();

}while(y=='y'||y=='Y');

return;

disp()
{

clrscr();

prev=first;

printf("First : ");

while(prev!=NULL)

printf(" %d :",prev->info);

prev=prev->next;

printf(" Last");

return;

Вам также может понравиться