Professional Documents
Culture Documents
C Assignment No.: 1 Course: Bachelor Of Computer Application (II Semester) Date of Submission at the Learning Centre: 5th april,2012
1. When is Dynamic memory allocation more suitable? Write a simple program using dynamic memory allocation to construct a singly-linked list with following functions: Create () Insert () Remove () Traverse () IsEmpty () IsFull () Ans. Run time allocation of memory #include<stdio.h> #include<conio.h> #include<alloc.h> #define NewNode (Node *)mallioc( sizeof(Node)) Typedef struct Node { int item; struct Node * next; } Node; Node * Create(Node *); void Display(Node *); int Count(Node *); Node * Insert(Node *); Node * Remove(Node *); void Search(Node *); void main() { Node *ptr=NULL,*start=NULL; int ch,cnt; start=Create( start ); Display(start), do
{ printf(\n Singly Linked List Operations \n); printf(1-> Count \n); printf(2-> Display \n); printf(3-> Insert \n); printf(4-> Remove \n); printf(5-> Search \n); printf(\nenter w. choice \n); scanf(%d,&ch); switch( ch) { case 1: printf(\n Number of Nodes = %d \n,Count (start)); break; case 2: Display(start); break; case 3: start= Insert(start); break; case 4: start= Remove(start); break; case 5: Search(start); break; default: printf(Invalid Selection \n); } } while(ch!=0);
} Node *create(Node *s) { Node *tem=NULL, *t1 =NULL; int num; t1-=s; do { printf(\n Enter the element \n); scanf(%d,&num); if(num!=-99) { tmp= New Node; tmp->item= num; tmp->next= NULL; if(s= =NULL ) s=t1=tmp; else { t1->next=tmp; tl=tl->next } } else printf(Linked List Created Successfully \n); } while(num!=-99); return(s); } void Display(Node *) { if(s= = NULL ) printf(MT Linked List \n); else { while(s!=NULL)
{ printf(%d,s->item); s=s->next; } printf(\n); } } int Count(Node *s) { int total= 0; while(s=NULL) { total++; s=s->next, } return(total); } Node *Insert(Node *) { Node *t1 =NULL, *tnp=NewNode; int pos; printf (Enter the position to be inserted \n); scanf (%d,& pos); if(pos>0 && pos <=Count(s)+ 1) { printf(Enter the element to be inserted \n); scanf(%d,& tmp->item); printf(Enter the name \n); scanf (%c, name); if (pos= =l) { tmp->next=s; s=tmp; } else {
t1=s; while(pos>2) { t1 =t1->next; pos--; } tmp->next=t1->next; t1->next=t1->next; } else printf(Invalid position \n); return(s); } Node *Remove(Node *s) { Node *t1 =s, *tmp=NULL; int pos; printf(Enter the position, to be remove \n); scanf(%d,&pos); if(pos>0 && pos<=Count(s)) { If(pos= = 1) { s=s->next, free(tl); } else { while(pos>2) { t1=t1->next, pos--; } tmp=t1->next; t1->next=tmp->next; free(tmp);
} } else printf(Invalid Position \n); return(s); } void Search(Node *s) { int ele; int flag=0; printf(Enter the element to be searched \n); scanf(%d,&ele); if(s!=NULL) { while(s!=NULL) { if(s-.item= =ele) { printf(\n %d is present \n,ele); flag= 1; } s=s-.next; } if(flag=0) printf(Element Not Found \n); } else printf(List is MT, Key element cant be searched \n); }
In the doubly linked list shown in figure (a) the link field of the leftmost node and link field of rightmost node points to NULL. The list shown in fig. (b) is a doubly linked circular list. In this list, the left link of the leftmost node contains address of the left most node. The list shown in fig. (c), is a doubly linked circular list with a header node. In this type of list, the left link of a header node contains of the header node. An empty list with a header node can be represented as shown in fig. (d), where the left link and right link of a header node point to itself. All those problems that can be solved using singly linked lists can be solved using doubly linked lists. It is left to the reader to implement all the problems solved so far, using doubly linked lists and doubly linked circular list. Given any problem let us implement them using doubly linked lists and with a header node. Using a header node, problems can be solved very easily and effectively
3. For the following Graph, Write its equivalent Adjacency list and Adjacency Matrix.
A
D F
A B C D E X F
A B C D E F
F 0 0 0 1 0 0
Adjacency matrix
4. Using Binary Search technique find search for Sweden in the following array of countries.Write complete algorithm.
0 Afghanista n Ans. 1 Australi a 2 Burm a 3 Ceylo n 4 Denmar k 5 Ethiopi a 6 Indi a 7 Switzerlan d 8 Swede n 9 Ugand a