You are on page 1of 9

//assi4a1.

c
#include<stdio.h>
#include<stdlib.h>
#include "singly.h"
void main()
{
NODE *head1;
int ch,pos,no;
head1=LILI;
do
{
printf("\n1:create");
printf("\n2:display");
printf("\n3:append");
printf("\n4:search");
printf("\n5:insert");
printf("\n6:delete by value");
printf("\n7:delete by position");
printf("enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:create(head1);
break;
case 2:display(head1);
break;
case 3: printf("enter a number to append");
scanf("%d",&no);
append(head1,no);
break;
case 4:printf("enter a number to search");
scanf("\n%d",&no);
pos=search(head1,no);
if(pos==-1)
printf("number not found");
else
printf("Number found at %d",pos);
break;
case 5:printf("enter number and position to insert");
scanf("%d%d",&no,&pos);
insert(head1,no,pos);
break;
case 6: printf("enter a number to delete");
scanf("\n%d",&no);
deletevalue(head1,no);
break;
case 7:printf("enter a position ");
scanf("%d",&pos);
deletepos(head1,pos);
break;
}
Prepared By Ms. Varsha T.Dond
}while(ch!=8);
}

/********************singly.h*****************************/

/*a) Implement a list library (singlylist.h) for a singly linked list


with the above
six operations. Write a menu driven driver program to call the
operations.
singly.h file*/
#include<stdio.h>
#define LILI (NODE *)malloc(sizeof(NODE))
typedef struct node
{
int info;
struct node *next;
}NODE;

void create(NODE *head)


{
int n,count;
NODE *last,*newnode;
printf("how many nodes");
scanf("%d",&n);
last=head;
for(count=1;count<=n;count++)
{
newnode=LILI;
newnode->next=NULL;
printf("\n Enter the node data");
scanf("%d",&newnode->info);
last->next=newnode;
last=newnode;
}
}

void display(NODE *head)


{
NODE *temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
printf("%d\t",temp->info);
}

int search(NODE *head,int num)


{
NODE *temp;
int pos;
for(temp=head->next,pos=1;temp!=NULL;temp=temp->next,pos++)
{
if(temp->info==num)
Prepared By Ms. Varsha T.Dond
return pos;
}
return -1;
}

void append(NODE *head,int num)


{
NODE *newnode,*temp=head;
while(temp->next!=NULL)
temp=temp->next;
newnode=LILI;
newnode->info=num;
newnode->next=NULL;
temp->next=newnode;
}

void insert(NODE *head,int num,int pos)


{
NODE *newnode,*temp;
int i;
for(temp=head,i=1;(temp!=NULL)&&(i<=pos-1);i++)
{
temp=temp->next;
}
if(temp==NULL)
{
printf("position is out of range");
return;
}
newnode=LILI;
newnode->info=num;
newnode->next=temp->next;
temp->next=newnode;
}
void deletevalue(NODE *head,int num)
{
NODE *temp,*temp1;
for(temp=head;temp->next!=NULL;temp=temp->next)
{
if(temp->next->info==num)
{
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
return;
}
}
printf("element not found");
}

void deletepos(NODE *head,int pos)


Prepared By Ms. Varsha T.Dond
{
NODE *temp,*temp1;
int i;
for(temp=head,i=1;(temp->next!=NULL)&&(i<=pos-1);i++)
temp=temp->next;
if(temp->next==NULL)
{
printf("position is out of range");
return;
}
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
}

/******************************SetA2***********************/

/*b) Implement a list library (singlylist.h) for a singly linked list.


Create a
linked list, reverse it and display reversed linked list.
singly1.h*/

#include<stdio.h>
#define LILI (NODE *)malloc(sizeof(NODE))
typedef struct node
{
int info;
struct node *next;
}NODE;
void create(NODE *head)
{
int n,count;
NODE *last,*newnode;
printf("how many nodes");
scanf("%d",&n);
last=head;
for(count=1;count<=n;count++)
{
newnode=LILI;
newnode->next=NULL;
printf("\n Enter the node data");
scanf("%d",&newnode->info);
last->next=newnode;
last=newnode;
}
}
void display(NODE *head)
{
NODE *temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
Prepared By Ms. Varsha T.Dond
printf("%d\t",temp->info);
}
void reverse(NODE *head)
{
NODE *t1=head->next,*t2,*t3;
if(t1==NULL)
return;
t2=t1->next;
if(t2==NULL)
return;
t3=t2->next;
t1->next=NULL;
while(t3!=NULL)
{
t2->next=t1;
t1=t2;
t2=t3;
t3=t3->next;
}
t2->next=t1;
head->next=t2;
}*/

//assi4a2.c

#include<stdio.h>
#include<stdlib.h>
#include "singly1.h"
void main()
{
NODE *head1;
int ch,pos,no;
head1=LILI;
do
{
printf("\n1:create");
printf("\n2:display");
printf("\n3:reverse");
printf("enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:create(head1);
break;
case 2:display(head1);
break;
case 3: reverse(head1);
break;
}
}while(ch!=4);
}
Prepared By Ms. Varsha T.Dond
/*Ass4 Set B1
a) There are lists where insertion should ensure the ordering of data
elements. Since the elements are in ascending order the search can
terminate once equal or greater element is found. Implement a singly
linked list of ordered integers(ascending/descending) with insert,
search
and display operations.*/

#include<stdio.h>
#include<stdlib.h>
#define LILI (NODE *)malloc(sizeof(NODE))
typedef struct node
{
int info;
struct node *next;
}NODE;
void createsorted(NODE *head)
{
int n,count;
NODE *temp,*newnode;
printf("how many nodes");
scanf("%d",&n);
for(count=1;count<=n;count++)
{
newnode=LILI;
newnode->next=NULL;
printf("\n enter the data");
scanf("%d",&newnode->info);
for(temp=head;temp->next!=NULL;temp=temp->next)
if(newnode->info<temp->next->info)
break;
newnode->next=temp->next;
temp->next=newnode;
}
}
void display(NODE *head)
{
NODE *temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
printf("%d\t",temp->info);
}
int search(NODE *head,int num)
{
NODE *temp;
int pos;
for(temp=head->next,pos=1;temp!=NULL;temp=temp->next,pos++)
{
if(temp->info==num)
Prepared By Ms. Varsha T.Dond
return pos;
}
return -1;
}
void insert(NODE *head,int num)

{
NODE *newnode,*temp;
int i;
for(temp=head,i=1;(temp->next!=NULL)&&(temp->next->info<num);i++)
temp=temp->next;
newnode=LILI;
newnode->info=num;
newnode->next=temp->next;
temp->next=newnode;
}
void main()
{
NODE *head;
int no,pos,ch;
head=LILI;
head->next=NULL;
do
{
printf("\n1:create sorted lili");
printf("\n2:display");
printf("\n3:search");
printf("\n4:insert a number in sorted manner");
printf("\nenter a choice");
scanf("%d",&ch);
switch(ch)
{
case 1: createsorted(head);
break;
case 2:display(head);
break;
case 3:printf("\nenter number to search");
scanf("%d",&no);
pos=search(head,no);
if(pos==-1)
printf("\nelement not found");
else
printf("\nelement found at %d pos",pos);
break;
case 4:printf("\nenter a number to insert");
scanf("%d",&no);
insert(head,no);
break;
}
}while(ch!=5);
}
Prepared By Ms. Varsha T.Dond
/*b) There are lists where new elements are always appended at the end
of
the list. The list can be implemented as a circular list with the
external
pointer pointing to the last element of the list. Implement singly
linked
circular list of integers with append and display operations. The
operation append(L, n), appends to the end of the list, n integers
either
accepted from user or randomly generated.*/

#include<stdio.h>
#include<stdlib.h>
#define LILI (NODE *)malloc(sizeof(NODE));
typedef struct node
{
int info;
struct node *next;
}NODE;
void create(NODE *head)
{
int n,count;
NODE *last,*newnode;
printf("enter how many nodes");
scanf("%d",&n);
last=head;
for(count=1;count<=n;count++)
{
newnode=LILI;
newnode->next=head;
printf("\n enter the node data");
scanf("%d",&newnode->info);
last->next=newnode;
last=newnode;
}
}
void display(NODE *head)
{
NODE *temp;
for(temp=head->next;temp!=head;temp=temp->next)
{
printf("%d\t",temp->info);
}
}
void append(NODE *head,int num)
{
NODE *newnode,*temp,*temp1;
Prepared By Ms. Varsha T.Dond
int i;
for(temp=head;temp->next!=head;)
temp=temp->next;
newnode=LILI;
newnode->info=num;
temp1=temp->next;
newnode->next=temp1;
temp->next=newnode;
}
void main()
{
NODE *head;
int no,ch;
head=LILI;
head->next=head;
do
{
printf("\n1:create");
printf("\n2:display");
printf("\n3:append");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create(head);
break;
case 2: display(head);
break;
case 3: printf("enter a number to append");
scanf("%d",&no);
append(head,no);
break;
}
}while(ch!=4);
}

Prepared By Ms. Varsha T.Dond

You might also like