• No results found

BUBBLE SORT

In document Data Stuctures Lab Manual (Page 71-95)

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++)

void main() {

int a[20],i,n;

int ch;

clrscr();

printf(“Enter the number of elements in the sorting array”);

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:

Thus a C-program is written to perform sorting by bubble sort.

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

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);

{

case 1:add() break;

case 2:delete() break;

case 3:display() break;

case 4:printf("enter no to be search:");

scanf("%d",&num);

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) {

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;

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.

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()

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

Return

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:

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++)

case ‘*’:

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

}

void push(int ele) {

top++;

st[top]=ele;

}

char pop() {

int el;

char e;

el=st[top] ; top--;

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.

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

OUTPUT:

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

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;

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;

int delcirq (struct node **f,struct node **r) {

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<=

stop 0 Read choice

Delete() display() view()

Start=null

Circularly linked list

yes

no

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();

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)

clrscr();

printf(“ Invalid choice,try again”);

getch();

node*temp,*new,*head;

int val, flag;

char ans=’Y’;

node*get_node();

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;

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)

}

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;

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);

for(i=1;i<pos-1;i++) list=listlink;

if(pos==n)

listlink=NULL;

else {

prev=list;

list=listlink;

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.

FLOWCHART:

start

Switch (choic

e)

Create()

While Ch<=

stop 0 Read choice

Delete() display() view()

Start=null

Main Program

yes no

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 Sub-Program:

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

In document Data Stuctures Lab Manual (Page 71-95)

Related documents