//WAP TO DEMONSTRATE THE OPERATIONS ON Single Linked List
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct sl_node
{
int m;
struct sl_node *link;
}*start=NULL;
typedef struct sl_node node;
void create()
{
int n,i,marks;
node *temp,*r;
printf("enter the no of elements of linked list\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the marks\n");
scanf("%d",&marks);
temp=(node*)malloc(sizeof(node));
temp->m=marks;
//temp=temp->link;
//}
if(start==NULL)
{
temp->link=NULL;
start=temp;
}
else
{
r=start;
while(r->link!=NULL)
{
r=r->link;
}
temp->link=r->link;
r->link=temp;
// }//while
} //else
}//create
}
void addbeg(int marks)
{
node* temp;
temp=(node*)malloc(sizeof(node));
temp->m=marks;
temp->link=start;
start=temp;
}
void addend(int marks)
{
node *temp,*r;
temp=start;
while(temp->link!=NULL)
{
temp=temp->link;
r=(node*)malloc(sizeof(node));
r->m=marks;
r->link=temp->link;
temp->link=r;
}
}
//add after specified node number
void addafter(int marks,int pos)
{
int i;
node* temp,*r;
temp=start;
for(i=1;i<pos;i++)
temp=temp->link;
r=(node*)malloc(sizeof(node));
r->m=marks;
r->link=temp->link;
temp->link=r;
}
void display()
{
node *temp=start;
if(temp==NULL)
printf("it is empty\n");
else
{
printf("sll follows\n");
while(temp!=NULL)
{
printf("%d->",temp->m);
temp=temp->link;
}//while
}//else
}//disp()
void del(int marks)
{
node *temp,*old;
temp=start;
while(temp!=NULL)
{
if(temp->m==marks)
{
if(temp==start)
start=start->link;
else
old->link=temp->link;
printf("%d is deleted from s.l\n",marks);
free(temp);
}
else
{
old=temp;
temp=temp->link;
} //else
} //while
printf("%d is not found in the s.l\n",marks);
}//del()
void destroy()
{
node *temp;
while(start!=NULL)
{
temp=start->link;
free(start);
start=temp;
}
printf("linked list is destroyed\n");
}
void menu()
{
printf("1.insert at the begining of ll\n");
printf("2.insert at the end\n");
printf("3.displaying \n");
printf("4.delete node\n");
printf("5.destroy the s.l\n");
printf("6.exit\n");
}
void main()
{
int ch,pos,marks,c,count;
node *start=NULL;
void create();
void addbeg(int);
// void addafter(int,int);
void addend(int);
void display();
void del(int);
void destroy();
clrscr();
create();
menu();
do
{
printf("enter ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("enter the marks\n");
scanf("%d",&marks);
addbeg(marks);
break;
}
case 2:{
printf("enter the marks\n");
scanf("%d",&marks);
addend(marks);
break;
}
/* case 3:{
printf("enter the marks\n");
scanf("%d",&marks);
addafter(marks);
break;
}
*/
case 3:{
display();
break;
}
case 4:{
printf("enter the marks\n");
scanf("%d",&marks);
del(marks);
break;
}
case 5:{
destroy();
break;
}
default :
exit();
}//switch()
}while(ch!=6);
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct ll
{
int data;
struct ll* link;
};
typedef struct ll node_type;
//creation of a linked list
node_type* sl_create()//this fun has no input but output first
{
node_type *temp,*t,*k,*first;
int x;
first=(node_type*)malloc(sizeof(node_type));
temp=first;
printf("enter the data fields of all the nodes\n");
while(scanf("%d",&x)!=EOF)
//EOF MEANS CONTROL +Z
{
t=(node_type*)malloc(sizeof(node_type));
t->data=x;//filing data field
temp->link=t;
temp=temp->link; //or t;//movin temp to latest node
}
temp->link=NULL;//fillin the link field of last node
k=first;//put k pointer to dummy node to delete it by free
first=first->link;//first come to action first node
free(k);//
return(first);
}
//displaying linked list
void ll_print(node_type *first)
{
node_type *temp;
for(temp=first;temp!=NULL;temp=temp->link)
{
printf("%d\t %u\n",temp->data,temp->link);//%u=address or position
}
}
//concatination two linked lists
node_type* concatinate(node_type* first1,node_type* first2)
{
node_type *temp;
if((first1==NULL)&&(first2==NULL))
{
return(NULL);
}
else
if((first1==NULL)&&(first2==NULL))
return(first2);
else if((first1!=NULL)&&(first2==NULL))
return(first1);
else
{ //putin a pointer temp &moving temp until temp link!=null
for(temp=first1;temp->link!=NULL;temp=temp->link)
{
;
}
temp->link=first2;
return(first1);
}
}
void main()
{
node_type *first1,*first2,*first3;
node_type* sl_create();
node_type* concatinate(node_type* first1,node_type* first2);
first1=sl_create();
first2=sl_create();
first3=concatinate(first1,first2);
ll_print(first3);
}
*******************************************************************
//count a linked list
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct ll
{
int data;
struct ll* link;
};
typedef struct ll node_type;
//creation of a linked list
node_type* sl_create()//this fun has no input but output first
{
node_type *temp,*t,*k,*first;
int x;
first=(node_type*)malloc(sizeof(node_type));
temp=first;
printf("enter the data fields of all the nodes\n");
while(scanf("%d",&x)!=EOF)
//EOF MEANS CONTROL +Z
{
t=(node_type*)malloc(sizeof(node_type));
t->data=x;//filing data field
temp->link=t;
temp=temp->link; //or t;//movin temp to latest node
}
temp->link=NULL;//fillin the link field of last node
k=first;//put k pointer to dummy node to delete it by free
first=first->link;//first come to action first node
free(k);//
return(first);
}
//write a c function to find the no of nodes (ie. lenght of link.list
int count(node_type *first)
{
node_type *temp;
int ctr;
ctr=0;
for(temp=first;temp!=NULL;temp=temp->link)
{
ctr++;
}
return(ctr);
}
//displaying linked list
void ll_print(node_type *first)
{
node_type *temp;
for(temp=first;temp!=NULL;temp=temp->link)
{
printf("%d\t %u\n",temp->data,temp->link);//%u=address or position
}
}
node_type * reverse(node_type *first)
{
node_type *p,*q,*r;
p=first;
q=NULL;
r=NULL;
while(p!=NULL)
{
r=q;//a pointer moved one step right
q=p;
p=p->link;
q->link=r;//addres of previous store NULL stored in r
//bcoz of this only links are changed
}
first=q;
return(first);
}
void main()
{
node_type *first;
node_type* sl_create();
int ans,count();
void ll_print();
first=sl_create();//linked list created
ans=count(first);
printf("no of nodes is %d\n",ans);
}
ll_print(first);//displaying
}
*********************************************************************************
//reversing a linked list
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct ll
{
int data;
struct ll* link;
};
typedef struct ll node_type;
//creation of a linked list
node_type* sl_create()//this fun has no input but output first
{
node_type *temp,*t,*k,*first;
int x;
first=(node_type*)malloc(sizeof(node_type));
temp=first;
printf("enter the data fields of all the nodes\n");
while(scanf("%d",&x)!=EOF)
//EOF MEANS CONTROL +Z
{
t=(node_type*)malloc(sizeof(node_type));
t->data=x;//filing data field
temp->link=t;
temp=temp->link; //or t;//movin temp to latest node
}
temp->link=NULL;//fillin the link field of last node
k=first;//put k pointer to dummy node to delete it by free
first=first->link;//first come to action first node
free(k);//
return(first);
}
//displaying linked list
void ll_print(node_type *first)
{
node_type *temp;
for(temp=first;temp!=NULL;temp=temp->link)
{
printf("%d\t %u\n",temp->data,temp->link);//%u=address or position
}
}
node_type * reverse(node_type *first)
{
node_type *p,*q,*r;
p=first;
q=NULL;
r=NULL;
while(p!=NULL)
{
r=q;//a pointer moved one step right
q=p;
p=p->link;
q->link=r;//addres of previous store NULL stored in r
//bcoz of this only links are changed
}
first=q;
return(first);
}
void main()
{
node_type *first,*reverse();
node_type* sl_create();
void ll_print();
first=sl_create();//linked list created
first=reverse(first);//function call
ll_print(first);//displaying
}
No comments:
Post a Comment