Académique Documents
Professionnel Documents
Culture Documents
No:1
MOVING NODES IN A LINKED LIST
AI M:
Write a program such that there is a function “move” which moves a node forward by
n positions in a linked list.
PROGRAM:
# include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
};
void main()
{
int num,n,i,elem,posi;
struct node *ptr,*temp;
ptr=NULL;
clrscr();
printf("MOVE operation in Link list\n");
printf("---------------------------");
printf("\n");
printf("Enter no of elements:");
scanf("%d",&n);
printf("--> %d",pt->data);
pt=pt->link;
}
}
Output:
AI M:
Write a program to generate a doubly linked list from it‟s singly linked list
representation.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include"malloc.h"
struct node
{
int data;
struct node *prev,*link;
};
struct node1
{
int data;
struct node1 *link;
};
void main()
{
int num,n,i,elem,posi;
struct node *ptr,*temp;
struct node1 *ptr1;
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct bnode
{
int data;
struct bnode *lchild,*rchild;
};
void insert(struct bnode **,int);
void search(struct bnode **,int);
void inorder(struct bnode *);
void main()
{
int i,will,num,n;
struct bnode *root;
root=NULL;
clrscr();
will=1;
printf("\nElement search in BST");
printf("\n---------------------");
printf("\n");
printf("Enter the number of nodes in BST: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf(" Enter the element you want to add: ");
Output:
PROGRAM:
# include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
};
void add(struct node **,int );
void display(struct node *);
void main()
{
int will,num;
clrscr();
struct node *ptr,*temp;
ptr=NULL;
will=1;
printf("Insertion Sort using link list\n");
printf("------------------------------");
printf("\n");
while(will==1)
{
printf(" Enter the element you want to add: ");
scanf("%d",&num);
add(&ptr,num);
display(ptr);
printf("--> %d",pt->data);
pt=pt->link;
}
}
Output:
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <math.h>
void main()
{
int n,i,n1,pos,ele;
clrscr();
printf(" Manipulation in Binary Tree");
printf("\n------------------------");
printf("\n");
printf(" \nEnter the number of nodes in binary tree: ");
scanf(" %d",&n);
n1= pow(2,n)-1;
printf("\nThe maximum number of possible positions: %d\n",n1);
int *a = (int *) malloc (n1 * sizeof(int));
for( i=0;i<n1;i++)
{
a[i]=0;
}
Output:
Nodes position: 1
Element: 5
Nodes position: 6
Element: 3
Nodes position: 3
Element: 7
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include"malloc.h"
struct node
{
int data;
struct node *prev,*link;
};
void add(struct node **,int);
void display(struct node *);
void search(struct node *,int);
void main()
{
int num,n,i,elem,posi;
struct node *ptr,*temp;
ptr=NULL;
clrscr();
printf("Linear search in Doubly link list\n");
printf("----------------------------------------");
printf("\n");
printf("Enter the number ofelements in list:");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
PROGRAM:
# include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
};
void main()
{
int will,num,i;
struct node *ptr,*temp;
void add(struct node **,int );
void display(struct node *);
void reverse(struct node **);
ptr=NULL;
clrscr();
printf("Reverse the Link list\n");
printf("---------------------\n");
printf("\n");
printf("Enter the number of elements for list:");
scanf("%d",&will);
for(i=1;i<=will;i++)
{
printf(" Enter the element you want to add: ");
printf("--> %d",pt->data);
pt=pt->link;
}
}
Output:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct bnode
{
int data;
struct bnode *lchild,*rchild;
};
void insert(struct bnode **,int);
void inorder(struct bnode *);
void main()
{
int i,will,num,n;
struct bnode *root;
root=NULL;
clrscr();
printf("Inorder traversal in BST\n");
printf("------------------------\n");
printf("\n");
printf("Enter number of nodes for BST:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf(" Enter the element you want to add:");
scanf("%d",&num);
12 23 24 34 35 50 60 80
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct bnode
{
int data;
struct bnode *lchild,*rchild;
};
void insert(struct bnode **,int);
void preorder(struct bnode *);
void main()
{
int i,will,num,n;
struct bnode *root;
root=NULL;
clrscr();
printf("Preorder traversal in BST\n");
printf("------------------------\n");
printf("\n");
printf("Enter number of nodes for BST:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf(" Enter the element you want to add:");
scanf("%d",&num);
12 11 5 1 45 34 23 67
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct bnode
{
int data;
struct bnode *lchild,*rchild;
};
void insert(struct bnode **,int);
void postorder(struct bnode *);
void main()
{
int i,will,num,n;
struct bnode *root;
root=NULL;
clrscr();
printf("Postorder traversal in BST\n");
printf("------------------------\n");
printf("\n");
printf("Enter number of nodes for BST:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf(" Enter the element you want to add:");
scanf("%d",&num);
23 34 12 58 90 67 50 45
PROGRAM:
# include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
};
void add(struct node **,int );
void display(struct node *);
void merge(struct node ** list1,struct node ** list2);
int count(struct node *);
void main()
{
int num,n,n2,i,j,elem,posi;
struct node *ptr,*ptr1,*temp;
ptr=NULL;
ptr1=NULL;
clrscr();
printf("Merge two Link lists\n");
printf("--------------------\n");
printf("\n");
printf("Enter no ofelements of List 1:");
scanf("%d",&n)
Output:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
struct node
{
char name[20];
int age;
int salary;
struct node *next;
}*current,*temp,*start,*prev;
void main()
{
int i,*nam,a,s;
clrscr();
start=temp=prev=current=NULL;
printf("Database using Link List\n");
printf("------------------------\n");
printf("\n");
printf("\nEnter the Details");
for(i=0;i<10;i++)
{
printf("\nName of the employee: ");
getch();
}
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <math.h>
void main()
{
int n,i,n1,pos,ele;
int bin,bin2;
clrscr();
printf("\n Checking of similar Binary Trees");
printf("\n---------------------------------");
printf("\n");
printf("\n Enter the number of nodes in binary tree 1:");
scanf(" %d",&n);
n1= pow(2,n)-1;
printf(" The max number of possible position is %d\n",n1);
int *a = (int *) malloc (n1 * sizeof(int));
for( i=0;i<n1;i++)
{
a[i]=0;
}
}
for( i=0;i<n1;i++)
{
printf("\nNodes of Tree 1: \t%d",a[i]);
}
printf(" \nEnter the number of nodes in binary tree 2:");
scanf(" %d",&bin);
bin2= pow(2,bin)-1;
printf("\nThe max number of possible position i %d\n",bin2);
int *b = (int *) malloc (bin2 * sizeof(int));
for( i=0;i<bin2;i++)
{
b[i]=0;
}
for( i=0;i<bin;i++)
{
printf(" \nThe position and Element:\n");
scanf(" %d %d",&pos,&ele);
insert(b,ele,pos);
}
for( i=0;i<bin2;i++)
{
printf("\nNodes of Tree 2 \t%d",b[i]);
}
similar(a,n1,b,bin2);
}
}
}
Nodes of Tree 1:
4 0 0 6 0 0 2
Nodes of Tree 2 :
4 0 0 6 0 0 2
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include"malloc.h"
struct node
{
int data;
struct node *prev,*link;
};
void add(struct node **,int);
void display(struct node *);
void intersect(struct node **,struct node **,struct node **);
void main()
{
int num,n,i,j,elem,posi;
struct node *ptr,*ptr1,*tempy;
ptr=NULL;
ptr1=NULL;
tempy=NULL;
clrscr();
printf("Intersection on two Link lists\n");
printf("-----------------------------------\n");
printf("\n");
printf("\nEnter no ofelements in list 1:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
4--> 8--> 1
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include"malloc.h"
struct node
{
int data;
struct node *prev,*link;
};
struct node1
{
int data;
struct node1 *link;
};
void add(struct node **,int);
void display(struct node *);
void generate(struct node **pt,struct node1 **pt1);
void display1(struct node1 *);
void main()
{
int num,n,i,elem,posi;
struct node *ptr,*temp;
struct node1 *ptr1;
ptr=NULL;
ptr1=NULL;
clrscr();
}
void display(struct node *pt)
{
printf("%d",pt->data);
pt=pt->link;
while(pt!=NULL)
{
printf("<--> %d",pt->data);
pt=pt->link;
}
}
Enter the number of elements you want to insert in DOUBLE link list:4
Enter the element you want to add : 4
4
Enter the element you want to add : 7
4<--> 7
Enter the element you want to add : 2
4<--> 7<--> 2
Enter the element you want to add : 8
4<--> 7<--> 2<--> 8