# Name: Rajat Chowdhry Roll Number: 520810922 Learning Centre: 2017 Subject Code: BC0038 Subject: Data structure

Using C Assignment No.: 1 Course: Bachelor Of Computer Application (II Semester) Date of Submission at the Learning Centre: 8th June, 2009

Ques. 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 can’t be searched \n”); }

Ques. 2. Ans.

Discus the doubly linked list with neat diagram.

Ques. 3

For the following Graph, Write its equivalent Adjacency list and Adjacency Matrix.

A

B

D F

C

E

A B C D E X F

B X A B B C D X

Ans. C E X E D X F X D X

A A 1 B 1 C 0 D 0 E 0 F 0

B 0 0 1 1 0 0

C 0 1 0 0 1 0

D 0 1 0 0 1 1

E 0 0 1 1 0 0

F 0 0 0 1 0 0