Subject | Download link |
DATA STRUCTURES C (all schemes) | |
Discrete Mathematic Structures (all schemes) | |
Electronic Circuits (all schemes) | |
Logic Design (all schemes) | |
Object Oriented programming with c++ (old scheme) | |
OOP with C++ (old scheme) | |
Unix & Shell programming (new scheme) |
if u have any contents to share with others pls mail it to mycollectionsmanoj@gmail.com
Showing posts with label 3 sem. Show all posts
Showing posts with label 3 sem. Show all posts
Monday, September 20, 2010
3 sem CS/IS old question paper
Wednesday, December 17, 2008
ld e notes( mux )
This ppt contains some worked out problems and circuit diagrams of fast adder
download
download
Sunday, November 30, 2008
unix e-notes
unix e-notes chap 1
Sunday, November 16, 2008
recursion-1
Write a recursive C program for
a] Searching an element on a given list of integers using binary search method.
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
int binary_search(int,int a[],int,int); /*FUNCTION PROTOTYPE*/
void main() /*MAIN FUNCTION*/
{
int item,i,n,pos; /*VARIABLE DECLARATION*/
int *a;
clrscr();
printf("\n Enter the no. of elements:\n");
scanf("%d",&n);
a=(int *)malloc(sizeof(int) *n);
printf("\nEnter the elements:\n");
for(i=0;i< pos="binary_search(item,a,0,n-1);" pos="="">high)
return-1;
mid=(low+high)/2;
if(item==a[mid])
return mid;
else
{
if(item< style="font-weight: bold;">
b] Solving the tower of Hanoi problem.

#include"stdio.h"
#include"conio.h"
void towers(int,char,char,char);
int count=0;
main()
{
int n;
clrscr();
printf("\n Enter the no of disks:");
scanf("%d",&n);
towers(n,'a','c','b');
printf("\nTotal no of moves required is %d",count);
getch();
}
void towers(int n,char frompeg,char topeg,char temppeg)
{
if(n==1)
{
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
count++;
return;
}
towers(n-1,frompeg,temppeg,topeg);
count++;
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
towers(n-1,temppeg,topeg,frompeg);
}
a] Searching an element on a given list of integers using binary search method.
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
int binary_search(int,int a[],int,int); /*FUNCTION PROTOTYPE*/
void main() /*MAIN FUNCTION*/
{
int item,i,n,pos; /*VARIABLE DECLARATION*/
int *a;
clrscr();
printf("\n Enter the no. of elements:\n");
scanf("%d",&n);
a=(int *)malloc(sizeof(int) *n);
printf("\nEnter the elements:\n");
for(i=0;i< pos="binary_search(item,a,0,n-1);" pos="="">high)
return-1;
mid=(low+high)/2;
if(item==a[mid])
return mid;
else
{
if(item< style="font-weight: bold;">
b] Solving the tower of Hanoi problem.

#include"stdio.h"
#include"conio.h"
void towers(int,char,char,char);
int count=0;
main()
{
int n;
clrscr();
printf("\n Enter the no of disks:");
scanf("%d",&n);
towers(n,'a','c','b');
printf("\nTotal no of moves required is %d",count);
getch();
}
void towers(int n,char frompeg,char topeg,char temppeg)
{
if(n==1)
{
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
count++;
return;
}
towers(n-1,frompeg,temppeg,topeg);
count++;
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
towers(n-1,temppeg,topeg,frompeg);
}
binary tree
Program 13:
Write a C program:
a] To construct a binary search tree of integers.
b] To traverse the tree using all methods i.e., inorder , preorder and postorder.
c] To display the elements in the tree.
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
#include"alloc.h"
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *Node;
/*FUNCTION PROTOTYPE DECLARATION*/
Node insert(int,Node);
void preorder(Node);
void inorder(Node);
void postorder(Node);
void main()
{
Node root=NULL; /*VARIBLE DECLARATION*/
int choice,item,ch;
clrscr();
do{ /*DO-WHILE LOOP*/
printf("1:Insert item\n");
printf("2:Preorder\n");
printf("3:Inorder\n");
printf("4:Postorder\n");
printf("5:Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to be inserted\n");
scanf("%d",&item);
root=insert(item,root);
break;
case 2:if(root==NULL)
printf("Tree is Empty\n");
else
{
printf("Preorder Traversal\n");
preorder(root);
printf("\n");
}
break;
case 3:if(root==NULL)
printf("Tree is Empty\n");
else
{
printf("Inorder Traversal\n");
inorder(root);
printf("\n");
}
break;
case 4:if(root==NULL)
printf("Tree is Empty\n");
else
{
printf("Postorder Traversal\n");
postorder(root);
printf("\n");
}
break;
case 5:exit(1);
break;
default:printf("Invalid Choice\n");
} /*END OF SWITCH STATEMENT*/
printf("Do you want to continue(y/n)?:");
ch=getche();
}
while(ch!='n'); /*END OF DO-WHILE LOOP*/
getch();
}
/*FUNCTION TO ALLOCATE MEMORY DYNAMICALLY*/
Node getnode()
{
Node temp;
temp=(Node)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Out of Memory\n");
exit(1);
}
return temp;
}
/*FUNCTION TO INSERT ELEMENTS INTO THE TREE*/
Node insert(int item,Node root)
{
Node temp,p,q;
temp=getnode();
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
return temp;
p=q=root;
while(item!=p->info && q!=NULL)
{
p=q;
if(item< p->info)
q=p->llink;
else
{
q=p->rlink;
}
}
if(item==p->info)
printf("%d is Duplicate\n",item);
else
{
if(item< p->info)
p->llink=temp;
else
p->rlink=temp;
}
return root;
}
/*FUNCTION TO TRAVERSE THE TREE USING PREORDER*/
void preorder(Node root)
{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->llink);
preorder(root->rlink);
}
}
/*FUNCTION TO TRAVERSE THE TREE USING INORDER*/
void inorder(Node root)
{
if(root!=NULL)
{
inorder(root->llink);
printf("%d\t",root->info);
inorder(root->rlink);
}
}
/*FUNCTION TO TRAVERSE THE TREE USING POSTORDER*/
void postorder(Node root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->info);
}
}
Write a C program:
a] To construct a binary search tree of integers.
b] To traverse the tree using all methods i.e., inorder , preorder and postorder.
c] To display the elements in the tree.
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
#include"alloc.h"
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *Node;
/*FUNCTION PROTOTYPE DECLARATION*/
Node insert(int,Node);
void preorder(Node);
void inorder(Node);
void postorder(Node);
void main()
{
Node root=NULL; /*VARIBLE DECLARATION*/
int choice,item,ch;
clrscr();
do{ /*DO-WHILE LOOP*/
printf("1:Insert item\n");
printf("2:Preorder\n");
printf("3:Inorder\n");
printf("4:Postorder\n");
printf("5:Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to be inserted\n");
scanf("%d",&item);
root=insert(item,root);
break;
case 2:if(root==NULL)
printf("Tree is Empty\n");
else
{
printf("Preorder Traversal\n");
preorder(root);
printf("\n");
}
break;
case 3:if(root==NULL)
printf("Tree is Empty\n");
else
{
printf("Inorder Traversal\n");
inorder(root);
printf("\n");
}
break;
case 4:if(root==NULL)
printf("Tree is Empty\n");
else
{
printf("Postorder Traversal\n");
postorder(root);
printf("\n");
}
break;
case 5:exit(1);
break;
default:printf("Invalid Choice\n");
} /*END OF SWITCH STATEMENT*/
printf("Do you want to continue(y/n)?:");
ch=getche();
}
while(ch!='n'); /*END OF DO-WHILE LOOP*/
getch();
}
/*FUNCTION TO ALLOCATE MEMORY DYNAMICALLY*/
Node getnode()
{
Node temp;
temp=(Node)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Out of Memory\n");
exit(1);
}
return temp;
}
/*FUNCTION TO INSERT ELEMENTS INTO THE TREE*/
Node insert(int item,Node root)
{
Node temp,p,q;
temp=getnode();
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
return temp;
p=q=root;
while(item!=p->info && q!=NULL)
{
p=q;
if(item< p->info)
q=p->llink;
else
{
q=p->rlink;
}
}
if(item==p->info)
printf("%d is Duplicate\n",item);
else
{
if(item< p->info)
p->llink=temp;
else
p->rlink=temp;
}
return root;
}
/*FUNCTION TO TRAVERSE THE TREE USING PREORDER*/
void preorder(Node root)
{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->llink);
preorder(root->rlink);
}
}
/*FUNCTION TO TRAVERSE THE TREE USING INORDER*/
void inorder(Node root)
{
if(root!=NULL)
{
inorder(root->llink);
printf("%d\t",root->info);
inorder(root->rlink);
}
}
/*FUNCTION TO TRAVERSE THE TREE USING POSTORDER*/
void postorder(Node root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->info);
}
}
doubly linked list
Write a C program to support the following operations on a doubly linked list where each node consists of integers.
a] Create a doubly linked list by adding each node at the front.
b] Insert new nodes to the left of the node whose key value is read as an input
c] Delete the node of the given data, if it is found, otherwise display appropriate message.
d] Display the contents of the file.
Program:
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
#include"alloc.h"
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *nod;
/*FUNCTION PROTOTYPE DECLARATION*/
nod getnode();
void freenode(nod);
nod insertf(int,nod);
nod insertat(int,nod);
nod del(int,nod);
void display(nod);
void main()
{
nod head; /*VARIABLE DECLARATION*/
int item,ch,choice;
clrscr();
head=getnode();
head->info=0;
head->rlink=head->llink=head;
do /*DO-WHILE LOOP*/
{
printf("1:Insert front\n");
printf("2:Insert to left of given node\n");
printf("3:Delete a node\n");
printf("4:Display\n");
printf("5:Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to insert\n");
scanf("%d",&item);
head=insertf(item,head);
break;
case 2:printf("Enter the key\n");
scanf("%d",&item);
head=insertat(item,head);
getch();
break;
case 3:printf("Enter the item to be deleted\n");
scanf("%d",&item);
head=del(item,head);
getch();
break;
case 4:display(head);
getch();
break;
} /*END OF SWITCH STATEMENT*/
}
while(choice!=5); /*END OF DO-WHILE LOOP*/
getch();
}
/*FUNCTION TO ALLOCATE MOEMORY DYNAMICALLY*/
nod getnode()
{
nod temp;
temp=(nod)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Memory not available\n");
exit(0);
}
return temp;
}
/*FUNCTION TO DEALLOCATE MEMORY OCCUPIED BY A NODE*/
void freenode(nod p)
{
free(p);
}
/*FUNCTION TO INSERT A NODE AT THE FRONT*/
nod insertf(int item,nod head)
{
nod temp;
nod cur;
temp=getnode();
temp->info=item;
cur=head->rlink;
head->rlink=temp;
temp->llink=head;
temp->rlink=cur;
cur->llink=temp;
return head;
}
/*FUNCTION TO INSERT A NEW NODE TO THE LEFT OF A NODE*/
nod insertat(int item,nod head)
{
nod temp,cur,prev;
if(head->rlink==head)
{
printf("List is empty\n");
return head;
}
cur=head->rlink;
while(cur!=head&&item!=cur->info)
cur=cur->rlink;
if(cur==head)
{
printf("Key not found\n");
return head;
}
prev=cur->llink;
temp=getnode();
printf("Enter the item to insert towards left of %d:",item);
scanf("%d",&temp->info);
prev->rlink=temp;
temp->llink=prev;
temp->rlink=cur;
cur->llink=temp;
return head;
}
/*FUNCTION TO DELETE A NODE OF GIVEN DATA*/
nod del(int item,nod head)
{
nod prev,cur,next;
if(head->rlink==head)
{
printf("List is Empty\n");
return head;
}
cur=head->rlink;
while(cur!=head&&item!=cur->info)
cur=cur->rlink;
if(cur==head)
{
printf("\nItem not found\n");
return head;
}
prev=cur->llink;
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
freenode(cur);
return head;
}
/*FUNCTION TO DISPLAY THE CONTENTS OF THE LIST*/
void display(nod head)
{
nod temp;
if(head->rlink==head)
{
printf("\n list is empty\n");
return;
}
temp=head->rlink;
printf("\nList Contents\n");
while(temp!=head)
{
printf("%d\t",temp->info);
temp=temp->rlink;
}
printf("\n");
return;
}
a] Create a doubly linked list by adding each node at the front.
b] Insert new nodes to the left of the node whose key value is read as an input
c] Delete the node of the given data, if it is found, otherwise display appropriate message.
d] Display the contents of the file.
Program:
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
#include"alloc.h"
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *nod;
/*FUNCTION PROTOTYPE DECLARATION*/
nod getnode();
void freenode(nod);
nod insertf(int,nod);
nod insertat(int,nod);
nod del(int,nod);
void display(nod);
void main()
{
nod head; /*VARIABLE DECLARATION*/
int item,ch,choice;
clrscr();
head=getnode();
head->info=0;
head->rlink=head->llink=head;
do /*DO-WHILE LOOP*/
{
printf("1:Insert front\n");
printf("2:Insert to left of given node\n");
printf("3:Delete a node\n");
printf("4:Display\n");
printf("5:Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to insert\n");
scanf("%d",&item);
head=insertf(item,head);
break;
case 2:printf("Enter the key\n");
scanf("%d",&item);
head=insertat(item,head);
getch();
break;
case 3:printf("Enter the item to be deleted\n");
scanf("%d",&item);
head=del(item,head);
getch();
break;
case 4:display(head);
getch();
break;
} /*END OF SWITCH STATEMENT*/
}
while(choice!=5); /*END OF DO-WHILE LOOP*/
getch();
}
/*FUNCTION TO ALLOCATE MOEMORY DYNAMICALLY*/
nod getnode()
{
nod temp;
temp=(nod)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Memory not available\n");
exit(0);
}
return temp;
}
/*FUNCTION TO DEALLOCATE MEMORY OCCUPIED BY A NODE*/
void freenode(nod p)
{
free(p);
}
/*FUNCTION TO INSERT A NODE AT THE FRONT*/
nod insertf(int item,nod head)
{
nod temp;
nod cur;
temp=getnode();
temp->info=item;
cur=head->rlink;
head->rlink=temp;
temp->llink=head;
temp->rlink=cur;
cur->llink=temp;
return head;
}
/*FUNCTION TO INSERT A NEW NODE TO THE LEFT OF A NODE*/
nod insertat(int item,nod head)
{
nod temp,cur,prev;
if(head->rlink==head)
{
printf("List is empty\n");
return head;
}
cur=head->rlink;
while(cur!=head&&item!=cur->info)
cur=cur->rlink;
if(cur==head)
{
printf("Key not found\n");
return head;
}
prev=cur->llink;
temp=getnode();
printf("Enter the item to insert towards left of %d:",item);
scanf("%d",&temp->info);
prev->rlink=temp;
temp->llink=prev;
temp->rlink=cur;
cur->llink=temp;
return head;
}
/*FUNCTION TO DELETE A NODE OF GIVEN DATA*/
nod del(int item,nod head)
{
nod prev,cur,next;
if(head->rlink==head)
{
printf("List is Empty\n");
return head;
}
cur=head->rlink;
while(cur!=head&&item!=cur->info)
cur=cur->rlink;
if(cur==head)
{
printf("\nItem not found\n");
return head;
}
prev=cur->llink;
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
freenode(cur);
return head;
}
/*FUNCTION TO DISPLAY THE CONTENTS OF THE LIST*/
void display(nod head)
{
nod temp;
if(head->rlink==head)
{
printf("\n list is empty\n");
return;
}
temp=head->rlink;
printf("\nList Contents\n");
while(temp!=head)
{
printf("%d\t",temp->info);
temp=temp->rlink;
}
printf("\n");
return;
}
queue implementation in lists(lap prog 11)
11.Write a C program using dynamic variables and pointers to construct a queue of integers using a singly linked list to perform the following operations:
a. Insert
b. Delete
c. Display
It must print appropriate messages for queue full and empty.
Program:
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/*/
#include"conio.h"
#include"alloc.h"
#define QSIZE 3 /*SYMBOLIC CONSTANTS*/
int count=0;
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int data;
struct node *next;
};
typedef struct node *Node;
/*FUNCTION PROTOTYPE DECLARATION*/
Node insert_rear(int,Node);
Node delete_front(Node);
Node getnode();
void freenode(Node);
void display(Node);
void main()
{
int item,ch,choice; /*VARIABLE DECLARATION*/
Node first=NULL;
clrscr();
do{ /*DO-WHILE LOOP*/
printf("\n1:INSERT, 2:DELETE, 3:DISPLAY, 4:EXIT\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to be inserted\n");
scanf("%d",&item);
first=insert_rear(item,first);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
case 4:exit(1);
break;
} /*END OF SWITCH STATEMENT*/
printf("Do you want to continue?(y/n):\n");
ch=getche();
}
while(ch!='n'); /*END OF DO-WHILE LOOP*/
getch();
}
/*FUNCTION TO ALLOCATE MEMORY DYNAMICALLY*/
Node getnode()
{
Node temp;
temp=(Node)malloc(sizeof(struct node));
return temp;
}
/*FUNCTION TO DEALLOCATE MEMORY OCCUPIED BY A NODE*/
void freenode(Node p)
{
free(p);
}
/*FUNCTION TO INSERT A NODE AT THE FRONT END*/
Node insert_rear(int item,Node first)
{
Node temp,cur;
temp=getnode();
if(count==QSIZE)
{
printf("Queue Full\n");
return first;
}
else
{
count++;
/*temp=getnode();*/
temp->data=item;
temp->next=NULL;
if(first==NULL)
return temp;
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=temp;
return first;
}
}
/*FUNCTION TO DELETE A NODE FROM THE END*/
Node delete_front(Node first)
{
Node temp;
if(first==NULL)
{
printf("Queue is Empty\n");
}
else
{
count--;
temp=first;
first=first->next;
printf("Deleted data is %d\n",temp->data);
freenode(temp);
}
return first;
}
/*FUNCTION TO DISPLAY THE CONTENTS OF THE QUEUE*/
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("Queue is Empty\n");
}
else
{
printf("Queue status:\n");
temp=first;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
}
a. Insert
b. Delete
c. Display
It must print appropriate messages for queue full and empty.
Program:
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/*/
#include"conio.h"
#include"alloc.h"
#define QSIZE 3 /*SYMBOLIC CONSTANTS*/
int count=0;
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int data;
struct node *next;
};
typedef struct node *Node;
/*FUNCTION PROTOTYPE DECLARATION*/
Node insert_rear(int,Node);
Node delete_front(Node);
Node getnode();
void freenode(Node);
void display(Node);
void main()
{
int item,ch,choice; /*VARIABLE DECLARATION*/
Node first=NULL;
clrscr();
do{ /*DO-WHILE LOOP*/
printf("\n1:INSERT, 2:DELETE, 3:DISPLAY, 4:EXIT\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to be inserted\n");
scanf("%d",&item);
first=insert_rear(item,first);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
case 4:exit(1);
break;
} /*END OF SWITCH STATEMENT*/
printf("Do you want to continue?(y/n):\n");
ch=getche();
}
while(ch!='n'); /*END OF DO-WHILE LOOP*/
getch();
}
/*FUNCTION TO ALLOCATE MEMORY DYNAMICALLY*/
Node getnode()
{
Node temp;
temp=(Node)malloc(sizeof(struct node));
return temp;
}
/*FUNCTION TO DEALLOCATE MEMORY OCCUPIED BY A NODE*/
void freenode(Node p)
{
free(p);
}
/*FUNCTION TO INSERT A NODE AT THE FRONT END*/
Node insert_rear(int item,Node first)
{
Node temp,cur;
temp=getnode();
if(count==QSIZE)
{
printf("Queue Full\n");
return first;
}
else
{
count++;
/*temp=getnode();*/
temp->data=item;
temp->next=NULL;
if(first==NULL)
return temp;
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=temp;
return first;
}
}
/*FUNCTION TO DELETE A NODE FROM THE END*/
Node delete_front(Node first)
{
Node temp;
if(first==NULL)
{
printf("Queue is Empty\n");
}
else
{
count--;
temp=first;
first=first->next;
printf("Deleted data is %d\n",temp->data);
freenode(temp);
}
return first;
}
/*FUNCTION TO DISPLAY THE CONTENTS OF THE QUEUE*/
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("Queue is Empty\n");
}
else
{
printf("Queue status:\n");
temp=first;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
}
Wednesday, November 12, 2008
Sunday, November 9, 2008
lab program 10
Write a C program using dynamic variables and pointers to construct a stack of integers using singly linked list to perform the following operations:
a.Push
b.Pop
c.Display
It must print appropriate messages for Stack overflow and empty.
#include"stdio.h" /* Including header files*/
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
#include"string.h"
struct node /* Structure template declaration*/
{
int data;
struct node *next;
};
typedef struct node *Node;
Node getnode(); /*function prototype*/
void freenode(Node);
Node insertf(int,Node);
Node delete_info(Node);
void display (Node);
int top=-1;
void main()
{
Node first=NULL;
int item,ch,choice;
clrscr();
do{
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.exit\n");
printf("\n enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the items to be inserted:");
scanf("%d",&item);
first=insertf(item,first);
break;
case 2:
first=delete_info(first); /*delete function call*/
break;
case 3:
display(first); /*display function call*/
break;
case 4:
exit(0);
break;
}
printf("\n do you want to continue...(y/n)");
ch=getche();
}while(ch!='n');
getch();
}
Node getnode() /*getnode function defination*/
{
Node temp;
temp=(Node)malloc(sizeof(struct node));/*creating node using malloc function*/
return temp;
}
void freenode(Node p)
{
free(p); /*removing a node using free function*/
}
Node insertf(int item,Node first) /*insertf function defination*/
{
Node temp; /* pointer temp to type Node*/
temp=getnode();
top++;
temp->data=item;
temp->next=first;
first=temp;
return(first);
}
Node delete_info(Node first)
{
Node temp;
if(first==NULL)
{
printf("\nstack overflow");
}
else
{
top--;
temp=first;
first=first->next;
printf("\n deleted data is %d",temp->data);
freenode(temp);
}
return first;
}
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("\nstack is empty");
}
else
{
printf("\n stack status");
temp=first;
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->next;
}
}
}
a.Push
b.Pop
c.Display
It must print appropriate messages for Stack overflow and empty.
#include"stdio.h" /* Including header files*/
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
#include"string.h"
struct node /* Structure template declaration*/
{
int data;
struct node *next;
};
typedef struct node *Node;
Node getnode(); /*function prototype*/
void freenode(Node);
Node insertf(int,Node);
Node delete_info(Node);
void display (Node);
int top=-1;
void main()
{
Node first=NULL;
int item,ch,choice;
clrscr();
do{
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.exit\n");
printf("\n enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the items to be inserted:");
scanf("%d",&item);
first=insertf(item,first);
break;
case 2:
first=delete_info(first); /*delete function call*/
break;
case 3:
display(first); /*display function call*/
break;
case 4:
exit(0);
break;
}
printf("\n do you want to continue...(y/n)");
ch=getche();
}while(ch!='n');
getch();
}
Node getnode() /*getnode function defination*/
{
Node temp;
temp=(Node)malloc(sizeof(struct node));/*creating node using malloc function*/
return temp;
}
void freenode(Node p)
{
free(p); /*removing a node using free function*/
}
Node insertf(int item,Node first) /*insertf function defination*/
{
Node temp; /* pointer temp to type Node*/
temp=getnode();
top++;
temp->data=item;
temp->next=first;
first=temp;
return(first);
}
Node delete_info(Node first)
{
Node temp;
if(first==NULL)
{
printf("\nstack overflow");
}
else
{
top--;
temp=first;
first=first->next;
printf("\n deleted data is %d",temp->data);
freenode(temp);
}
return first;
}
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("\nstack is empty");
}
else
{
printf("\n stack status");
temp=first;
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->next;
}
}
}
link lists lab program
Write a C program using dynamic variables and pointers, to construct a singly linked list containing of the following information in each node: student ID(int), name(string) and semester(int). The operations to be supported are:
a] The insertion operation:
i] At the front of the list
ii] At the back of the list
iii] At any position of the list
b] Deleting a node based on student ID. If the specified node is not present in the list, an error message to be displayed.
c] Searching a node based on student ID and update the information content. If the specified node is not present in the list, an error message to be displayed.
d] Displaying all nodes of a file.
#include"stdio.h" /* Including header files*/
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
#include"string.h"
struct node /* Structure template declaration*/
{
char name[20];
int id,sem;
struct node *next;
};
typedef struct node *Node;
Node getnode(); /*function prototype*/
void freenode(Node);
Node insertf(struct node,Node);
Node insertr(struct node,Node);
Node insert_at(struct node,int,Node);
Node delete_info (int,Node);
void search_up(int,Node);
void display (Node);
Node create(struct node *);
void main()
{
Node first=NULL;
struct node s;
int ch,tid,pos;
clrscr();
do /*do while statement*/
{
clrscr();
printf("\n1.Insert at front of list");
printf("\n2.Insert at back of list");
printf("\n3.Insert at any position of list");
printf("\n4.delete a student id");
printf("\n5.Search a Node");
printf("\n6.Display the contents\n7. exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch) /* switch statement begin*/
{
case 1:
create(&s); /*create function call*/
first=insertf(s,first); /*insertf function call*/
break;
case 2:
create(&s);
first=insertr(s,first); /*insertr function call*/
break;
case 3:
create(&s);
printf("\n Enter the position");
scanf("%d",&pos);
first=insert_at(s,pos,first); /*insert_at function call*/
break;
case 4:
printf("\n Enter the student id");
scanf("%d",&tid);
first=delete_info(tid,first); /*delete function call*/
getch();
break;
case 5:
printf("\n Enter the student id to be searched");
scanf("%d",&tid);
search_up(tid,first); /*search_up function call*/
getch();
break;
case 6:
printf("\n Student Information\n");
display(first); /*display function call*/
getch();
break;
case 7:
exit(0);
} /*end of switch statement*/
} /*end of do while loop*/
while(1);
} /*end of function main*/
Node insertf(struct node s,Node first) /*insertf function defination*/
{
Node temp; /* pointer temp to type Node*/
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL)
return(temp);
else
{
temp->next=first;
return(temp);
}
}
Node getnode() /*getnode function defination*/
{
Node temp;
temp=(Node)malloc(sizeof(struct node));/*creating node using malloc function*/
return temp;
}
Node insert_at(struct node s,int pos,Node first)
{
Node temp,prev,cur;
int count;
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL && pos==1)
{
return temp;
}
if(first==NULL)
{
printf("\n invalid position....");
getch();
return first;
}
if(pos==1)
{
temp->next=first;
return temp;
}
prev=NULL;
cur=first;
count=1;
while(cur!=NULL && pos!=count)
{
prev=cur;
cur=cur->next;
count++;
}
if(pos==count)
{
prev->next=temp;
temp->next=cur;
return first;
}
printf("\n invalid position");
getch();
return first;
}
void freenode(Node p)
{
free(p); /*removing a node using free function*/
}
Node insertr(struct node s,Node first) /*insertr function defination*/
{
Node temp,cur;
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL)
return(temp);
else
{
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=temp;
return first;
}
}
Node create(struct node *p) /*create node function defination*/
{
printf("\n Enter student name");
scanf("%s",p->name);
printf("\n Enter student id");
scanf("%d",&p->id);
printf("\n Enter the semester");
scanf("%d",&p->sem);
return;
}
Node delete_info(int id,Node first)
{
Node prev,cur;
if(first==NULL)
{
printf("\n list is empty");
getch();
return first;
}
if(id==first->id)
{
cur=first;
first=first->next;
freenode(cur);
printf("\n deletipn successful");
return first;
}
prev=NULL;
cur=first;
while(cur!=NULL&&id!=cur->id)
{
prev=cur;
cur=cur->next;
}
if(cur==NULL)
{
printf("\n student with given id was not found");
getch();
return first;
}
prev->next=cur->next;
freenode(cur);
printf("\n deletion successful");
return first;
}
void search_up(int id,Node first)
{
Node prev,cur;
int choice,ch;
if(first==NULL)
{
printf("\n list is empty");
getch();
return;
}
cur=first;
while(cur!=NULL&&id!=cur->id)
{
cur=cur->next;
}
if(cur==NULL)
{
printf("\n student with given id was not found");
getch();
}
else
{
printf("\nstudent found");
printf("\n wnt to update...(y/n)");
ch=getche();
if(ch=='y')
{
printf("\n update field...");
printf("\n1:Name");
printf("\n2:Id");
printf("\n3:Sem");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n enter the new name:");
scanf("%s",&cur->name);
break;
case 2:printf("\n enter the new ud:");
scanf("%d",&cur->id);
break;
case 3:printf("\n enter the new sem:");
scanf("%s",&cur->sem);
break;
defualt:printf("\n invalid choice");
}
}
}
}
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("\n list is empty");
return;
}
temp=first;
printf("%20s%10s%5s\n","student name","id","sem");
printf("________________________________________\n");
while(temp!=NULL)
{
printf("%20s",temp->name);
printf("%10d",temp->id);
printf("%5d",temp->sem);
temp=temp->next;
printf("\n");
}
}
a] The insertion operation:
i] At the front of the list
ii] At the back of the list
iii] At any position of the list
b] Deleting a node based on student ID. If the specified node is not present in the list, an error message to be displayed.
c] Searching a node based on student ID and update the information content. If the specified node is not present in the list, an error message to be displayed.
d] Displaying all nodes of a file.
#include"stdio.h" /* Including header files*/
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
#include"string.h"
struct node /* Structure template declaration*/
{
char name[20];
int id,sem;
struct node *next;
};
typedef struct node *Node;
Node getnode(); /*function prototype*/
void freenode(Node);
Node insertf(struct node,Node);
Node insertr(struct node,Node);
Node insert_at(struct node,int,Node);
Node delete_info (int,Node);
void search_up(int,Node);
void display (Node);
Node create(struct node *);
void main()
{
Node first=NULL;
struct node s;
int ch,tid,pos;
clrscr();
do /*do while statement*/
{
clrscr();
printf("\n1.Insert at front of list");
printf("\n2.Insert at back of list");
printf("\n3.Insert at any position of list");
printf("\n4.delete a student id");
printf("\n5.Search a Node");
printf("\n6.Display the contents\n7. exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch) /* switch statement begin*/
{
case 1:
create(&s); /*create function call*/
first=insertf(s,first); /*insertf function call*/
break;
case 2:
create(&s);
first=insertr(s,first); /*insertr function call*/
break;
case 3:
create(&s);
printf("\n Enter the position");
scanf("%d",&pos);
first=insert_at(s,pos,first); /*insert_at function call*/
break;
case 4:
printf("\n Enter the student id");
scanf("%d",&tid);
first=delete_info(tid,first); /*delete function call*/
getch();
break;
case 5:
printf("\n Enter the student id to be searched");
scanf("%d",&tid);
search_up(tid,first); /*search_up function call*/
getch();
break;
case 6:
printf("\n Student Information\n");
display(first); /*display function call*/
getch();
break;
case 7:
exit(0);
} /*end of switch statement*/
} /*end of do while loop*/
while(1);
} /*end of function main*/
Node insertf(struct node s,Node first) /*insertf function defination*/
{
Node temp; /* pointer temp to type Node*/
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL)
return(temp);
else
{
temp->next=first;
return(temp);
}
}
Node getnode() /*getnode function defination*/
{
Node temp;
temp=(Node)malloc(sizeof(struct node));/*creating node using malloc function*/
return temp;
}
Node insert_at(struct node s,int pos,Node first)
{
Node temp,prev,cur;
int count;
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL && pos==1)
{
return temp;
}
if(first==NULL)
{
printf("\n invalid position....");
getch();
return first;
}
if(pos==1)
{
temp->next=first;
return temp;
}
prev=NULL;
cur=first;
count=1;
while(cur!=NULL && pos!=count)
{
prev=cur;
cur=cur->next;
count++;
}
if(pos==count)
{
prev->next=temp;
temp->next=cur;
return first;
}
printf("\n invalid position");
getch();
return first;
}
void freenode(Node p)
{
free(p); /*removing a node using free function*/
}
Node insertr(struct node s,Node first) /*insertr function defination*/
{
Node temp,cur;
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL)
return(temp);
else
{
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=temp;
return first;
}
}
Node create(struct node *p) /*create node function defination*/
{
printf("\n Enter student name");
scanf("%s",p->name);
printf("\n Enter student id");
scanf("%d",&p->id);
printf("\n Enter the semester");
scanf("%d",&p->sem);
return;
}
Node delete_info(int id,Node first)
{
Node prev,cur;
if(first==NULL)
{
printf("\n list is empty");
getch();
return first;
}
if(id==first->id)
{
cur=first;
first=first->next;
freenode(cur);
printf("\n deletipn successful");
return first;
}
prev=NULL;
cur=first;
while(cur!=NULL&&id!=cur->id)
{
prev=cur;
cur=cur->next;
}
if(cur==NULL)
{
printf("\n student with given id was not found");
getch();
return first;
}
prev->next=cur->next;
freenode(cur);
printf("\n deletion successful");
return first;
}
void search_up(int id,Node first)
{
Node prev,cur;
int choice,ch;
if(first==NULL)
{
printf("\n list is empty");
getch();
return;
}
cur=first;
while(cur!=NULL&&id!=cur->id)
{
cur=cur->next;
}
if(cur==NULL)
{
printf("\n student with given id was not found");
getch();
}
else
{
printf("\nstudent found");
printf("\n wnt to update...(y/n)");
ch=getche();
if(ch=='y')
{
printf("\n update field...");
printf("\n1:Name");
printf("\n2:Id");
printf("\n3:Sem");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n enter the new name:");
scanf("%s",&cur->name);
break;
case 2:printf("\n enter the new ud:");
scanf("%d",&cur->id);
break;
case 3:printf("\n enter the new sem:");
scanf("%s",&cur->sem);
break;
defualt:printf("\n invalid choice");
}
}
}
}
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("\n list is empty");
return;
}
temp=first;
printf("%20s%10s%5s\n","student name","id","sem");
printf("________________________________________\n");
while(temp!=NULL)
{
printf("%20s",temp->name);
printf("%10d",temp->id);
printf("%5d",temp->sem);
temp=temp->next;
printf("\n");
}
}
Subscribe to:
Posts (Atom)





















