Data Stuctures Lab Manual

95  Download (0)

Full text

(1)

BIO-DATA

AIM:

To write a C-program to print the bio-data.

ALGORITHM:

Step:1 Start the program.

Step:2 Read name, father’s name, city, age. Step:3 Enter the details.

Step:4 Print the details. Step:5 Stop the program.

PROGRAM:

#include<stdio.h> #include<conio.h> void main()

{

char name, fathername, city; int age;

clrscr();

printf(“Enter the name:”); scanf(“%s”,name);

printf(“\n Enter the father’s name:”); scanf(“%s”,fathername);

printf(\n Enter the city”); scanf(“%s”,city);

printf(“\n Enter the age:”); scanf(“%d”,&age);

printf(“\n Name:%s”,Name);

printf(“\n Father’s Name:%s”,fathername); printf(“\n City:%s”,city);

printf(“\n Age:%d”,age); getch();

}

RESULT:

(2)

FLOWCHART:

OUTPUT:

Enter the name: R. Siva Kumar Enter the Father’s name: M. Rajavel. Enter the city: Tiruchengode.

Enter the age:21

Name: R. Siva Kumar. Father’s Name: M. Rajavel. City: Tiruchengode.

Age: 21

START

Read name, father name, city, age

Print name, father name, city, age

(3)

AVERAGE OF ‘N’ NUMBERS

AIM:

To write a C-program to find the average of ‘n’ numbers.

ALGORITHM:

Step:1 Start the program. Step:2 Read n.

Step:3 Enter the numbers.

Step:4 Use for loop with condition i < n find its sum. Step:5 Calculate the average.

Step:6 Stop the program.

PROGRAM: #include<stdio.h> #include<conio.h> void main() { int a[20],i,n; float avg,s=0;

printf(“Enter the number of element to be added”); scanf(“%d”,&n);

printf(“\n Enter the numbers”); for(i=0;i<n;i++) { Scanf(“%d”,&a[i]); } for(i=0;i<n;i++) { s=s+a[i]; } avg=s/n;

printf(“The average of %d numbers=%f”,n,avg); getch();

}

RESULT:

(4)

FLOWCHART:

OUTPUT:

Enter the number of element: 5 Enter the numbers: 7 8 9 5 4 The average of 5 no is : 6.600000 START Read n For(i=0;i<n;i++) Read a[i] For(i=0;i<n;i++) s=s+a[i] Avg=s/n Print avg stop

(5)

LARGEST AND SMALLEST NUMBER IN AN ARRAY

AIM:

To write a C-program to find the largest and smallest number in an array.

ALGORITHM:

Step:1 Start.

Step:2 Read ‘n’ number element of array.

Step:3 Save the numbers in ascending order using temp’t’. Step:4 Print the first element (i.e) smallest end largest elements. Step:5 Stop. PROGRAM: #include<stdio.h> #include<conio.h> void main() { int n,i,j,a[15],t; clrscr();

printf(“Enter the numbers to be sort:”); scanf(“%d”,&n); for(i=1;i<=n;i++) { scanf(“%d”,&a[i]); } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } }

printf(“The minimum value is %d”,a[1]); printf(“The maximum value is %d”,a[n]); getch();

}

RESULT:

Thus the C-program to print the largest and smallest of ‘N’ numbers is executed and the output is verified.

(6)

FLOW CHART:

OUTPUT:

Enter the numbers to be sort:5 1 5 3 4 2

The minimum value is 1 The maximum value is 5

start Read n For(i=1;i<=n;i++) Ifa[i]>a[j ] I=a[i] A[i]=a[j] A[j]=n Print minimum value a[i] stop For(j=i+1;j<=n;j++)

Print maximum value a[j]

(7)

SALES REPORT OF GIRLS

AIM:

To write a ‘c’ program to find the total value of each item by each girl.

ALGORITHM:

Step 1. Start the program

Step 2. Read the no of items and girls.

Step 3.Use for loop to get the value of sales and value of each item and grand total. Step 4. Print the value of sales, value of each item and grand total.

Step 5. Stop PROGRAM: #include<stdio.h> #include<conio.h> void main() { int b[10][10],m, a, s=0,c=0; clrscr(); printf(“ Enter m, n”); scanf(“%d%d”,&m,&n); for(i=0;i<m;i++) { For(j=0;j<n;j++) { Scanf(“%d”,&b[i][j]); a=0; a=a+b[i][j];

printf(“Total value of each item by %d girl=%d”,i++,c); } For(i=0;i<n;i++) { For(j=0;j<m;j++) { c=0; c=a+b[i][j];

printf(“ Total value of sales by each girl=%d”,c); }

s=s+c; }

printf(“Grand total of each item sold by each girl=%d”,s); getch();

}

RESULT:

Thus the ‘c’ program to print the sales report of girls executed and output was verified.

(8)

FLOW CHAR T: START Read m,n a=a+b[i][j] for(i=0;i<m;i++) for(i=0;i<n;i++) Print a for(j=0;j<n;j++) for(i=0;i<n;i++) c=c+b[i][[j] Print c A FLOWCHART:

(9)

OUTPUT:

Enter m,n: 2 2

Enter the value of each item by 1 girl=8 Total value of each item by 2 girl=8 Total value of sales by 1 girl=5 Total value of sales by 2 girl=11

Grand total of each item sold by all girl=16

s=s+c

A

STOP

(10)

STUDENT DETAILS

AIM:

To write a ‘C’ program to print the mark details of n students.

ALGORITHM:

Step 1. Start the program

Step 2. Read the marks of srudents using arrays and loops. Step 3. Find the average of 3 students by using arrays and loops. Step 4. Find the average of student .

Step 5. Print average.

Step 6. Find the overall average. Step 7. Print the overall average. Step 8. Stop. PROGRAM: #include<stdio.h> #include<conio.h> void main() { int a[5][5], i, j; float avg, b, s, t=0;

clrscr(); for(i=1;i<4;i++) {

printf(“\n Enter the mark for %d student”,i); for(j=1;j<6;j++) { scanf(“%d”,a[i][j]); s=s+a[i][j]; } avg=s/5; Ss0;

printf(“ The no %d student avg is %f”,I,avg); t=t+avg;

} b=t/3;

printf(“The overall avg is %f”,b); getch();

}

RESULT:

(11)

FLOWCHART:

START

Read the marks

S=0;t=0 For(j=0;j<4;j++) For(j=1;j<6;j++) s=s+a[i][j] Avg=s/5 A B

(12)

OUTPUT:

Enter the mark for 1 student:50 50 50 50 The avg of 1 student is 50

Enter the mark for 2 student:70 70 70 70 The avg of 2 student is 70

Enter the mark of 3 student: 80 80 80 80 The avg of 3 student is 80

Overall avg is 66.66 b=avg/5 t=t+avg Print avg STOP A Write b B

(13)

FACTORIAL OF N NUMBERS

AIM:

To find the factorial of given numbers using recursive function in ‘c’ program.

ALGORITHM:

Step 1: Start the program Step 2: read n

Step2: Find the factorial of n numbers using recursive function Step 4: Stop the program

PROGRAM:

#include<stdio.h> #include<conio.h> int fact (int n) void main() {

int n; clrscr();

printf(“ enter the no:”); scanf(“%d”,&n);

printf(“the factorial of %d is %d” ,n ,fact(n)); getch();

}

int fact (int n) {

int f=1; if(n<0); {

printf(“cannot find factorial”); } else if(n>1) { f=n*fact n-1; return(f); } else return (1); } RESULT:

Thus the ‘c’ program to find the factorial of given number using recursion function is executed and output was verified.

(14)

FLOW CHART: start fact Read n Print fact stop Int fact(int n) If n < 0 Print

Cannot find factorial

Return f Return 1 If (n> 1) F=n*fact(n-1) Main program Sub program no yes

(15)

OUTPUT:

Enter the value of n: 5 The factorial of 5 is 120

(16)

FIBONACCI SERIES

AIM:

To write a C-program to print a Fibonacci series.

ALGORITHM:

Step:1 Start the program Step:2 Read the value of 'n'

Step:3 Executing he function int fib(int n) Step:4 Find the Fibonacci series

Step:5 End program.

PROGRAM: #include<stdio.h> include<conio.h> void main() { int n; int fib(int n); printf("enter n/n"); scanf("%d",&n); fib(n); } int fib(int n); { int a=0;b=1;f,i,x; printf("\n%d\t%d",a,b); for(i=0;i<=n:i++) { x=a; a=b; b=x+a; f=printf("\t%d",b); } return f; } RESULT:

(17)

OUTPUT:

ENTER THE VALUE OF N; 5 0 1 1 2 3

start

Read n

F=fib(int n)

Print n

Stop

Int fib (int n)

If

n<

=i1

Return n

stop

a=0

b=1

x=a

a=b

b=a+b

For(i=2;i<n;i+

+)

next i

stop

yes

Sub program Main program

no

FLOWCHART:

(18)

TOWER OF HANOI

AIM:

To write a C-program for solving tower of Hanoi.

ALGORTHIM:

Step:1 Start the program.

Step:2 If n==1,move single disc from A to C and stop.

Step:3 Move the top = n-1 disc from A to B using C as auxiliary Step:4 Move remaining disc from A to C

Step:5 Move the n-1 disc from B to C using A as auxiliary Step:6 Stop the program.

PROGRAM.

#include<stdio.h> #include<conio.h>

void hanoi(int n,char initial,char final,char temp) {

if(n==1) {

printf("move disc one from middle %c\n",initial,final); return;

hanoi(n-1;initial, final,temp);

printf("move disc %d from %c \n"n,initial,final); hanoi(n-1,temp,final,initial);

printf("move disc %d from %c \n",n,temp, final); }

void main() {

int n; clrscr();

printf("enter the no of disc to move"); scanf("%d",&n);

hanoi(n,'A','B','C'); getch();

}

RESULT:

(19)

FLOWCHART: Main program: START Read n Tower(n,’A’,’B’,’C,) STOP Tower(int,char,char,char) if(n== 1) Tower(n-1,from reg auxillary to reg)

Print move disc from reg to reg

Print move disc n from reg to reg

Tower(n-1,aux reg to reg from reg)

return

(20)

OUTPUT:

enter the no of disc to move 3

move disc middle from A to C move disc 2 from A

move disc from middle C to B move disc 1 from C to B move disc 3 from A

move disc from middle C to A move disc 2 from C

(21)

PALINDROME USING STACK

AIM:

To write a C-program to check whether the given string is palindrome or not.

ALGORITHM:

Step:1 Get the string.

Step:2 Find the string length. Step:3 Push each character to stack

Step:4 Retrieve each character from stack in reverse order and store in new variable

Step:5 Compare two characters and if two strings are equal say the string is palindrome otherwise the string is not palindrome.

PROGRAM:

#include<stdio.h> #include<conio.h> #include<string.h> static int top=0 static char stack[100] void main()

{

void push(char); char(pop);

char stack[100],char str[100],l; int top i,j,l,c;

clrscr();

printf("\n Enter the string"); scanf("%s",str); l=strlenght(str); for(i=0;i<l;i++) push(str[i]); for(i=0;i<l;i++) { k=loop; if(str[i]!=k) {

printf("The string is not palindrome"); end;

} else

printf(“The string is Palindrome”); } getch(); } void push(char c) { stack[top]=c; top=top++; }

(22)

char pop() { top=top-1; return(stack top) } RESULT:

(23)

OUTPUT:

Enter the string

Madam

The string is Palindrome

start

Read the

string

L=strlen[i]

Print not

palindrome

Print palindrome

For(i=1;i<=l;i+

+)

For(i=1;i<=l;i+

+)

If

I=k

stop

no

yes

FLOWCHART:

(24)

CONVERSION OF PREFIX TO POSTFIX

AIM:

To write a C program to convert prefix to postfix expression..

ALGORITHM:

Step:1 Start the program. Step:2 Read the given string.

Step:3If the input symbol read is ‘c’ push it into the stack.

Step:4 If the input symbol read is an operand then place it in the expression Step:5 If the input symbol read is an operator,then

a)check the precedence of the operator read. If it has a higher precedence then remove it from the stack and place it in the postfix expression.

Repeat 5a) till operator in the stack has a higher precedence than that being read.

Step:6 If the input symbol read is closing parenthesis then pop all the operators from the stack and place them in the postfix expression till the opening parenthesis is encountered.

Step:7 Stop the program.

PROGRAM: #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> #include<process.h> #include<string.h> char pre[40],post[40]; int top=0,st[20]; void postfix(); void push(int); char pop(); void main() { clrscr();

printf(“Enter the expression”); scanf(“%s”,&pre); postfix(); getch(); } void postfix() { int i,j=0; fro(i=0;pre[i]!=’\0’;i++) { switch(inf[i]) { case ‘+’: while(st[top]>=1) post[j++]=pop(); push(1); break;

(25)

case ‘-’: while(st[top]>=1) post[j++]=pop(); push(2); break; case ‘*’: while(st[top]>=3) post[j++]=pop(); push(3); break; case ‘/’: while(st[top]>=3) post[j++]=pop(); push(4); break; case ‘^’: while(st[top]>=1) post[j++]=pop(); push(5); break; case ‘(’: push(0); break; case ‘)’: while(st[top]!=0) post[j++]=pop(); top--; break; default: post[j++]=pre[i]; } } while(top>0) post[j++]=pop();

printf(“\nThe postfix expression is”,post); }

void push(int ele) { top++; st[top]=ele; } char pop() { int el; char e; el=st[top] ; top--; switch(el) { case 1: e= ‘+’; break case 2: e= ‘-’;

(26)

break; case 3: e= ‘*’; break; case 4: e= ‘/’; break; case 5: e= ‘^’; break; } return(e); } RESULT:

Thus a C program to convert the prefix expression to postfix expression was written, executed and the output was verfied.

(27)

FLOWCHART: START Read input Pre to post STOP Pre to post while input()! =10 if operat ed op2=pop() op1=pop() print postfix exp() i++

Print the expression yes

no

Sub-Program Main program

(28)

OUTPUT:

Enter the expreesion: +ab The postfix expression is ab+

(29)

IMPLEMENTATION OF STACK

AIM:

To write a C- program to implement the element of stack.

ALGORITHM:

Step 1: Start.

Step 2: Define structure & size of stack. Step 3: Initialize st.top = -1.

Step 4: Read choice for 1.push 2.pop 3.display 4.Exit. Step 5: If choice is 1,then perform push operation. If choice is 2,then perform pop operation. If choice is 3,then perform display operation. If choice is 4,then perform Exit.

Step 6: Read the choice Y_continue or N_not continue. Step 7: If Y_continue else exit.

Step 8: STOP. PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define size 5 struck stack { int s[size] int top; } st; int st full() { If (st.top>=size-1) return 1; else return 0; }

void push(int item) { st.top++ st.s[st.top]=item; } int stempty() { if(st.top==-1) return 1; else return 0; } int pop() { int item: item = st.s[st.top];

(30)

st.top--; return(item); } void display() { int i; If (stempty())

printf(" \n stack is empty"); else { for(i=st.top;i>=0;i++) printf(" \n %d",st.s[i]); } } void main(void) { int item,choice; char ans; st.top =-1; clrscr();

printf(" \n\t\t Implementation of stack"); do

{

printf("\n Main menu");

printf("\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit"); printf("Enter your choice");

scanf("%d",& choice); switch (choice)

{ case 1: {

printf("\n Enter the item to be pushed"); scanf("%d",&item):

if(st.full())

printf("\n stack is full"); else push(item); } break; case 2: { If (stempty())

printf("\n Empty stack!underflow!!"); else

{

item = pop();

printf("\n the popped element is %d",item); } break; case 3: display(); break; case 4: exit();

(31)

}

printf("\n Do you want to continue"); ans=getchar();

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

}

RESULT:

Thus the C-program to implement the element of stack using arrays was done and the output was verified.

(32)

FLOWCHART:

switch

(choic

e)

start

print

1.push

2.pop

3.display

display()

top=null

read choice

pop()

push()

if

choice<=

y

stop

yes

no

Main

program:

(33)

OUTPUT: Implementation of stack Main menu 1.Push 2.Pop 3.Display 4.Exit

Enter your choice 1

Enter the item to be pushed 3 Do you want to continue y Enter your choice 2

The popped element is 3 Do you want to continue y The stack is empty

Do you want to continue y Enter your choice 4

push()

read item

temp=malloc(size of stack)

temp->data=item

temp->next=top

return

Sub-program

(34)

IMPLEMENTATION OF QUEUE

AIM:

To write a C-program to implement the element of queue.

ALGORITHM:

Step 1: Start.

Step 2: Define structure & size of queue. Step 3: Initialize Q.rear=0,Q.front=-1.

Step 4: Read choice for 1.insert 2.delete 3.display 4.Exit. Step 5: If choice is 1,then perform insert operation. If choice is 2,then perform delete operation. If choice is 3,then perform display operation. If choice is 4,then perform Exit.

Step 6: Read the choice Y_continue or N_not continue. Step 7: If Y_continue else exit.

Step 8: Stop. PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define size 5 struck queue { int Q[size] int front,rear; } Q; int Q full() { If (Q.rear>=size-1) return 1; else return 0; }

void insert(int item) { st.rear++; Q.que[Q.rear]=item; } int Qetempty() { If(Q.rear==-1) return 1; else return 0; } int delete() {

(35)

int item: item = Q.que[Q.rear]; st.front++; return(item); } void display() { int i; If (stempty())

printf(" \n queue is empty"); else { for(i=Q.front;i<=Q.rear;i++) printf(" \n %d",Q.que[i]); } } void main(void) { int item,choice; char ans; Q.front==0; Q.rear==0; clrscr();

printf(" \n\t\t Implementation of queue"); do

{

printf("\n Main menu");

printf("\n 1.Insert \n 2.Delete \n 3.Display \n 4.Exit"); printf("Enter your choice");

scanf("%d",& choice); switch (choice)

{ case 1: {

printf("\n Enter the item to be inserted"); scanf("%d",&item):

if(Q.full())

printf("\n queue is full"); else insert(item); } break; case 2: { If (Qetempty())

printf("\n Empty queue!underflow!!"); else

{

item = delete();

printf("\n the deleted element is %d",item); }

} break; case 3:

(36)

display(); break; case 4: exit(); }

printf("\n Do you want to continue"); ans=getchar();

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

}

RESULT:

Thus the C-program to implement the element of stack using arrays was done and the output was verified.

(37)

FLOWCHART: Main Program:

(38)
(39)

OUTPUT: Implementation of queue Main menu 1.Insert 2.Delete 3.Display 4.Exit

Enter your choice 1

Enter the item to be inserted 4 Do you want to continue y Enter your choice 2

The deleted element is 4 Do you want to continue y The queue is empty

Do you want to continue y Enter your choice 4

(40)

SINGLY LINKED LIST

AIM:

To write a ‘C’ program to perform operations like create, insert,delete, seach,and display in a singly linked list.

ALGORITHM:

Step 1. Start the program.

Step 2. To perform various operations read the value for choice. Step 3. If the choice is 1. create function is called to create to the list. a) Assign temp=NULL and Flag=TRUE and also ans=’Y’

b) Read the value for val and allocate memory for the new node. A new node is created and the above procedure is followed until ans=’Y’.

step 4. If the choice is 2, display() is executed to display the list. a) Assign temp=bead.

b) If temp =NULL, print “ the list is empty”. c) print the list under it until temp is NULL.

Step 5. If the choice is 3 search() is executed to search the element in the list. Step 6. If the choice is 4 insert() is executed to insert an element.

Step 7.If the choice is 5 delete () is executed

Step 8. If we want to exit ,the choice is 6 and the default statement is” Invalid choice,try again”. Step 9. Stop the program.

PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 typedef struct SLL { int data; struct SLL*next; }node; Node*create(); void main() {

int choice, val; char ans; node*head; void display(node*); node*search(node*,int); void insert(node*) void delete(node**); head=NULL; do { clrscr();

(41)

printf(“\n 1. create \n 2. display\n 3. search\n 4. insert\n 5. delete \n 6.quit”); printf(“ \n Enter your choice”);

scanf(“%d”,& choice); switch(choice) { case1: head=create(); break; case 2: display (head); break; case 3:

printf(“ enter the element you want to search”); scanf(“%d”,val); scanf(“head,val); break; case 4: insert(head); break; case 5: dele (head); break; case 6: exit(0); default; clrscr();

printf(“ Invalid choice,try again”); getch(); } } while(choice!=t); } node*create() { node*temp,*new,*head; int val, flag;

char ans=’Y’; node*get_node(); temp=NULL; flag=TRUE; do { {

printf(“\n Enter the element:”); scanf(“%d”,&val);

new=get_node(); If(new ==NULL)

printf(“ \n memory is not allocated”); new->data=val; if(flag) { head= new; temp=head; flag=FALSE;

(42)

} else { temp->next=new; temp=new; }

printf(“\n do you want to enter more element?(y/n)”); ans=getche();

}

while (ans==’Y’);

printf(“The singly linked list is created”); getch(); clrscr(); return head; } node*get_node() { node*temp; temp=(node*)malloc(size of node)); temp->next=NULL; return temp; } void display(node*head) { node*temp; temp=head; if(temp==NULL) {

printf(“\n The list is empty”); getch(); clrscr(); return; } while(temp!=NULL) } printf(“%d->”,temp->data); temp=temp->next; } printf(“NULL”); } node*searchnode(node*head,int key) { node*temp; int found; temp=head; if(temp==NULL) {

printf(“The linked list is empty”); getch(); clrscr(); return NULL; } found=FALSE; while(temp!=NULL&&!found){

(43)

if(found) {

printf(“The element is present in the list\n”); getch();

return temp; }

else {

printf(“The element is not present in the list\n”); getch();

return NULL; }

}

void insert(node *head) { node *temp,*New; int val; temp=head; if(temp==NULL) {

printf(“\nInsertion is not possible”); getch();

return; } clrscr();

printf(“\nEnter the elements”); scanf(“%d”,&val);

New=(node*)malloc(sizeof(node)); if(New==NULL)

printf(“\nMemory is not allocated”); Newdata=val; Newnext=NULL; Newnext=tempnext; tempnext=New; printf(“\nElement is inserted”); getch(); } void del() { int t,pos,j;

printf(“\n want to delete any data?(y/n)”); scanf(“%c”,j);

while(j==’y’) {

list=head;

printf(“enter the position to be deleted”); fflush (stdin); scanf(“%d”,&pos); if(pos==1) { head=listlink; else

(44)

{ for(i=1;i<pos-1;i++) list=listlink; if(pos==n) listlink=NULL; else { prev=list; list=listlink; next=listlink; prevlink=next; } } n--;

printf(“want delete any data?(y/n)”); fflush (stdin);

scanf(“%c”,&j); }

}

RESULT:

Thus the c-program for singly linked list had been written and the operations such as creation ,deletion, insertion, display was performed.

(45)

FLOWCHART:

start

Switch

(choic

e)

Create()

While

Ch<=

0

stop

Read

choice

Delete()

display()

view()

Start=null

Main Program

yes

(46)

view() return list=head if list=NUL L print roll no name print roll no name

print list is empty

for(list!=NULL;list=list->link)

next list

(47)

OUTPUT:

Program for various operations of linked list 1. create

2. display 2. search

3. insert an item in the list 4. delete an item in the list 5. exit

Enter your choice:1 Enter the element 5

Do you want to enter more element=Y Enter the element 6

Do you want to enter more element=N 1. create

2. display 3. search

4. insert an item in the list 5. delete an item in the list 6. exit

Enter your choice : 3

Enter the element you want to search: 5 The element is present

(48)

IMPLEMENTATION OF DOUBLY LINKED LIST

AIM:

To write and execute a program to implement the operation in double linked list.

ALGORITHM:

Step:1 Start. Step:2 Read.

Step:3 If c==1,create new node.

Step:4 If c==2,insert new node at specific location. Step:5 If c==3,delete a particular node.

Step:6 If c==4,display the list. Step:7 If c==5,exit. Step:8 Stop. PROGRAM: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int data; struct node*next,*prev; } *New,*new1,*temp,*start,*dummy; void add(void) struct node*get_node(); void display(void); void delete(void); int find(int); int first=1; void main() { char ans; int choice,num,found=0; struct=NULL; do{ clrscr();

printf("Program for double link list");

printf("\n1.insert\n2.delete\n3.display\n4.searching\n5.exit"); printf("\n enter your choice:");

scanf("%d",&choice); switch(choice); { case 1:add() break; case 2:delete() break; case 3:display()

(49)

break;

case 4:printf("enter no to be search:"); scanf("%d",&num); temp=start; while((temp!=NULL)&&(found==0)) found=find(num); if(found) printf("\n no is present"); break; case 5:exit(0) }

printf("\n do u want to continue?"); ans=getch(); } while(ans=='y'||ans=='Y') getch(); } void add(void) { clrscr(); New=get_node();

printf("\n\n\tEnter the element:"); scanf("%d",&New->data); if(first==1) { start=New; first=0; } else { dummy=start; while(dummy->next!=NULL) dummy=dummy->next; dummy->next=New; New->pre=dummy; } } struct node*get_node() { new1=(node*)malloc(sizeof(struct node)); new1->next=NULL; new1->prev=NULL; return(new1); } void display(void) { clrscr(); temp=stat; if(temp==NULL);

printf("\n double linked list is empty"); else

{

while(temp!=NULL) {

(50)

printf("%d",temp->data); temp=temp->next; } printf("NULL"); } getch(); }

int find(int num) { if(temp->data==num) return(1) else temp=temp>next; return 0; } void delete(void) { int num,flag=0; int found; int least=0; clrscr(); temp=start; if(temp==NULL)

printf("\n sorry dll not created"); else

{

printf("\n Enter the no of deleated"); scanf("%d",&num); while((flag==0)&&temp!=NULL)) { found=find(num); flag=found; } if(found==0)

printf("\n no not found"); else { if(temp=start) { start=start->next; temp->next=NULL; start->next=NULL; free(temp); getch();

printf("\n the starting node is deleted"); } else { if(temp->next==NULL) last=1; else last=0; (temp->next)->prev=temp->prev; (temp->prev)->next=temp->next;

(51)

temp->prev=NULL; free(temp);

if(last)

printf("\n the last node is deleted"); else

printf("\n the intermediate node is deleted"); }

} } }

RESULT:

Thus,a program to implement double linked list was written,executed and output was verified.

(52)

START

Print program for various operation on doubly linked list 1.create 2.display 3.search 4.insert 5.delete 6.exit Read choice Switch choice while (choice!=6) STOP

create() display() search() insert() delete() exit()

FLOWCHART:

(53)

OUTPUT:

Program for double link list Insert

Delete Display Searching Exit

Enter your choice:1

f data=num i next=null rprev=r qnext=r

(54)

CIRCULARLY LINKED LIST

AIM:

To write a program to implement circularly linked list and perform the operations such as creation, insertion, deletion and view

ALGORITHM:

Step 1.Start the program.

Step 2.Adding the element in the circularly linked list is achieved by addcirq();

Creating a new node using malloc. Getting the item in the data field of the node. Linking the next field to the list.

Step 3.Removing the node from the list using delcirq() operation.

If the queue is empty print that the list is empty. Link the data of the node to the temp node and free the temp node..

Step 4.View the list using display operation.. Step 5.Using print function print all the data. Step 6.Stop the program.

PROGRAM: #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data;

struct node *link; };

void addcirq(struct node**,struct node**,int); int delcirq(struct node**,struct node**); void cirq_display(struct node*);

void main()

struct node *front,*rear; front=rear=NULL; addcirq(&front,&rear,10); addcirq(&front,&rear,17); addcirq(&front,&rear,18); addcirq(&front,&rear,23); addcirq(&front,&rear,12); clrscr();

printf(“\n Before deletion:\n”); cirq_display(front);

delcirq(&front,&rear); delcirq(&front,&rear); printf(“\n After deletion:\n”); cirq_display(front);

getch(); }

(55)

Void addcirq (struct node **f,struct node **r,int item) {

struct node *q;

q=malloc (sizeof(struct node)); qdata+=item; if(*f==null) *f=q; else (*r)link=q; (*r)q; (*r)link=(*f); }

int delcirq (struct node **f,struct node **r) { struct node *q; int item; if(*f==null) printf(“queue is empty”); else { if(*f==*r) { item=(*f)data; free(*f); *f=null; *r=null; } else { q=*f; item=qdata; *f=(*f)link; (*r)link=*f; free(q); } return(item); } return Null; }

void cirq_display(struct node *f) {

struct node *q=f,*p=Null; while(q1=p) { printf(“%d”,qdata); qqlink; p=f; } } RESULT:

(56)

Thus a C program to implement circularly linked list was written, executed and the output was verified. FLOW CHART:

start

Switch

(choic

e)

Create()

While

Ch<=

0

stop

Read

choice

Delete()

display()

view()

Start=null

Circularly linked list

yes

(57)

BINARY TREE

AIM:

To write a C-program to create the simple binary tree and recursive traversal.

ALGORITHM:

Step:1 Start the program. Step:2 Read the choice

Step:3 If the choice is one go to create ( ) function. Step:4 If the choice is two go to inorder ( ) function. Step:5 If the choice is three go to preorder ( ) function. Step:6 If the choice is four go to postorder ( ) function. Step:7 If the choice is five go to exit the program.

Sub – function:

Create ( )

i) Node = (node *) malloc (size of (node)); ii) Assign the right and left value as the NULL Insert ( )

i) Read where to insert the data (right/left). ii) If the value is ‘r’||’R’ assign root->right=new. Inorder ( )

i) Check the condition if(temp !=NULL)

ii) First print the left root and data and right root. Preorder ( )

i) Check the condition if (temp! = NULL)

ii) First print the data and then left root and right root. Postorder ( )

i) Check the condition if (temp! = NULL)

ii) First print the right root and then left root at last print the data.

PROGRAM:

#include<stdio.h> #include<conio.h>

typedef struct bin {

int data;

struct bin *left; struct bin *right; }node;

void insert (node *,node*); void inorder (node *); void preorder (node *); void postorder (node *); nod *get_node( );

(58)

void main( ) { int choice; char ans = ‘n’; node *new,*root; root = null; clrscr( ); do {

printf (“\n program for implementing simple binary tree”); printf (“\n 1.create”);

printf (“\n 2.inorder”); printf (“\n 3.preorder”); printf (“\n 4.postorder”); printf (“\n 5.exit”);

printf (“\n \t Enter Your Choice”); scanf (“%d”,&choice); switch (choice) { case 1: root = null; do { new=get_node( );

printf(“\n enter the element”); scanf(“%d”,& new->data); if(root = = null)

root= new; else

insert (root, new);

printf (“\m do you want to enter more elements?(y/n)”); ans = getch();

}

while (ans==’y’|| ans==’y’) clrscr ( );

break; case 2:

if (root ==null)

printf (“tree is not created!”); else

inorder (root); break;

case 3:

if (root ==null)

printf (“there is not created!”); preorder (root);

break; case 4:

if (root = = null)

printf (“ tree is not created!”); postorder (root);

(59)

} }while(choice!=5); } node *get_node ( ) { node *temp;

temp = (node *) malloc (size of(node)); temp ->left = null;

temp->right = null; return temp; }

void insert (node *root, node *new) {

char ch;

printf(“\n where to insert left/right of %d”, root->data); ch=getch( ); if((ch==’r’) || (ch==’r’)) { if(root->right == null) { root->right=new; } else insert (root->right,new); } else { if (root->left ==null) { root->left=new; } else insert (root->left,new); } }

void inorder (node *temp) { if (temp!=null) { inorder (temp->left); printf (“ %d”,temp->data); inorder (temp->right); } }

void preorder (node *temp) {

if(temp != null) {

printf(“%d”,temp ->data); preorder (temp ->left); preorder (temp->right);

(60)

} }

void postorder (node *temp) {

if (temp!=null) {

postorder (temp-> left); postorder (temp->right); printf (“ %d “,temp->data); }

}

RESULT:

Thus a C-program to create the simple binary tree and recursive traversal had been executed and verified.

FLOW CHART:

Start

Print 1.create 2.Preorder 3.Inorder 4.Postorder

Read ch

(61)

A switc h (ch) while(c h <=8) Create node(b tree, temp) preorder (btree) insert( btree) post order( btree) Stop

(62)

if(btree= =NULL) tree *insert node (tree *btree,tree* temp) if(temp-> data> btree->data) if(temp-> data=btr ee->data) return Print “Data already exists” if(temp-> data< btree->data)

(63)

OUTPUT:

Program for implementing simple binary tree 1. Create

2. Inorder 3. Preorder 4. Postorder

Enter your choice 1 Enter the element 5

Do you want to enter more elements? (y/n)y Enter the element 3

Do you want to enter more elements? (y/n)y Enter the element 4

Do you want to enter more elements? (y/n)y Enter the element 6

Do you want to enter more elements? (y/n)n Program for implementing simple binary tree 1. Create

2. Inorder 3. Preorder 4. Postorder

Enter your choice 2

(64)

SEQUENTIAL SEARCH

AIM:

To write a C-program to perform the linear search operation on some number of elements.

ALGORITHM:

Step:1 Start the program. Step:2 Read the key value

Step:3 Go to the functions create and display. Step:4 If (status = = 1) print the value is present. Step:5 Else print the value is not present.

Step:6 Stop the program.

Sub – function.

Create ( )

i) Read the n value.

ii) Read the a[i] elements using for loop. Display ( )

i) Print the elements. Search ( )

i) If (a[i] = = k) return 1. ii) Else return 0.

PROGRAM: #include<stdio.h> #include<conio.h> #define max 10 int a[max] , n , i; void create ( ) {

printf (“ \n how many elements”); scanf (“%d”,&n);

printf (“\n enter the elements”); for (i=0;i<n;i++)

scanf (“%d”.& a[i]); }

void display ( ) {

(65)

printf (“\n the elements are”); for (i=0;i<n;i++) printf (“\n %d ”,a[i]); } search (int k) { for(i=0;i<n;i++) { if(a[i]= =k) return 1; } return 0; } void main( ) {

int status, key; clrscr ( );

create ( ); display ( );

printf (“\m enter the element which you wish to search”); scanf (“%d”,& key);

status = search (key); if (status = = 1)

printf(“\n the element is present”); else

printf(“\n the element is not found”); getch ( );

}

(66)

Thus the c – program to perform the linear search operation on some number of elements had been executed and verified.

FLOW CHART: Main program: for(i=0;i<n; i++) Enter the element linearsearch() Enter thelimit

Print the output

Stop if search = =-1

(67)

Sub-program:

OUTPUT:

How many elements 5

Enter the elements 23 36 56 48 79 Enter the element you wish to search 48 The element is present

for(i=0;i<n;i++) linearsearch() if a[i]= =k return 1

(68)

QUICKSORT

AIM:

To write a C-program to sort the elements by quick sort.

ALGORITHM:

Step:1 Read the total number of elements in the list, say n. Step:2 Store the elements in the array.

Step:3 Take the first element from the array and call it as the pivoted element.

Step:4 Now find all elements which are less than the pivoted element and place them before the pivoted element. Thus the array will be divided in the lesser elements ,pivoted elements and elements greater than the pivoted element.

Step:5 Repeat step 4 each time placing the pivoted element at the proper position. Thus the complete list will get sorted.

Step:6 Stop. PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 10 void main() { int n,i,x[MAX];

void quick(int x[],int,int); clrscr();

printf(“How many elements do you wish to sort?\n”); scanf(“%d”,&n);

for(i=0;i<n;i++) {

printf(“\n Enter the elements:”); scanf(“%d”,&x[i]);

}

quick(x,0,n-1);

printf(“\n\t\t The sorted elements are:” ); for(i=0;i<n;i++)

printf(“\n\t\t%d”,x[i]); getch();

}

void quick(int x[MAX],int lb,int ub) {

(69)

int partition(int x[MAX],int lb,int ub); if(lb<ub) { new pivote=partition(x,lb,ub); quick(x,lb,new pivote-1); quick(x,new pivote+1,ub); } }

int partition(int x[MAX],int lb,int ub) {

int q,pivot,i,lower;

void interchange(int x[MAX],int a,int b; interchange(x,lb,(lb+ub)/2); pivote=x[lb]; lower=lb; for(i=lb+1;i<=ub;i++) { if(x[i]<pivot) { lower=lower+1; interchange(x,lower,i); } } interchange(x,lb,lower); q=lower; return q;

void interchange(int x[MAX],int a,int b) { int temp; temp=x[a]; x[a]=x[b]; x[b]=temp; } RESULT:

(70)

FLOW CHART:

OUTPUT:

Enter how many elements do you wish to sort:5 START Print “How many elements” Read n for(i=0;i<n;i++) Read x[i] Print “Enter the elements” Next i Quick(x,0,n-1) Print “The sorted elements are “ for(i=0;i<n;i++) STOP Print x[i]

(71)

Enter the elements:55 Enter the elements:44

Enter the elements:33 Enter the elements:22 Enter the elements:11

The sorted elements are 11 22 33 44 55

BUBBLE SORT

AIM:

To write a C-program to perform sorting by bubble sort.

ALGORITHM:

Step:1 Read the total number of element, n. Step:2 .Store the elements in the array. Step:3 Set i=0.

Step:4 Compare the adjacent elements. Step:5 Repeat step 4 for all n elements.

Step:6 Increment the value of i by 1 and repeat step 4,5 for i<n. Step:7 Print the list of sorted elements.

Step:8 Stop.

PROGRAM:

#include<stdio.h> #include<conio.h> #include<process.h>

void bubblesort(int a[20],int n); void bubblesort(int a[20],int n); { int i,j,temp; for(i=0;i<n-1;i++) { for(j=0;j<n;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } for(i=0;i<n;i++) printf(“\n\t%d”,a[i]); } void main() { int a[20],i,n; int ch; clrscr();

(72)

scanf(“%d”,&n);

printf(“Enter the number of elements in the array”); for(i=0;i<n;i++)

scanf(“%d”,&a[i]); bubblesort(a,n); }

RESULT:

(73)

FLOW CHART:

Main program: Sub-program:

OUTPUT:

Enter the number of elements in the sorting array:3 Enter the elements in the array:

30 50 10

Read

Elements

Enter the elements Start stop For(i=o;i<n;i+ +) Bubble(a,n) int swapped=TRUE i=0 j=0 Whil e J<n-1 Ajj=a[j+1] ; A[] [j]<a[j+1] Swapped=TRUE; Temp=a[j]; A[j]=a[j+1]; J+1 Print the elements

whil e j<n

(74)

After sorting 10 30 50

IMPLEMENTATION OF DOUBLY LINKED LIST

AIM:

To write and execute a program to implement the operation in double linked list.

ALGORITHM:

Step:1 Start. Step:2 Read.

Step:3 If c==1,create new node.

Step:4 If c==2,insert new node at specific location. Step:5 If c==3,delete a particular node.

Step:6 If c==4,display the list. Step:7 If c==5,exit. Step:8 Stop. PROGRAM: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int data; struct node*next,*prev; } *New,*new1,*temp,*start,*dummy; void add(void) struct node*get_node(); void display(void); void delete(void); int find(int); int first=1; void main() { char ans; int choice,num,found=0; struct=NULL; do{ clrscr();

printf("Program for double link list");

printf("\n1.insert\n2.delete\n3.display\n4.searching\n5.exit"); printf("\n enter your choice:");

scanf("%d",&choice); switch(choice); {

(75)

case 1:add() break; case 2:delete() break; case 3:display() break;

case 4:printf("enter no to be search:"); scanf("%d",&num); temp=start; while((temp!=NULL)&&(found==0)) found=find(num); if(found) printf("\n no is present"); break; case 5:exit(0) }

printf("\n do u want to continue?"); ans=getch(); } while(ans=='y'||ans=='Y') getch(); } void add(void) { clrscr(); New=get_node();

printf("\n\n\tEnter the element:"); scanf("%d",&New->data); if(first==1) { start=New; first=0; } else { dummy=start; while(dummy->next!=NULL) dummy=dummy->next; dummy->next=New; New->pre=dummy; } } struct node*get_node() { new1=(node*)malloc(sizeof(struct node)); new1->next=NULL; new1->prev=NULL; return(new1); } void display(void) { clrscr(); temp=stat; if(temp==NULL);

(76)

printf("\n double linked list is empty"); else { while(temp!=NULL) { printf("%d",temp->data); temp=temp->next; } printf("NULL"); } getch(); }

int find(int num) { if(temp->data==num) return(1) else temp=temp>next; return 0; } void delete(void) { int num,flag=0; int found; int least=0; clrscr(); temp=start; if(temp==NULL)

printf("\n sorry dll not created"); else

{

printf("\n Enter the no of deleated"); scanf("%d",&num); while((flag==0)&&temp!=NULL)) { found=find(num); flag=found; } if(found==0)

printf("\n no not found"); else { if(temp=start) { start=start->next; temp->next=NULL; start->next=NULL; free(temp); getch();

printf("\n the starting node is deleted"); }

else {

(77)

last=1; else last=0; (temp->next)->prev=temp->prev; (temp->prev)->next=temp->next; temp->prev=NULL; free(temp); if(last)

printf("\n the last node is deleted"); else

printf("\n the intermediate node is deleted"); }

} } }

RESULT:

Thus,a program to implement double linked list was written,executed and output was verified.

(78)

START

Print program for various operation on doubly linked list 1.create 2.display 3.search 4.insert 5.delete 6.exit Read choice Switch choice while (choice!=6) STOP

(79)

OUTPUT:

Program for double link list Insert

Delete Display Searching Exit

Enter your choice:1

f data=num i next=null rprev=r qnext=r

(80)

CONVERSION OF PREFIX TO POSTFIX

AIM:

To write a C program to convert prefix to postfix expression..

ALGORITHM:

Step:1 Start the program. Step:2 Read the given string.

Step:3If the input symbol read is ‘c’ push it into the stack.

Step:4 If the input symbol read is an operand then place it in the expression Step:5 If the input symbol read is an operator,then

a)check the precedence of the operator read. If it has a higher precedence then remove it from the stack and place it in the postfix expression.

Repeat 5a) till operator in the stack has a higher precedence than that being read.

Step:6 If the input symbol read is closing parenthesis then pop all the operators from the stack and place them in the postfix expression till the opening parenthesis is encountered.

Step:7 Stop the program.

PROGRAM: #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> #include<process.h> #include<string.h> char pre[40],post[40]; int top=0,st[20]; void postfix(); void push(int); char pop(); void main() { clrscr();

printf(“Enter the expression”); scanf(“%s”,&pre); postfix(); getch(); } void postfix() { int i,j=0; fro(i=0;pre[i]!=’\0’;i++) { switch(inf[i]) { case ‘+’: while(st[top]>=1) post[j++]=pop(); push(1); break; case ‘-’: while(st[top]>=1) post[j++]=pop(); push(2); break;

(81)

case ‘*’: while(st[top]>=3) post[j++]=pop(); push(3); break; case ‘/’: while(st[top]>=3) post[j++]=pop(); push(4); break; case ‘^’: while(st[top]>=1) post[j++]=pop(); push(5); break; case ‘(’: push(0); break; case ‘)’: while(st[top]!=0) post[j++]=pop(); top--; break; default: post[j++]=pre[i]; } } while(top>0) post[j++]=pop();

printf(“\nThe postfix expression is”,post); }

void push(int ele) { top++; st[top]=ele; } char pop() { int el; char e; el=st[top] ; top--; switch(el) { case 1: e= ‘+’; break case 2: e= ‘-’; break; case 3: e= ‘*’; break; case 4:

(82)

e= ‘/’; break; case 5: e= ‘^’; break; } return(e); } RESULT:

Thus a C program to convert the prefix expression to postfix expression was written, executed and the output was verfied.

(83)

FLOWCHART: START Read input Pre 2 post STOP Pre 2 post while input()! =10 if operat ed op2=pop() op1=pop() print postfix exp() i++

Print the expression yes

no

Sub-Program Main program

(84)

OUTPUT:

Enter the expreesion: +ab The postfix expression is ab+

(85)

CIRCULARLY LINKED LIST

AIM:

To write a program to implement circularly linked list and perform the operations such as creation, insertion, deletion and view

ALGORITHM:

Step 1.Start the program.

Step 2.Adding the element in the circularly linked list is achieved by addcirq();

Creating a new node using malloc. Getting the item in the data field of the node. Linking the next field to the list.

Step 3.Removing the node from the list using delcirq() operation.

If the queue is empty print that the list is empty. Link the data of the node to the temp node and free the temp node..

Step 4.View the list using display operation.. Step 5.Using print function print all the data. Step 6.Stop the program.

PROGRAM: #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data;

struct node *link; };

(86)

int delcirq(struct node**,struct node**); void cirq_display(struct node*);

void main()

struct node *front,*rear; front=rear=NULL; addcirq(&front,&rear,10); addcirq(&front,&rear,17); addcirq(&front,&rear,18); addcirq(&front,&rear,23); addcirq(&front,&rear,12); clrscr();

printf(“\n Before deletion:\n”); cirq_display(front);

delcirq(&front,&rear); delcirq(&front,&rear); printf(“\n After deletion:\n”); cirq_display(front);

getch(); }

Void addcirq (struct node **f,struct node **r,int item) {

struct node *q;

q=malloc (sizeof(struct node)); qdata+=item; if(*f==null) *f=q; else (*r)link=q; (*r)q; (*r)link=(*f); }

int delcirq (struct node **f,struct node **r) { struct node *q; int item; if(*f==null) printf(“queue is empty”); else { if(*f==*r) { item=(*f)data; free(*f); *f=null; *r=null; } else { q=*f; item=qdata; *f=(*f)link; (*r)link=*f; free(q); }

(87)

return(item); }

return Null; }

void cirq_display(struct node *f) {

struct node *q=f,*p=Null; while(q1=p) { printf(“%d”,qdata); qqlink; p=f; } } RESULT:

Thus a C program to implement circularly linked list was written, executed and the output was verified. FLOW CHART:

start

Switch

(choic

e)

Create()

While

Ch<=

0

stop

Read

choice

Delete()

display()

view()

Start=null

Circularly linked list

yes

(88)

SINGLY LINKED LIST

AIM:

To write a ‘C’ program to perform operations like create, insert,delete, seach,and display in a singly linked list.

ALGORITHM:

Step 1. Start the program.

Step 2. To perform various operations read the value for choice. Step 3. If the choice is 1. create function is called to create to the list. a) Assign temp=NULL and Flag=TRUE and also ans=’Y’

b) Read the value for val and allocate memory for the new node. A new node is created and the above procedure is followed until ans=’Y’.

step 4. If the choice is 2, display() is executed to display the list. a) Assign temp=bead.

b) If temp =NULL, print “ the list is empty”. c) print the list under it until temp is NULL.

Step 5. If the choice is 3 search() is executed to search the element in the list. Step 6. If the choice is 4 insert() is executed to insert an element.

Step 7.If the choice is 5 delete () is executed

Step 8. If we want to exit ,the choice is 6 and the default statement is” Invalid choice,try again”. Step 9. Stop the program.

PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0

(89)

typedef struct SLL { int data; struct SLL*next; }node; Node*create(); void main() {

int choice, val; char ans; node*head; void display(node*); node*search(node*,int); void insert(node*) void delete(node**); head=NULL; do { clrscr();

printf(“ program to perform various operations on linked list”);

printf(“\n 1. create \n 2. display\n 3. search\n 4. insert\n 5. delete \n 6.quit”); printf(“ \n Enter your choice”);

scanf(“%d”,& choice); switch(choice) { case1: head=create(); break; case 2: display (head); break; case 3:

printf(“ enter the element you want to search”); scanf(“%d”,val); scanf(“head,val); break; case 4: insert(head); break; case 5: dele (head); break; case 6: exit(0); default; clrscr();

printf(“ Invalid choice,try again”); getch(); } } while(choice!=t); } node*create() {

(90)

node*temp,*new,*head; int val, flag;

char ans=’Y’; node*get_node(); temp=NULL; flag=TRUE; do { {

printf(“\n Enter the element:”); scanf(“%d”,&val);

new=get_node(); If(new ==NULL)

printf(“ \n memory is not allocated”); new->data=val; if(flag) { head= new; temp=head; flag=FALSE; } else { temp->next=new; temp=new; }

printf(“\n do you want to enter more element?(y/n)”); ans=getche();

}

while (ans==’Y’);

printf(“The singly linked list is created”); getch(); clrscr(); return head; } node*get_node() { node*temp; temp=(node*)malloc(size of node)); temp->next=NULL; return temp; } void display(node*head) { node*temp; temp=head; if(temp==NULL) {

printf(“\n The list is empty”); getch();

clrscr(); return; }

(91)

} printf(“%d->”,temp->data); temp=temp->next; } printf(“NULL”); } node*searchnode(node*head,int key) { node*temp; int found; temp=head; if(temp==NULL) {

printf(“The linked list is empty”); getch(); clrscr(); return NULL; } found=FALSE; while(temp!=NULL&&!found){ if(found) {

printf(“The element is present in the list\n”); getch();

return temp; }

else {

printf(“The element is not present in the list\n”); getch();

return NULL; }

}

void insert(node *head) { node *temp,*New; int val; temp=head; if(temp==NULL) {

printf(“\nInsertion is not possible”); getch();

return; } clrscr();

printf(“\nEnter the elements”); scanf(“%d”,&val);

New=(node*)malloc(sizeof(node)); if(New==NULL)

printf(“\nMemory is not allocated”); Newdata=val;

(92)

Newnext=tempnext; tempnext=New; printf(“\nElement is inserted”); getch(); } void del() { int t,pos,j;

printf(“\n want to delete any data?(y/n)”); scanf(“%c”,j);

while(j==’y’) {

list=head;

printf(“enter the position to be deleted”); fflush (stdin); scanf(“%d”,&pos); if(pos==1) { head=listlink; else { for(i=1;i<pos-1;i++) list=listlink; if(pos==n) listlink=NULL; else { prev=list; list=listlink; next=listlink; prevlink=next; } } n--;

printf(“want delete any data?(y/n)”); fflush (stdin);

scanf(“%c”,&j); }

}

RESULT:

Thus the c-program for singly linked list had been written and the operations such as creation ,deletion, insertion, display was performed.

(93)

FLOWCHART:

start

Switch

(choic

e)

Create()

While

Ch<=

0

stop

Read

choice

Delete()

display()

view()

Start=null

Main Program

(94)

view() return list=head if list=NUL L print roll no name print roll no name

print list is empty

for(list!=NULL;list=list->link)

next list

(95)

OUTPUT:

Program for various operations of linked list 6. create

2. display 7. search

8. insert an item in the list 9. delete an item in the list 10. exit

Enter your choice:1 Enter the element 5

Do you want to enter more element=Y Enter the element 6

Do you want to enter more element=N 2. create

2. display 7. search

8. insert an item in the list 9. delete an item in the list 10. exit

Enter your choice : 3

Enter the element you want to search: 5 The element is present

Figure

Updating...

References

Related subjects :
Outline : BUBBLE SORT