Saturday, 3 September 2022

AVL TREE IN C LANGUAGE

IMPLEMENTATION OF AVL TREES INSERTION, DELETION OPERATIONS

Exercise.No:8

TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL: Code with u - YouTube



AIM: 

To write the program to implement AVL Tree and its operation. 

ALGORITHM: 

For Insertion: 



Step 1: First, insert a new element into the tree using BST's (Binary Search Tree) insertion 

 logic. 

Step 2: After inserting the elements you have to check the Balance Factor of each node. 

Step 3: When the Balance Factor of every node will be found like 0 or 1 or -1 then the 

 algorithm will proceed for the next operation. 

Step 4: When the balance factor of any node comes other than the above three values then the 

 tree is said to be imbalanced. Then perform the suitable Rotation to make it balanced 

 and then the algorithm will proceed for the next operation. 

For Deletion: 



Step 1: Firstly, find that node where k is stored 

Step 2: Secondly delete those contents of the node (Suppose the node is x) 

Step 3: Claim: Deleting a node in an AVL tree can be reduced by deleting a leaf. There are 

 three possible cases: 

• When x has no children then, delete x 

• When x has one child, let x' becomes the child of x. 

• Notice: x' cannot have a child, since sub trees of T can differ in height by at 

most one then replaces the contents of x with the contents of x' 

• then delete x' (a leaf) 

• then find x's successor z (which has no left child) 

• then replace x's contents with z's contents, and 

• delete z 

Step 4: When x has two children, 

AVL Tree Rotations: 

In AVL tree, after performing every operation like insertion and deletion we need to check 

the balance factor of every node in the tree. If every node satisfies the balance factor 

condition then we conclude the operation otherwise we must make it balanced. We use 

rotation operations to make the tree balanced whenever the tree is becoming imbalanced due 

to any operation. 

Single Left Rotation (LL Rotation): 

In LL Rotation every node moves one position to left from the current position. 

Single Right Rotation (RR Rotation): 

In RR Rotation every node moves one position to right from the current position. 

Left Right Rotation (LR Rotation): 

The LR Rotation is combination of single left rotation followed by single right rotation. In 

LR Rotation, first every node moves one position to left then one position to right from the 

current position. 

Right Left Rotation (RL Rotation) 

The RL Rotation is combination of single right rotation followed by single left rotation. In 

RL Rotation, first every node moves one position to right then one position to left from the 

current position. 

Time Analysis of AVL Trees: 

AVL tree is binary search tree with additional property that difference between height of left 

sub-tree and right sub-tree of any node can’t be more than 1.

PROGRAM:

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

typedef struct node

{

 int data;

struct node *left,*right;

int ht;

}node;

node *insert(node *, int);

node *Delete(node *, int);

void preorder(node *);

void inorder(node *);

int height( node *);

node *rotateright(node *);

node *rotateleft(node *);

node *RR(node *);

node *LL(node *);

node *LR(node *);

node *RL(node *);

int BF(node *);

void main()

{

node *root=NULL;

int x, n, i, op;

do

{

printf("\n1)Create ");

printf("\n2)Insert ");

printf("\n3)Delete ");

printf("\n4)Print ");

printf("\n5)Quit ");

printf("\n Enter Your Choice: ");

scanf("%d",&op);

switch(op)

{

case 1:printf("\n Enter no.of elements:");

scanf("%d",&n);

printf("\n Enter tree data:");

root=NULL;

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

{

scanf("%d",&x);

root=insert(root,x);

}

break;

case 2:printf("\n Enter a data : ");

scanf("%d",&x);

root=insert(root,x);

break;

case 3:printf("\n Enter a data : ");

scanf("%d",&x);

root=Delete(root,x);

break;

case 4: printf("\n Preorder sequence :\n");

preorder(root);

printf("\n Inorder sequence :\n");

inorder(root);

break;

}

}while(op<5);

}

node * insert(node *T, int x)

{

if(T==NULL)

{

T=(node*)malloc(sizeof(node));

T->data=x;

T->left=NULL;

T->right=NULL;

}

else

if(x > T->data) 

{

T->right=insert(T->right,x);

if(BF(T)==-2)

if(x>T->right->data)

T=RR(T);

else

T=RL(T);

}

else

if(x<T->data)

{

T->left=insert(T->left,x);

if(BF(T)==2)

if(x < T->left->data)

T=LL(T);

else

T=LR(T);

}

T->ht=height(T);

return(T);

}

node * Delete(node *T, int x)

{ node *p;

if(T==NULL)

{

return NULL;

}

else

if(x > T->data) 

{

T->right=Delete(T->right,x);

if(BF(T)==2)

if(BF(T->left)>=0)

T=LL(T);

else

T=LR(T);

}

else

if(x<T->data)

{

T->left=Delete(T->left,x);

if(BF(T)==-2)

if(BF(T->right)<=0)

T=RR(T);

else

T=RL(T);

}

else

{

if(T->right !=NULL)

p=T->right;

while(p->left != NULL)

p=p->left;

T->data=p->data;

T->right=Delete(T->right, p->data);

if(BF(T)==2)

if(BF(T->left)>=0)

T=LL(T);

else

T=LR(T);

}

else

return(T->left);

}

T->ht=height(T);

return(T);

}

int height(node *T)

{

int lh, rh;

if(T==NULL)

return(0);

if(T->left==NULL)

lh=0;

else

lh=1+T->left->ht;

if(T->right==NULL)

rh=0;

else

rh=1+T->right->ht;

if(lh>rh)

return(lh);

return(rh);

}

node * rotateright(node *x)

{

node *y;

y=x->left;

x->left=y->right;

y->right=x;

x->ht=height(x);

y->ht=height(y);

return(y);

}

node * rotateleft(node *x)

{

node *y;

y=x->right;

x->right=y->left;

y->left=x;

x->ht=height(x);

y->ht=height(y);

return(y);

}

node * RR(node *T)

{

T=rotateleft(T);

return(T);

}

node * LL(node *T)

{

T=rotateright(T);

return(T);

}

node * LR(node *T)

{

T->left=rotateleft(T->left);

T=rotateright(T);

return(T);

}

node * RL(node *T)

{

T->right=rotateright(T->right);

T=rotateleft(T);

return(T);

}

int BF(node *T)

{

int lh, rh;

if(T==NULL)

return(0);

if(T->left==NULL)

lh=0;

else

lh=1+T->left->ht;

if(T->right==NULL)

rh=0;

else

rh=1+T->right->ht;

return(lh-rh);

}

void preorder(node *T)

{

if(T!=NULL)

{

printf(" %d(Bf=%d)", T->data, BF(T));

preorder(T->left);

preorder(T->right);

}

}

void inorder(node *T)

{

if(T!=NULL)

{

inorder(T->left);

printf(" %d(Bf=%d)", T->data, BF(T));

inorder(T->right);

}

}

OUTPUT:

1)Create:

2)Insert:

3)Delete:

4)Print:

5)Quit:

Enter Your Choice: 1

Enter no.of elements :4

Enter tree data: 2

4

5

6

1)Create:

2)Insert:

3)Delete:

4)Print:

5)Quit:

Enter Your Choice: 4

Preorder sequence:

4(Bf=-1) 2(Bf=0) 5(Bf=-1) 6(Bf=0)

Inorder sequence:

2(Bf=0) 4(Bf=-1) 5(Bf=-1) 6(Bf=0)

1)Create:

2)Insert:

3)Delete:

4)Print:

5)Quit:

Enter Your Choice: 3

Enter a data: 5

1)Create:

2)Insert:

3)Delete:

4)Print:

5)Quit:

Enter Your Choice: 4

Preorder sequence:

4(Bf=0) 2(Bf=0) 6(Bf=0)

Inorder sequence:

2(Bf=0) 4(Bf=0) 6(Bf=0)

1)Create:

2)Insert:

3)Delete:

4)Print:

5)Quit:

Enter Your Choice: 2

Enter a data: 43

1)Create:

2)Insert:

3)Delete:

4)Print:

5)Quit:

Enter Your Choice: 4

Preorder sequence:

4(Bf=-1) 2(Bf=0) 6(Bf=-1) 43(Bf=0)

Inorder sequence:

2(Bf=0) 4(Bf=-1) 6(Bf=-1) 43(Bf=0)

1)Create:

2)Insert:

3)Delete:

4)Print:

5)Quit:

Enter Your Choice: 5

Result: 

Thus, the concept of AVL Tree was implemented successfully.

THREE TRAVERSAL & BINARY SEARCH TREE


IMPLEMENTATION TREE TRAVERSAL 

(INORDER – PREORDER – POST ORDER)

Exercise.No:6

NOTE:

    * For any queries comment below, instantly the solution will be posted *

TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL: Code with u - YouTube

AIM: 

To write the program to implement the Tree Traversal. 

ALGORITHM: 

1. Read the integers 

2. Create the functions for preorder, in order and post order 

3. Perform push and pop operations. 

FOR INORDER 

Inorder(pos t) 

T!=null 

Inorder(t-> left) 

Printf(“%s”, t->data); 

Inorder(t->right) 

FOR PREORDER 

Preorder(pos t) 

T!=null 

Printf(“%s”, t->data); 

Preorder(t->left) 

Inorder(t->right) 

FOR POSTORDER 

Postorder(pos t) 

Postorder(t->left) 

postorder(t->right) 

Printf(“%s”, t->data); 

4. Visit in the order left, root, right, 

5. Display the visited nodes

PROGRAM:

// program showing various operations on Expression tree. Tree is created

// from a postfix expression

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

#include<ctype.h>

typedef struct treenode

{

char data;

struct treenode *left,*right;

}treenode;

typedef struct stack

{

treenode *data[20];

int top;

}stack;

void init(stack *s)

{

s->top=-1;

}

treenode * pop(stack *s)

{

treenode *p;

p=s->data[s->top];

s->top=s->top-1;

return(p);

}

void push(stack *s, treenode *p)

{

s->top=s->top+1;

s->data[s->top]=p;

}

treenode *create();

void inorder(treenode *T);

void preorder(treenode *T);

void postorder(treenode *T);

void main()

{

treenode *root=NULL,*p;

int x, op;

do

{

printf("\n\n1)Create\n2)Preorder");

printf("\n3)Inorder\n4)Postorder\n5)Quit");

printf("\n Enter Your Choice :");

scanf("%d",&op);

switch(op)

{

case 1: root=create();break;

case 2: preorder(root);break;

case 3: inorder(root);break;

case 4: postorder(root);break;

}

}while(op<5);

}

void inorder(treenode *T)

{

if(T!=NULL)

{

inorder(T->left);

printf("%c", T->data);

inorder(T->right);

}

}

void preorder(treenode *T)

{ if(T!=NULL)

{ printf("%c", T->data);

preorder(T->left);

preorder(T->right);

}

}

void postorder(treenode *T)

{ if(T!=NULL)

{

postorder(T->left);

postorder(T->right);

printf("%c", T->data);

}

}

treenode * create()

{

char a[50];

int i;

treenode *p,*q,*root;

stack s;

init(&s);

printf("\n Enter a postfix expression : ");

scanf("%s",&a);

for(i=0;a[i]!='\0';i++)

{

if(isalnum(a[i]))

{

p=(treenode*)malloc(sizeof(treenode));

p->left=p->right=NULL;

p->data=a[i];

push(&s,p);

}

else

{

q=pop(&s);

p=pop(&s);

root=(treenode*)malloc(sizeof(treenode));

root->left=p;

root->right=q;

root->data=a[i];

push(&s,root);

}

}

root=pop(&s);

return(root);

}

OUTPUT:

1)Create

2)Preorder

3)Inorder

4)Postorder

5)Quit

Enter Your Choice :1

Enter a postfix expression: pk-ap+*

1)Create

2)Preorder

3)Inorder

4)Postorder

5)Quit

Enter Your Choice :2

*-pk+ap

1)Create

2)Preorder

3)Inorder

4)Postorder

5)Quit

Enter Your Choice :3

p-k*a+p

1)Create

2)Preorder

3)Inorder

4)Postorder

5)Quit

Enter Your Choice :4

pk-ap+*

1)Create

2)Preorder

3)Inorder

4)Postorder

5)Quit

Enter Your Choice :5

RESULT: 

Thus, the program Tree Traversal was implemented and executed successfully.


Thursday, 1 September 2022

POLYNOMIAL ADDITION USING LINKED LIST

 IMPLEMENTATION OF POLYNOMIAL ADDITION USING LINKED LIST

Exercise.No:5 

TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : 

NOTE : 

        In the below content incase of some error problem the program was slightly modified, so to remodel it the following steps should be taken 

            1.)In "co eff" remove the space between "co" and "eff".

            2.)In "print f" remove the space between "print" and "f".

            3.)In "scan f" remove the space between "scan" and "f".

            4.)In "std io. h" remove the space between "std" and "io" then space between "io." and "h".

            5.)In "con io. h" remove the space between "con" and "io" then space between "io." and "h".

            6.)In "p add" remove the space between "p" and "add".

            7.)In "size of" remove the space between "size" and "of".

    * For any queries comment below ,instantly the solution will be posted*

AIM: 

To implement the program for polynomial addition using linked List. 

ALGORITHM: 

1. Using the function poly1() read the coefficient and exponent terms of the first polynomial 

 until exponent term is zero 

2. Using the function poly2() read the coefficient and exponent terms of the second 

 polynomial until exponent term is zero 

3. Using the function polyadd() add the two polynomials with the following comparisons 

4. If the exponent term in the first polynomial is greater than the exponent in the second 

 polynomial, add the node of the first polynomial with the resultant polynomial 

5. If the exponent term in the first polynomial is lees than the exponent in the second 

 polynomial, add the node of the second polynomial with the resultant polynomial 

6. If the exponent term in the first polynomial is equal to the exponent in the second 

 polynomial, add both the coefficient of the first and second polynomial and the node to the 

 resultant polynomial 

7. Traverse both the polynomial according to the above comparison up to the NULL value of 

 both the polynomials are reached. 

8. Display the resultant polynomial 

PROGRAM :

#include <std io. h>

#include <con io. h>

#include <std lib. h>

#include <math. h>

typedef struct node

{ int power;

float co eff;

struct node *next;

}node;

node * insert(node *head, int power, float co eff);

node * create();

node * p add(node *head1,node *head2);

void print(node *head);

int main()

{

node *head1,*head2,*head3;

int op;

float value, x;

print f("\n Enter 1st Polynomial : ");

head1=create();

print(head1);

print f("\n Enter 2nd Polynomial : ");

head2=create();

print(head2);

head3=p add(head1,head2); 

print f("\n Addition of two polynomials : ");

print(head3);

}

node * insert(node *head, int power, float co eff)

{ node *p,*q;

p=(node*)malloc(size of(node));

p->power=power; p->co eff=co eff;

p->next=NULL;

if(head==NULL)

return(p);

else

if(power<head->power)

{ p->next=head;

return(p);

}

else

{ q=head;

while(q->next!=NULL && power>=q->next->power)

q=q->next;

p->next=q->next;

q->next=p;

if(q->power==p->power)

{

q->co eff=q->co eff +p->co eff;

q->next=p->next;

free(p);

}

return(head);

}

}

node * create()

{

int n, I, power;

float co eff;

node *head;

head=NULL;

print f("\n Enter No. of Terms:");

scan f("%d", &n);

print f("\n enter a term as a tuple of (power, coefficient)");

for(I=1;I<=n; I++)

{

scan f("%d %f", &power, &co eff);

head=insert(head, power, co eff);

}

return(head);

}

node * p add(node *head1,node *head2)

{

node *head=NULL;

int power; float co eff;

while(head1 != NULL && head2 != NULL)

{

if(head1->power < head2->power)

{

head=insert(head,head1->power,head1->co eff);

head1=head1->next;

continue;

}

if(head2->power < head1->power)

{ head=insert(head,head2->power,head2->co eff);

head2=head2->next;

continue;

}

head=insert(head,head1->power,head1->coeff+head2->co eff);

head1=head1->next;

head2=head2->next;

}

while(head1!=NULL)

{head=insert(head,head1->power,head1->co eff);

head1=head1->next;

}

while(head2!=NULL)

{head=insert(head,head2->power,head2->co eff);

head2=head2->next;

}

return(head);

}

void print(node *head)

{ print f("\n");

while(head!=NULL)

{

print f("%6.2fX^%d ",head->co eff, head->power);

head=head->next;

}}

OUTPUT :

Enter 1st Polynomial :

Enter No. of Terms:3

enter a term as a tuple of (power, coefficient)3 3 2 2 1 1

1.00X^1 2.00X^2 3.00X^3

Enter 2nd Polynomial :

Enter No. of Terms:2

enter a term as a tuple of (power, coefficient)2 2 5 5

2.00X^2 5.00X^5

Addition of two polynomials :

1.00X^1 4.00X^2 3.00X^3 5.00X^5

RESULT: 

Thus the program for polynomial addition using linked list was implemented and it’s 

executed successfully.

            *Let us promote a thing, "Code with us" YOUTUBE channel doing a coding works for cheap price their services are 
                        1.) APP DEVELOPMENT
                        2.)WEB DEVELOPMENT
                        3.)REVERSE ENGINEERING (Penetration testing)
                        4.)TEACHING PROGRAMMING LANGUAGES
           To reach "Code with us" immediately comment below to book your time slot.

Stack and Queue operation in C language

IMPLEMENTATION OF STACK AND ITS OPERATION

Exercise.No:3 



TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : Code with u - YouTube

AIM: 

To implement the program for Push, Pop and Display operations in Stack. 

ALGORITHM: 

PUSH OPERATION: 

Step 1: If Top=Max-1 

Print “Overflow : Stack is full” and Exit 

End If 

Step 2: Top=Top+1 

Step 3: Stack[TOP]=Element 

Step 4: End 

POP OPEARTION: 

Step 1: If TOP=-1 

Print “Underflow: Stack is empty” and Exit 

End if 

Step 2: Set Del element=Stack[Top] 

Step 3: Top=Top-1 

Step 4: Del Element 

Step 5: End

NOTE : 

        In the below content incase of some error problem the program was slightly modified, so to remodel it the following steps should be taken 

            1.)In "in it" remove the space between "in" and "it".

            2.)In "print f" remove the space between "print" and "f".

            3.)In "scan f" remove the space between "scan" and "f".

            4.)In "std io. h" remove the space between "std" and "io" then space between "io." and "h".

            5.)In "con io. h" remove the space between "con" and "io" then space between "io." and "h".

PROGRAM :

#include<std io. h>

#include<con io. h>

#define MAX 15

typedef struct stack

{

int data[MAX];

int top;

}stack;

stack s;

void in it(stack*s);

void push(stack*s, int x);

void pop(stack*s);

int full(stack*s);

int empty(stack*s);

void print(stack*s);

int main()

{

int op,op1;

do

{

print f("\n\n1)Initialize\n2)Push\n3)Pop\n4)Full");

print f("\n5)Empty\n6)Print\n7)Quit");

print f("\n Enter your Choice:");

scan f("%d", &op);

switch(op)

{

case 1:init(&s);

break;

case 2:printf("\n Enter your element to push : ");

scan f("%d",&op1);

push(&s,op1);

break;

case 3:pop(&s);

break;

case 4:full(&s);

break;

case 5:empty(&s);

break;

case 6:print(&s);

break;

}

}while(op<7);

}

void in it(stack*s)

{

s->top=-1;

}

void push(stack*s, int x)

{

if(s->top==MAX-1)

{

print f("\n Stack is full");

}

else

{

s->top=s->top+1;

s->data[s->top]=x;

}

}

void pop(stack*s)

{

if(s->top==-1)

{

print f("\n Stack is empty");

}

else

{

if(s->top==0)

{

s->top=-1;

}

else

{

s->top=s->top-1;

}

}

}

int empty(stack*s)

{

if(s->top==-1)

{

print f("\n Stack is empty");

}

else

{

print f("\n Stack is not empty");

}

return(0);

}

int full(stack*s)

{

if(s->top==MAX-1)

{

print f("\n Stack is full");

}

else

{

print f("\n Stack is not full");

}

return(0);

}

void print(stack*s)

{

int a;

if(s->top==-1)

{

print f("Stack is empty");

}

else

{

for(a=s->top; a>=0;a--)

{

print f("\n %d", s->data[a]);

}

}

}

OUTPUT :

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:1

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:2

Enter your element to push : 12

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:2

Enter your element to push : 13

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:2

Enter your element to push : 14

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:6

14

13

12

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:3

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:6

13

12

RESULT: 

Thus the program for Push, Pop and Display operations in Stack was implemented and 

executed successfully.


Tuesday, 30 August 2022

Doubly linked list using C programming

 IMPLEMENTATION OF DOUBLY LINKED LIST AND ITS OPERATIONS 

TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : Code with u - YouTube

AIM:

     To implement doubly linked list and performing insert, search, view and delete operations. 

 ALGORITHM:

     1. CREATION:

             a. Creating a node

             b. Reading details for a node from user

             c. Connect the node with the list 

     2. INSERTION:

                                                                                



             a. Get the node using struct node(), and read the details of the node using new node() 

             b. Check whether the list is empty or not 

             c. FIRST- The forward link field of the new node is made to point the data field of the first node in the list by assigning of the first node.

             d. The backward link field of the first node is made to point the data field of the first node in the list by assigning of the new node. 

             e. Assigning the new node as the head pointer

             f. LAST – the forward link field of the last node is made to point the new node by assigning the address of the new node.

             g. The backward link field of the new node is made to point the last node in the list by assigning the address of the new node.

             h. The forward link field of the new node is set to NULL. 

             I. MIDDLE- the forward link field of the new node is made to point the next node in the list by assigning its address

             j. The backward link field of the next node is made to point the new node, by assigning the address of the preceding node

             k. The forward link field of the preceding node is made to point the new node, by assigning the address of the new node.

     3. DISPLAY

             a. Get the contents from the list

             b. If the list is empty print it is empty.

             c. If the list is not empty print the entire list. 

     4. FIND

             a. It finds the address of the given element and returns the address of the particular element.

             b. Returns null if the element is not found.

     5. DELETION

                                                                        



             a. Check whether the list is empty or not.

             b. FIRST – set the head pointer of the second node in the list

             c. Set the backward link filed of the head in the list to NULL

             d. Release the memory for the deleted node

             e. LAST- The link field of the previous node

             f. The forward link field of the previous node is set to NULL

             g. Release the memory for the deleted node

             h. MIDDLE- The forward link field of the previous node is made to point the previous node, by assigning its address 

             I. Release the memory for the deleted node.

                                                                                



NOTE : 

        In the below content incase of some error problem the program was slightly modified, so to remodel it the following steps should be taken 

            1.)In "d node" remove the space between "d" and "node".

            2.)In "print f" remove the space between "print" and "f".

            3.)In "scan f" remove the space between "scan" and "f".

 PROGRAM :

 #include<std io. h>

  #include<con io. h>

#include<std lib. h>

typedef struct d node

 {

 int data;

 struct d node *next,*pre v;

 }d node;

 d node * create()

 {

 int x, n, I;

 d node *head=NULL;

 d node *p;

 print f("\n Enter no of data : ");

 scan f("%d", &n);

 for(I=0;I<n; I++)

{

print f("\n Next Data : ");

scan f("%d", &x);

if(head==NULL)//insert the first node

{

head=p=(d node*)malloc(size of(d node));

p->next=p->p rev=NULL; 

p->data=x;

 }

 else

 {

 p->next=(d node *) malloc(size of(d node));

 p->next->data=x;

 p->next->p rev=p;

 p=p->next;

 p->next=NULL;

 }

 }

 return(head);

 } 

void print(d node *head)

 {

 d node *p;

 print f("\n Data stored in the Doubly linked list : ");

 for(p=head; p!=NULL ; p=p->next) 

print f("%d ",p->data);

 }

 int search(d node *head, int x)

 {

 int I=0;

 d node *p; 

for(p=head; p!=NULL ; p=p->next) 

{

 if(p->data == x) 

return(I);

 I++; 

}

 return -1;

 }

 d node *insert(d node *head, int x, int loc)

 {

 d node *p,*q;

 int I;

 p=(d node*)malloc(size of(d node));

 p->data=x;

 p->next=p->pre v=NULL;

 if(loc==1) // inserting as a first node 

{

 p->next=head;

 head->pre v=p;

 head=p;

 }

 else

 {

 q=head;

 for(I=1; I<loc-1;I++)

if(q->next!=NULL) 

q=q->next;

 else

 { 

print f("\n Overflow **** "); 

return head;

 } //insert a node as next node of q

 p->next=q->next;

 p->pre v=q;

 q->next=p;

 q->next->pre v=q;

 }

 return(head);

 }

 d node *Delete(d node *head, int loc) 

{

 d node *p,*q;

 int I;

 if(loc==1) //Deleting the first node 

{

 p=head;

 head=head->next;

 head->pre v=NULL; 

free(p);

 }

 else

 {

 q=head;

 for(I=1;I<loc; I++)//position q on the node to be deleted

if(q->next==NULL)

 {

 print f("\n Underflow *****");

 return(head);

 }

 else 

q=q->next; 

}

 if(q->next != NULL) 

q->next->pre v=q->pre v;

 q->pre v->next=q->next;

 free(q);

 }

 return(head);

 }

 int main() 

{

 int op, x, loc;

 d node *head=NULL; 

do

 {

 print f("\n\n1)Create\n2)Print\n3)Insert\n4)Delete\n5)Search");

 print f("\n6)Quit");

 print f("\n Enter your Choice : ");

 scan f("%d", &op);

 switch(op)

 { 

case 1: head=create();

break; 

case 2: print(head);

break; 

case 3: print f("Enter the location :"); 

scan f("%d", &loc);

 print f("Enter the data :");

 scan f("%d", &x);

 head=insert(head, x, loc);

break;

 case 4: print f("Enter the location :");

 scan f("%d", &loc); 

head=Delete(head, loc);

break; 

case 5: print f("Enter element to be searched : "); 

scan f("%d", &x);

 loc=search(head, x);

 if(loc==-1) 

print f("\n Element not found ");

 else

 print f("\n Found at location :%d ",loc+1);

 break;

 }

 }

while(op<6);

 }

 OUTPUT : 

1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

Enter your Choice : 1 

Enter no of data : 3 

Next Data : 3

 Next Data : 4

 Next Data : 5 

1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

 Enter your Choice : 2

 Data stored in the Doubly linked list : 3 4 5 


1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

Enter your Choice : 4

 Enter the location :2


1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

Enter your Choice : 2 

Data stored in the Doubly linked list : 3 5


1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

 Enter your Choice : 3

 Enter the location :55

 Enter the data :2

 Overflow **** 

1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit  

Enter your Choice : 5

 Enter element to be searched : 3

 Found at location :1

1)Create

 2)Print

 3)Insert 

4)Delete 

5)Search 

6)Quit 

 Enter your Choice : 6

 RESULT:

             Thus the program for doubly linked List and its operations was implemented and it’s executed successfully.

Single linked list using C programming advance data structure

IMPLEMENTATION OF SINGLY LINKED LIST AND ITS OPERATIONS

           



 TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNELCode with u - YouTube                                                                                                     

 AIM:

     To implement singly linked list and performing insert, search, display and delete operations. 

 ALGORITHM:

     1: CREATION:

                 a. Creating a node b. Reading details for a node from user c. Connect the node with the list 

                                                                    

    

 2. INSERTION:

                 a. Get the node using the structure node (), and read the node details using the new node().

                 b. Check if the list is empty or not 

                 c. FIRST: The binding field of the new node is made to point to the data field of the first node in the list by assigning the first node. 

                 d. The head pointer is made to point the data field of the new one by assigning the address of the new node.

                 e. LAST: The binding field of the last node is made to point to the data field of the first node in the list by assigning the new node. 

                f. The link field of the new node is set to NULL. 

                g. MIDDLE- the binding field of the new node is made to point to the data field of the next node in the list by assigning its address 

                h. The binding field of the next node is made to point to the data field of the new one by assigning the address of the new node. 

                                                                        


    

 3. DELETION:

                 a. Check whether the list is empty or not. 

                 b. FIRST – set the head pointer of the second node in the list 

                 c. Release the memory for the deleted node d. LAST- the link field of the previous node 

                 e. Release the memory for the deleted node 

                 f. MIDDLE- the link field of the previous node 

                 g. Release the memory for the deleted node 

  4. DISPLAY:

                 a. Get the contents from the list 

                 b. If the list is empty print it is empty. 

                 c. If the list is not empty print the entire list. 

PROGRAM : 

 #include<studio. h>

#include<con io. h>

#include<std lib. h>

 typedef struct node 

{

 int data;

 struct node *next;

 }node;

 node *create();

 node *insert b(node *head, int x);

 node *insert e(node *head, int x);

 node *insert in(node *head, int x);

 node *delete b(node *head);

 node *delete e(node *head); 

 node *delete in(node *head);

 void search(node *head);

 void print(node *head);

 int main() 

{

 int op,op1,x;

 node *head=NULL;

 do

 {

 print f("\n\n1)Create\n2)Insert\n3)Delete\n4)Search"); 

print f("\n5)Print\n6)Quit");

 print f("\n Enter your Choice:");

 scan f("%d", &op);

 switch(op)

 {

 case 1:head=create();

 break;

 case 2:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");

 print f("\n Enter your choice : ");

 scan f("%d",&op1);

 print f("\n Enter the data to be inserted : ");

 scan f("%d", &x);

 switch(op1)

 {

 case 1: head=insert b(head, x);

 break; 

case 2: head=insert e(head, x);

 break;

 case 3: head=insert in(head, x);

 break; 

}

 break;

 case 3:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");

 print f("\n Enter your choice : ");

 scan f("%d",&op1);

 switch(op1)

 {

 case 1:head=delete b(head);

 break;

 case 2:head=delete e(head); 

break;

 case 3:head=delete in(head);

 break;

 }

 break;

 case 4:search(head);

 break; 

case 5:print(head);

 break; 

}

 }

while(op<6);

 }

 node *create() 

{

 node *head,*p;

 int I, n; head=NULL;

 print f("\n Enter no of data:");

 scan f("%d", &n);

 print f("\n Enter the data:"); 

for(I=0;

index=(node*)malloc(size of(node)); 

p=p->next;

 }

 p->next=NULL;

 scan f("%d",&(p->data));

 }

 return(head);

 }

 node *insert b(node *head, int x)

 { 

node *p;

 p=(node*)malloc(size of(node));

 p->data=x;

 p->next=head; 

head=p; 

return(head); 

}

 node *insert e(node *head, int x)

 {

 node *p,*q; p=(node*)malloc(size of(node));

 p->data=x;

 p->next=NULL; 

if(head==NULL) 

return(p); //locate the last node 

for(q=head ;q->next!=NULL ;q=q->next) ;

 q->next=p; return(head);

 }

 node *insert in(node *head, int x)

 { 

node *p,*q;

 int y;

 p=(node*)malloc(size of(node));

 p->data=x;

 p->next=NULL;

 print f("\n Insert after which number ? : ");

 scan f("%d", &y); //locate the data 'y' 

for(q=head ; q != NULL && q->data != y ; q=q->next) ;

 if(q!=NULL)

 {

 p->next=q->next; 

q->next=p;

 }

 else

 print f("\n Data not found ");

 return(head);

 }

 node *delete b(node *head)

 {

 node *p,*q; 

if(head==NULL)

 {

 print f("\n Underflow....Empty Linked List");

 return(head);

 }

 p=head;

 head=head->next;

 free(p);

 return(head);

 }

 node *delete e(node *head)

 {

 node *p,*q; 

if(head==NULL) 

{

 print f("\n Underflow....Empty Linked List");

 return(head);

 }

 p=head;

 if(head->next==NULL) 

{

 // Delete the only element 

head=NULL; 

free(p);

 return(head);

 } 

//Locate the last but one node 

for(q=head ;q->next->next !=NULL ;q=q->next) ;

 p=q->next;

 q->next=NULL;

 free(p);

 return(head);

 }

 node *delete in(node *head)

 {

 node *p,*q; int x, I;

 if(head==NULL)

 {

 print f("\n Underflow....Empty Linked List");

 return(head); 

}

 print f("\n Enter the data to be deleted : "); 

scan f("%d", &x);

 if(head->data==x) 

{

 // Delete the first element 

p=head; 

head=head->next;

 free(p);

 return(head);

 }

 //Locate the node previous to one to be deleted

 for(q=head ;q->next->data!=x && q->next !=NULL ;q=q->next ) ;

 if(q->next==NULL)

 {

 print f("\n Underflow.....data not found"); 

return(head);

 }

 p=q->next; 

q->next=q->next->next;

 free(p);

 return(head);

 }

 void search(node *head)

 {

 node *p;

 int data, loc=1;

 print f("\n Enter the data to be searched: ");

 scan f("%d", &data);

 p=head; 

while(p!=NULL && p->data != data)

 {

 loc++;

 p=p->next; 

}

 if(p==NULL) 

print f("\n Not found:");

 else 

print f("\n Found at location=%d", loc);

 }

 void print(node *head)

 {

 node *p;

 print f("\n\n");

 for(p=head ;p!=NULL ;p=p->next) 

print f("%d ",p->data);

 }

 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit 

Enter your Choice:1

 Enter no of data:5

 Enter the data:1 4 7 2 6 


 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit

 Enter your Choice:6 

1 4 7 2 6 


 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit

 Enter your Choice:

5 6 2 7 4 1


 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit

 Enter your Choice:7 


RESULT: 

             Thus the program for singly linked List and its operations was implemented and it’s executed successfully.

FRIENDSHIP & GOALS

WHAT IS FRIENDSHIP:  For all the attention we pay to love stories, some of the most compelling stories (in fiction or not) are about best fr...