0% found this document useful (0 votes)
240 views

Data Structure File MPCT

The document contains programs and algorithms for implementing various data structures and algorithms. It includes programs for stacks, queues, binary trees, and converting infix expressions to postfix and prefix expressions. For stacks and queues, it provides programs for insertion and deletion operations. For binary trees, it outlines algorithms for traversal, insertion, and deletion. It also includes a program that takes an infix expression as input and outputs the equivalent postfix expression.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

Data Structure File MPCT

The document contains programs and algorithms for implementing various data structures and algorithms. It includes programs for stacks, queues, binary trees, and converting infix expressions to postfix and prefix expressions. For stacks and queues, it provides programs for insertion and deletion operations. For binary trees, it outlines algorithms for traversal, insertion, and deletion. It also includes a program that takes an infix expression as input and outputs the equivalent postfix expression.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 37

Submitted to- submitted by-

Mr. AKSHAYA SAHAY Mr. VIKALP KULSHRESTHA


Lect. Of C.S.E/I.T. deptt. Roll no.- 0903EC061114
Subject –Data Structure

S. N. Title REMARK

 Program to implement insertion (PUSH) and deletion (POP) in


stacks.
 Program to implement insertion and deletion using pointers in
queues.
 Traversal in binary trees and insertion and deletion of trees.
 Algorithm and program of converting infix to postfix and prefix
expression.
 Program to implement dqueues in arrays.
 Program to insert and delete nodes from a singly linked list at
1. Beginning
2. End
3. specified position
 Program for linear hashing.
 Algorithm for BFS and DFS.
 Write program for following sorting techniques
1. quick sort
2. bubble sort
3. selection sort
4. heap sort
Program to implement insertion (PUSH) and deletion (POP) in
stacks
# include <stdio.h>
# include <conio.h>
# define MAX 3
int stack [MAX], tos = -1, num = 0;
void push (void);
int pop (void);
void disp (void);

void main ( )
{
clrscr( );
for ( ; ; )
{
printf (“stack of integers demo :”);
printf (“Menu Press …\n”);
printf (“1. push\n 2. pop\n 3. Display\n 4. Exit\n”);
scanf (“%d” , &num);
switch (num)
{
case 1:
push ( );
break;
case 2:
if (tos= = -1)
printf (“stack is empty. \n”);
else
printf (“removed %d from position %d\n”, pop( ), tos+1);;
break;
case 3:
disp( );
break;
case 4:
exit (0);
break;
default:
printf (“invalid option entered. \n”);
break;
} //switch
} //for
} //main
void push (void)
{
if (tos == MAX-1)
{
printf (“you have entered the top of the stack.”);
printf (“can not add any more elements. \n”);
}
else
{
printf (“add to stack. Enter an integer to add to stack :”);
scanf (“%d”, &num);
tos ++;
stack [tos] =num;
printf (“%d added to position %d in the stack \n”, num, tos+1);
}
}
int pop (void)
{
int j;
j =(tos = =-1)?0: stack [tos];
if ( tos != -1)
{
stack[tos] =0;
tos- -;
}
return j;
}
void disp (void)
{
int i ;
if (tos >=0)
{
for (i =0; i <=tos; i + +)
printf (“%d in position in the array \n”, stack[i], i);
}
else
printf (“stack is empty \n Nothing to display! \n”);
}

Program to implement insertion and deletion in queues


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

# define MAX 10

void addq (int*, int, int*, int*);


int delq (int*, int*, int*);

void main ( )
{
int arr[MAX];
int front = -1, rear = -1, I;

clrscr ( );

addq (arr, 23, &front, &rear);


addq (arr, 9, &front, &rear);
addq (arr, 11, &front, &rear);
addq (arr, -10, &front, &rear);
addq (arr, 25, &front, &rear);
addq (arr, 16, &front, &rear);
addq (arr, 17, &front, &rear);
addq (arr, 22, &front, &rear);
addq (arr, 19, &front, &rear);
addq (arr, 30, &front, &rear);
addq (arr, 32, &front, &rear);

i =delq (arr, &front, &rear);


printf (“\n Item deleted: %d”, i);

i=delq (arr, &front, &rear);


printf (“\n Item deleted: %d”, i);

i=delq (arr, &front, &rear);


printf (“\n Item deleted: %d”, i);

getch( );
}

/* adds an element to the queue */


void addq (int *arr, int item, int *pfront, int *prear)
{
if (*prear = = MAX-1)
{
printf (“\n Queue is full.”);
return;
}
(*prear)+ +;
arr[*prear] =item;

if (*pfront = = -1)
*pfront = 0;
}
/* removes an element from the queue */
int delq (int *arr, int *pfront, int *prear)
{
int data;

if (*pfront = = -1)
{
printf (“\n Queue is empty.”);
return NULL;
}

data = arr[*pfront];
arr[*pfront] = 0;
if (*pfront = = *prear)
*pfront = *prear = -1;
else
(*pfront) + +;
return data;
}

Traversal in binary trees and insertion and deletion of trees


Preorder traversal
Initially push NULL onto STACK and then set PTR: =
ROOT. Then repeat the following steps until PTR = NULL or, equivalently, while
PTR=! NULL.
a. Proceed down the left-most path rooted at PTR, processing each node N
on the path and pushing each right child R (N), if any, onto STACK. The
traversing ends after a node N with no left child L (N) is processed. (Thus
PTR is updated using the assignment PTR: = LEFT [PTR], and the
traversing stops when LEFT [PTR] = NULL.)
b. [Backtracking.] Pop and assign to PTR the top element on STACK. If PTR =!
NULL, then return to step (a); otherwise Exit.

Inorder traversal
Initially push NULL onto STACK and then set PTR:
= ROOT. Then repeat the following steps until NULL is popped from STACK.
a. Proceed down the left-most path rooted at PTR, pushing each node N
onto STACK and stopping when a node N with no left child is pushed onto
STACK.
b. [Backtracking.] Pop and process the nodes on STACK. If NULL is popped,
then Exit. If a node N with a right child R (N) is processed, set PTR =R (N)
(by assigning PTR: = RIGHT [PTR]) and return to step (a).

Post order traversal


Initially push NULL onto STACK and then set PTR:
= ROOT. Then repeat the following steps until NULL is popped from STACK.
a. Proceed down the left-most path rooted at PTR. At each node N of the
path, push N onto STACK and, if N has a right child R(N), push –R(N) onto
STACK.
b. [Backtracking.] Pop and process positive nodes on STACK. If NULL is
popped, then Exit. If a negative node is popped, that is, PTR= -N for some
node N, set PTR= N and return to step (a).

Insertion in trees
INSBT (INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM, LOC)

A binary search tree T is in memory and an ITEM of information is given. This


algorithm finds the location LOC of ITEM as a new node in T at location LOC.
1) Call FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR).
2) If LOC=! NULL, then Exit.
3) [Copy ITEM into new node in AVAIL list.]
 If AVAIL = NULL, then: Write: OVERFLOW, and Exit.
 Set NEW: = AVAIL, AVAIL: = LEFT [AVAIL] and INFO [NEW]: = ITEM.
 Set LOC: = NEW, LEFT [NEW]: = NULL and RIGHT [NEW]: =NULL.
4) [Add ITEM to tree.]

If PAR = NULL, then:

Set ROOT: = NEW

Else if ITEM < INFO [PAR], then:

Set LEFT [PAR]: = NEW.

Else:

Set RIGHT [PAR]: = NEW.

[End of if structure.]

5) Exit.

Deletion in trees
DEL (INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM)
A binary search tree T is in memory and an ITEM of information is given. This
algorithm deletes ITEM from the tree.
1) [finds the loations os ITEM and its parent]
Call FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2) [ITEM in tree?]
If LOC = NULL, then: write: ITEM not in tree, and Exit.
3) [Delete node containing ITEM.]
If RIGHT [LOC] =! NULL and LEFT [LOC] =! NULL, then:
Call CASEB (INFO, LEFT, RIGHT, ROOT, LOC, PAR).
Else:
Call CASEA (INFO, LEFT, RIGHT, ROOT, LOC, PAR).
[End of if structure.]
4) [Return deleted node to the AVAIL list.]
Set LEFT [LOC]: = AVAIL and AVAIL: = LOC.
5) Exit.
Algorithm and program of converting infix to postfix and
prefix expression

Algorithm for infix to postfix


Q-giving infix expression.
P-To be generated postfix expression.
1. Add a “(” to the left and “)” to the right of the given infix
Expression q.
2. Scan q element by element from left to right on repeat the
Step 3,4,5,6 till stack is not empty.
3. If and operand push is found then push add to p
[And of it block]
4. If” (” is encountered then push it in to stack.
[End of block]
5. If an operator op is encountered then.
a. pop all the operator from the stack and add them to p which have same
precedence
then the encountered operator till”)” is found .
b. add the operator to the stack .[end of the block ]
6. if a”)” is encountered then.
a. pop all the operator from top of the stack and add them to p “(“ is
encountered.
b. remove the “(“ from the stack
[it is not to be added any where]
[end of if block]
7. Display expression P as the result.
8. Return.

Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 15
#define true 1
#define false 0

/*Structure Declaration*/
typedef struct
{
char data[MAX];
char top;
}STK;

/*Function Declarations*/
void input(char str[]);
void intopre(char str1[],char pre[]);
void intopost(char str1[],char post[]);
int isoperand(char sym);
int prcd(char sym);
void push(STK *s1,char elem);
int pop(STK *s1);
int empty(STK *s2);
int full(STK *s2);
void dis(char str[]);

void main()
{
STK s;
int cs,ans;
char str[MAX],pre[MAX],post[MAX];
clrscr();
do /*Using Do-while Loop*/
{
clrscr( );
printf("
-----Program for Expressions-----");
printf("
printf("Input The String:");
MENU:
");
printf("1.Infix to Prefix
");
printf("2.Infix to Postfix");
printf("
3.Exit");
cs=getche();

switch(cs) /*Using Switch Case*/


{
case 1:
intopre(str,pre);
break;
case 2:
intopost(str,post);
break;
case 3:
break;
default:
printf("
Enter a Valid Choise!"); /*Default Case*/
break;
}
printf("
Do you wish to Continue?(y/n)");
ans=getche();
}while(ans=='y'||ans=='Y'); /*Condition for Do-while loop*/

getch();
}

/**************************************************/
/*To Input String*/
/**************************************************/
void input(char str)
{
printf("Enter the Infix String:");
scanf("%s",str);
}

/**************************************************/
/*To Covert Infix To Prefix*/
/**************************************************/
void intopre(STK s1,char str1[],char pre[])
{
int len,flag;
len=strlen(str1);
int check=0,cnt=len-1,pos=0;
char elem;

while(cnt>=0) /*while condition*/


{
flag=0;
if(isoperand(str1[cnt])) /*Checking for Operand*/
{
printf("%c",str1[cnt]);
cnt--;
pos++;
}
else
{
check=prcd(str1[cnt]);
while(check==false)
{
pre[pos]=str1[cnt];
flag=1;
pos++;
cnt--;
}
if(flag==0)
{
elem=pop(&s1);
printf("%c",elem);
}
}

}
}

/**************************************************/
/*To Convert Infix To Postfix*/
/**************************************************/
void intopost(STK s1,char str1[],char post[])
{
int len;
len=strlen(str1);
int check=0,cnt=len-1,pos=0;

}
/**************************************************/
/*To Check For Operand*/
/**************************************************/
int isoperand(char sym)
{
if('A'<sym<'Z'||'a'<sym<'z')
return(true);
return(false);
}

/**************************************************/
/*To Check The Precedence*/
/**************************************************/
int prcd(char sym)
{

/**************************************************/
/*To Display String*/
/**************************************************/
void dis(char str[])
{

}
/******************************************/
/*Push Function Definition*/
/******************************************/
void push(STK *s1,char elem)
{
if(!full(s1))
{
s1->top++; /*Incrementing top*/
s1->data[s1->top]=elem; /*Storing element*/
}
else
printf("
Stack is Full!");
}
/******************************************/
/*Full Function Definition*/
/******************************************/
int full(STK *s2)
{
if(s2->top==MAX) /*Condition for Full*/
return(true);
return(false);
}

/******************************************/
/*Pop Function Definition*/
/******************************************/
int pop(STK *s1)
{
char elem;
if(!empty(s1))
{
elem=s1->data[s1->top]; /*Storing top stack element in elem*/
s1->top--; /*Decrementing top*/
return(elem);
}
return(false);
}
/******************************************/
/*Empty Function Definition*/
/******************************************/
int empty(STK *s2)
{
if(s2->top==-1) /*Condition For Empty*/
return(true);
return(false);
}

Write program for following sorting techniques.


Bubble sort: -

#include<stdio.h>
Void main()
{
Int a [100],n,I;
Printf(“how many element any array”);
Scanf(“%d”,&n);
For(i=0;i<=0;i++);
{
Scanf(“%d”,& a[i]);
}
Bubble-short(a,n);
Printf(“shorted line is as follows”)
For(i=0;i<n;i++);
Printf(“%d”,a[i]);
}
Void bubble-short(int a[],int n)
{
Int temp ,I, j;
For(i=0;i<n;i++);
{
For(j=0;j<n-i-1;j++)
{
If(a[j]>a[j+1])
{
Temp=a[j];
A[j]a[j+1];
A[j]a[j+1];
=temp;
}
}
}
}

Selection sort:-

#include<stdio.h>
#include<conio.h>
Void main()
{
Int a[100],n,I;
Printf(“how many elements”);
Scanf(“%d”,&a[i]);
For(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
Selection-sort(a,n);
For(i=0;i<n;i++)
Printf(“soted array in”);
For(i=0;i<=n;i++)
{
Printf(“%d”,a[i]);
}
}
Void selection-sort (int a[],int n)
{
Int min, loc,temp,I,j;
Min =a[0];
For(i=0;i<=n-1;i++)
{
Min=a[i];
Loc=I;
For(j=i+1;i<=n-1;i++)
{
If(a[j]<min)
{
Min=a[j];
Loc=j;
}
}
If(loc!=i);
{
Temp=a[i];
A[i]=a[loc];
A[loc]=temp;
}
}
}

Quick sort:-

#include<stdio.h>
#include<conio.h>
#define max 100
Int a[max],n,I,l,h;
Void main()
{
Void input(void);
Input();
Getch();
}
Void input(void)
{
Void quick_sort(inta[]),int l,int h);
Void output(int a[],int n);
Printf(“how many element in the array”);
Scanf(“%d”,&n);
For(i=0;i<=n-1;i++)
{
Scanf(“%d”,&a[i]);
}
L=0
H=n-1;
Quck_sort(a,l,h);
Printf(“sorted array:\n”);
Output(a,n);
}
Void quick_sort(int a[],intl,inth)
{
Int temp,key,low,high;
Low=l;
High=h;
Key=a[(low+high)/2];
Do
{
While(key>a[low])
{
Low++;
}
While(key<a[high])
{
High--;
}
If(low<=high)
{
Temp=a[low];
A[low++]=a[high];
A[high--]=temp;
}
}
While(low<=high);
If(l<=high)
Quick_sort(a,l,high);
If(low<h)
Quick_sort(a,low,h);
}
Void output(int a[],int n)
{
For(j=i+1;i<=n-1;i++)
{
Printf(“%d\n”,a[i]);
}
}
Heap sort:-

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

void main()
{
int *x,i,n;
int temp;
void heap(int *,int);
clrscr();
fflush(stdin);
printf("
Heap Sort
");
printf("
Enter How many Numbers : ");
scanf("%d",&n);
x = (int *)malloc(n * sizeof(int));
for(i=0;i<n;i++)
{fflush(stdin);
scanf("%d",&x[i]);
}
heap(x,n);

for(i=n-1;i>=1;i--)
{
temp = x[i];
x[i] = x[0];
x[0] = temp;
heap(x,i-1);
}

printf("
Resultant Array
");
for(i=0;i<n;i++)
printf("%d ",x[i]);
free(x);
getch();
}
void heap(int *a,int n)
{
int i,temp;
for(i=n/2;i>=0;i--)
{
if(a[(2*i)+1] < a[(2*i)+2] && (2*i+1)<=n && (2*i+2)<=n)
{
temp = a[(2*i)+1];
a[(2*i)+1] = a[(2*i)+2];
a[(2*i)+2] = temp;
}
if(a[(2*i)+1] > a[i] && (2*i+1)<=n && i<=n)
{
temp = a[(2*i)+1];
a[(2*i)+1] = a[i];
a[i] = temp;
}
}
}

Algorithm for BFS and DFS.

1.Breadth-first search algorithm


Our breadth-first search algorithm will use a queue of Integers to store the most recently visited
vertices and use the head of this queue to dictate how to explore the graph further.

static public int  BFS(Graph  G,  int v, int[]  LevelOrder)


{
     int n  = G.order();
     for  (int  i=0;  i<n;  i++) LevelOrder[i]=0;
 
     Queue  toGrow =  new Queue();
     int cnt  = 0;
 
      LevelOrder[v]  = ++cnt;
      toGrow.enque(new Integer(v));
 
      while (!toGrow.empty())
      {
        int grow  = ((Integer)  toGrow.deque()).intValue();
        Vector  nbrs  = G.neighbors(grow);
 
        for  (int  i=0;  i<nbrs.size(); i++)
        {
           int u  = ((Integer)  nbrs.elementAt(i)).intValue();
 
           if ( LevelOrder[u] == 0  )  //  i.e.,  not visited  yet
            {
            LevelOrder[u] =  ++cnt;
            toGrow.enque(new Integer(u));
            }
        }
     }
return  cnt;
}
#include<iostream.h>

#define MAX 10

class DFS
{
private : int n;
int adj[MAX][MAX];
int visited[MAX];
public : void dfs(int);
void readmatrix();
};

void DFS :: readmatrix()


{
int i,j;
cout << "\nEnter the number of Vertices in the Graph : ";
cin >> n;
cout << "\nEnter the Adjacency Matrix\n\n";
for (i = 1; i <= n; i++)
for (j = 1; j<= n; j++)
cin >> adj[i][j];
for (i = 1; i <= n; i++)
visited[i] = 0;
}

void DFS :: dfs(int source)


{
int i;
visited[source] = 1;
cout << source << " ";
for (i = 1; i <= n; i++)
if (adj[source][i] && !visited[i])
dfs(i);
}

int main()
{
int source;
DFS depth;
depth.readmatrix();
cout << "\nEnter the Source : ";
cin >> source;
cout << "\nThe nodes visited in the DFS order is : ";
depth.dfs(source);
return 0;
}

Depth-first search algorithm

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

#definr true 1
#define false 0
#define max 8

Struct node
{
Int data;
Struct node *next;
};
Int visited[max];
Void dfs(int,truct node**);
Struct node*getnode_write(int);
Void del(strct node*);

Void main()
Sruct node arr[max];
Struct node *v1,*v2,*v3,*v4;
Int I;
Clrscr();
V1=getnode_write(2)
Arr[0]=v1;
V1->next=v2=getnode_write(8);
V2->next=null;

V1=getnode_write(1)
Arr[1]=v1;
V1->next=v2=getnode_write(3);
V2->next=v3=getnode_write(8);
V3->next=null;

V1=getnode_write(1)
Arr[2]=v1;
V1->next=v2=getnode_write(4);
V2->next=v3=getnode_write(8);
V3->next=null;

V1=getnode_write(1)
Arr[3]=v1;
V1->next=v2=getnode_write(7);
V2->next=null;

V1=getnode_write(6)
Arr[4]=v1;
V1->next=v2=getnode_write(7);
V2->next=null;

V1=getnode_write(5)
Arr[5]=v1;
V1->next=null;

V1=getnode_write(4)
Arr[6]=v1;
V1->next=v2=getnode_write(5);
V2->next=v3=getnode_write(8);
V3->next=null;

V1=getnode_write(1)
Arr[7]=v1;
V1->next=v2=getnode_write(2);
V2->next=v3=getnode_write(3);
V3->next=v3=getnode_write(7);
V4->next=null;

Dfs(1,arr);
For(i=0;i<max;i++)
Del(arr[i]);

Getch();
}
Void dfs(int v ,struct node**p)
{
Struct node *q;
Visited[v-1]=true;
Printf(“%d\t”,v);

Q=*(p+v-1);

While(q!=null)
{
If(visited->data-1]==false)
Dfs(q->data,p);
Else q=q->next;
}
Struct node*getnode_write(int val)
{
Struct node *newnode;
Newnode=(struct node*) malloc(sizeof(struct node));
Newnode->data=val;
Return newnode;
}
Void del(struct node*n)
{
Struct node*temp;
While(n!=null){
Temp=n->next;
Free(n);
N=temp;
}
}

Program to implement dqeues in arrays.


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

#define max 10
Void addatqatbeg(int*,int,int*,int*);
Void addatqatend(int*,int,int*,int*);
Void addatqatbeg(int*,int*,int*);
Void addatqatbend(int*,int*,int*);
Void display(int*);
Int count(int*);

Void main();
{
Int arr[max];
Int front,rear,I,n;

Clrscr();
Front=rear=-1;
For(i=0;max;i++)
Arr[i]=0;

Addq(arr,17,&front,&rear);
Addq(arr,10,&front,&rear);
Addq(arr,8,&front,&rear);
Addq(arr,-9,&front,&rear);
Addq(arr,13,&front,&rear);
Addq(arr,28,&front,&rear);
Addq(arr,14,&front,&rear);
Addq(arr,5,&front,&rear);
Addq(arr,25,&front,&rear);
Addq(arr,6,&front,&rear);
Addq(arr,21,&front,&rear);
Addq(arr,11,&front,&rear);

Printf(“\nleelment in deque” );
Display (arr);
N=count(arr);
Printf(“\n total number of element indeque,%d”,n);

I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);

I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);

I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);

I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);

Print(“\n lement in the deque after deletion”)


Display(arr);

Addqatend(arr,16,&front,&rear);
Addqatend(arr,7,&front,&rear);

Print(“\n lement in the deque after addition”)


Display(arr);

I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);

I=delqatbeg(arr,&front,&rear);
Printf(“item extracted %d”,i);

Print(“\n lement in the deque after deletion”)


Display(arr);

N=count(arr);
Printf(“\n total number of element in deqeue: %d”,n);

Getch();
}
Void addqatbeg(int*arr,int ietm,int,int *pfront,int *prear)
{
Int I,,k,c;
If(*pfront==0&&*prear==max-1)
{
Printf(“\ndeque is full.\n”);
Return;
}
If(pfront==-1)
{
*pfront=*prear=0;
Arr[*pfront]=item;
Return;
}
If(*prear!=max-1)
{
C=count(arr);
K=*prear+1;
For(i=1;i<=c;i++)
{
Arr[k]=arr[k-1]
;
k--;
}
Arr[k]=item;
*pfront=k;
(prear)++;
}
Else
{
(*prear)--;
Arr[*pfront]=item;
}

Void addq(int*arr,int item,int*pfront,int*pear)


{
Int I,k;
{
Printf(“\n dqeue is full”);
Return;
}

If(*pfront==-1)
{
*prear=*pfront=0;
Arr[*prear]=item;
Return;
}
I(*rear==max-1)
{
K=*pfront-1for(i=*pfront-1;i<*prear;i++)
{
K=I;
If(k==max-1)
Arr[k]=0;
}
(*prear)--;
(*pfront--;
}
(*prear)++;
Arr[*prear]=item;
}

int addqatbeg(int*arr, int,int *pfront,int *prear)


{
Int item;
If (*pfront==-1)
{
Printf(“\n qeue is empty’);
Return 0;
}
item =arr[*pfront]
Arr[*pfront]=0;

If(*pfront==*prear)]
*pfront=*prear=-1;
Else
(*pfront)++;

Return item;
}

int addqatbeg(int*arr, int,int *pfront,int *prear)


{
Int item;
If (*pfront==-1)
{
Printf(“\n qeue is empty’);
Return 0;
}
item =arr[*pfront]
Arr[*pfront]=0;

(prear)--;
If (*pfront==-1)

(*pfront)=-1;

Return item;
}
Void display(int&arr
{
Int j;

Printf(“\n front->”);
For(i=0;i<max;i++)
Printf(“\t%d”arr[j]);
Printf(“<-rear”);
}

Int count(int*arr)
{
Int c=0;

For(i=0;i<max;i++)
{
If (Arr[i]!=0)
C++;
}
Return c;
}

Program to insert and delete nodes from a singly linked list at

Inserting a node at the Beginning:-

Algorithm:-
1.[cheque overflow ?]
If ptr =null,then
Print , overflow
Exit
Else
Ptr =(node*)malloc(size of node)).
End it
2. set ptr_> info=item
3.Set ptr->nxt=start
4.set start=ptr

The c code for above algorithms:-

Void insertat begine(int item)


{
Node*p;
P=(node*)malloce(sizeof(node))
p->info=item;
if(start== null);
p->next=null;
else
p-.next*p= start;
start=*p;
}

Inserting a node at the end:-

Algorithm:-

1.[cheque overflow ?]
If ptr =null,then
Print , overflow
Exit
Else
Ptr =(node*)malloc(size of node)).
End it
2. set ptr_> info=item
3.Set ptr->next=null
4.if start =null and if then set start=p;
5.set loc=start
6.repeat step 7 untill loc-next!=null
7.set loc=loc-next
8.set loc-next=p

The c code for above algorithms:-

Void insert_at _end(int item)


{
Node*p,*loc;
P=(node*)malloce(sizeof(node))
p->info=item;
if(start== null);
p->next=null;
if(start== null)
{start=p:}
Else
{
Loc=start;
While(loc-> next!=null)
{oc=loc->next=null}
Loc->next=p
}
}
Inserting a node at the specified position:-

Algorithm:-
1.[cheque overflow ?]
If ptr =null,then
Print , overflow
Exit
Else
Ptr =(node*)malloc(size of node)).
End if
2. set ptr_> info=item
3.Set ptr->next=null then
Set start =p
Setp-> next=null
End if
4.inilisize the counter (1) and pointers
(node* temp)
Set i=o
Set temp=start
5.repeat step6 and 7 untill 1< next
7.stepi=i+1
8.set p->next=temp->next
9.set temp->next=p;

The c code for above algorithms:-

Void insert_spe(int item,int loc)


{
Node*p,*temp;
Int k;
For(k=o,temp=start;k<loc;k++
{
Temp=temp->next;
If(tep==null);
{
Printf(“node in the list at less then one\n”);
Return;
}
}
P=(node*)mallo(sizeof(node));
p->info=item;
p->next=loc->next;;
loc->next=p;
}

deletiona node at the Beginning:-

Algorithm:-

1.[check for under flow ?]


If start =null ,then
Print”lined list empty’
Exit
End if
2.set ptr=start
3.set start=start->next
4.print element deleted is,prs->next
5.free (ptr)

The c code for above algorithms:-

Voiddele_beg(void)
{
Node*p;
If(start= =null);
(Return);
Else
{
P=start;
Start=start->next;
Printf(“element deleted is=y.d”)p->num;
Free(p);
}

deletiona node at the end:-

Algorithm:-

1.[check for under flow ?]


If start =null ,then
Print”lined list empty’
Exit
End if
2.if start-.next=null,then;
Set ptr=start
Set start =null
Print element deleted is =ptr->info
Free (ptr
End if
3. Set ptr=start
4.repeat steps 5 and 6 will ptr ->next !=null
5.set loc=ptr
6.set ptr=ptr ->next
7.set loc->next=null
8.free(ptr);

The c code for above algorithms:-

Void dele_end()
{
Node*p,*loc;
(return:)
Else if (start->next= =null)
{
P=stat
Start=null
Printf(“element deleted is =%d”,p->num
Free(p);
}
Else
{
Loc=start;
while(P=start->next=null)
{
Loc=start->next;
}
Loc->next=null;
Free(p);
}
}

Deletion node at the specified position:-

Algorithm:-
1.[check for under flow?]
If ptr==null ,then
Print “underflow”
Exit
2.[initialize the countr , 1 and pointer]
Node *temp,node*ptr;
Set i=0
Set*ptr=start
3.repeat step 4 to9 untill i<=loc
4.set temp=ptr
5.set ptr=ptr-.next
6.set i=i+1
7.print”element deleted is”,
pts=tyle8.set temp->next=ptr->new
9.free (ptr).

The c code for above algorithms:-

Dele_spe()
{
Node8p,*temp;
Int I,loc;
Printf(“enter the position to delet\n”);
Scnf(“y.d”,& loc);
If(start==num)
{
Printf(“empty list”)
}
Else
{
Ptr= start;
For(i=1;I,=loc;i++)
{
Temp=ptr->next;
}
Printf(“no deleted is=%d”,pts->num)
Temp->next=ptr->next;
Free(ptr);
}
}

You might also like