58
www.pradeepkumar.org //PROGRAM TO IMPLEMENT SINGLY LINKED LIST\\ #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> struct node { int data; struct node *link; }; void append(struct node **,int); void add_at_begin(struct node **,int); void del(struct node **,int); void in_middle(struct node **,int,int); int count(struct node *); void display(struct node *); void main() { int num,loc; char choice; struct node *p;

C and Data Structure notes

  • Upload
    natwarr

  • View
    3.854

  • Download
    1

Embed Size (px)

DESCRIPTION

C and Data Structure notes,Data Structure,Stack,Queue, Linked List,Binary tree,preorder,inorder,postorder traversal,DS with C, C programming source code,C programming examples, C Programming Assignment,lecture

Citation preview

Page 1: C and Data Structure notes

www.pradeepkumar.org

//PROGRAM TO IMPLEMENT SINGLY LINKED LIST\\

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<stdlib.h>

struct node

{

int data;

struct node *link;

};

void append(struct node **,int);

void add_at_begin(struct node **,int);

void del(struct node **,int);

void in_middle(struct node **,int,int);

int count(struct node *);

void display(struct node *);

void main()

{

int num,loc;

char choice;

struct node *p;

Page 2: C and Data Structure notes

p=NULL;

do

{

clrscr();

printf("PROGRAM TO IMPLEMENT SINGLY LINKED LIST\n");

printf("—————————————\n");

printf("\n 1. Create/appending the list");

printf("\n 2. Insert node at begining");

printf("\n 3. Insert node in middle");

printf("\n 4. Deleting a node");

printf("\n 5. Counting the no. of nodes");

printf("\n 6. Displaying the list");

printf("\n 7. Exit");

oper:

gotoxy(1,15);printf(" ");

gotoxy(1,11);printf("\n Enter your choice : ");

choice=getch();

switch(choice)

{

char ans;

case ’1′:

do

{

Page 3: C and Data Structure notes

printf("Enter any number : ");

scanf("%d",&num);

append(&p,num);

printf("Entr more (y/n) : ");

fflush(stdin);

ans=getchar();

}while(ans!=’n');

break;

case ’2′:

printf("Enter the data : ");

scanf("%d",&num);

add_at_begin(&p,num);

break;

case ’3′:

printf("\n Enter the data position : ");

scanf("%d",&loc);

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

scanf("%d",&num);

in_middle(&p,loc,num);

break;

case ’4′:

printf("\n Enter the data u want to delete : ");

scanf("%d",&num);

Page 4: C and Data Structure notes

del(&p,num);

break;

case ’5′:

printf("\n The no. of nodes are %d",count(p));

getch();

break;

case ’6′:

display(p);

getch();

break;

case ’7′:

printf("\n Quiting…….");

getch();

exit(0);

break;

default:

gotoxy(1,15);printf("Invalid choice please Enter correct choice");

getch();

goto oper;

}

}while(choice!=7);

}

void append(struct node **q,int num)

Page 5: C and Data Structure notes

{

struct node *temp,*r;

temp=*q;

if(*q==NULL)

{

temp=(struct node *)malloc(sizeof(struct node));

temp->data=num;

temp->link=NULL;

*q=temp;

}

else

{

temp=*q;

while(temp->link!=NULL)

{

temp=temp->link;

}

r=(struct node *)malloc(sizeof(struct node));

r->data=num;

r->link=NULL;

temp->link=r;

}

}

Page 6: C and Data Structure notes

void display(struct node *q)

{

if(q==NULL)

{

printf("\nEmpty link list can’t display the data");

getch();

goto last;

}

while(q!=NULL)

{

printf("\n%d",q->data);

q=q->link;

}

last:

}

int count(struct node *q)

{

int c=0;

if(q==NULL)

{

printf("Empty link list \n");

getch();

goto last;

Page 7: C and Data Structure notes

}

while(q!=NULL)

{

c++;

q=q->link;

}

last:

return c;

}

void add_at_begin(struct node **q,int num)

{

struct node *temp;

if(*q==NULL)

{

printf("Link list is empty can’t insert");

getch();

goto last;

}

else

{

temp=(struct node *)malloc(sizeof(struct node));

temp->data=num;

temp->link=*q;

Page 8: C and Data Structure notes

*q=temp;

}

last:

getch();

}

void in_middle(struct node **q,int loc,int num)

{

struct node *temp,*n;

int c=1,flag=0;

temp=*q;

if(*q==NULL)

{

printf("\nLink List is empty can’t insert");

getch();

goto last;

}

else

while(temp!=NULL)

{

if(c==loc)

{

n=(struct node *)malloc(sizeof(struct node));

n->data=num;

Page 9: C and Data Structure notes

n->link=temp->link;

temp->link=n;

flag=1;

}

c++;

temp=temp->link;

}

if(flag==0)

{

printf("\nNode specified doesn’t Exist can’t Enter the Data");

getch();

}

else

{

printf("Data Inserted");

getch();

}

last:

getch();

}

void del(struct node **q,int num)

{

if(*q==NULL)

Page 10: C and Data Structure notes

{

printf("\n Empty linked list cant delete the data");

getch();

goto last;

}

else

{

struct node *old,*temp;

int flag=0;

temp=*q;

while(temp!=NULL)

{

if(temp->data==num)

{

if(temp==*q)

*q=temp->link;

else

old->link=temp->link;

free(temp);

flag=1;

}

else

{

Page 11: C and Data Structure notes

old=temp;

temp=temp->link;

}

}

if(flag==0)

printf("\n Data not found…");

else

printf("\n Data deleted…Tap a key to continue");

getch();

}

last:

getch();

}

Sample output:

PROGRAM TO IMPLEMENT SINGLY LINKED LIST

—————————————

1. Create/appending the list

2. Insert node at beginning

3. Insert node in middle

4. Deleting a node

5. Counting the no. of nodes

6. Displaying the list

7. Exit

Page 12: C and Data Structure notes

Enter your choice : Enter any number : 12

Entr more (y/n) : y

Enter any number : 23

Entr more (y/n) : y

Enter any number : 34

Enter more (y/n) :n

Enter your choice :6

12

23

34

Enter your choice :5

The no. of nodes are 3

Enter your choice :4

Enter the data u want to delete : 25

Data not found…

Enter your choice :3

Enter the data position : 2

Enter the data : 00

Data Inserted

Enter your choice :6

12

23

0

Page 13: C and Data Structure notes

34

Enter your choice :7 Quiting…….

QUEUE IMPLEMENTATION USING ARRAYS

#include<stdio.h>

#include<conio.h>

void create(void);

void display(void);

void queue_in(void);

void queue_out(void);

void front_rear(void);

int front,rear;

int q[25];

void create()

{

int n;

printf("Enter the number of elements in the queue \n");

scanf("%d",&n);

printf("Enter the elements \n");

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

{

scanf("%d",&q[rear]);

}

Page 14: C and Data Structure notes

front=0;

}

void display()

{

if(rear!=0)

{

printf("The elements in queue \n");

for(front=0;front<rear;front++)

{

printf("%d",q[front]);

}

front=0;

}

else if(rear==0)

printf("The queue is empty \n");

else if(rear==25)

printf("The queue is full \n");

}

void front_rear()

{

if(rear!=0)

{

printf("the front element is : %d \n",q[front]);

Page 15: C and Data Structure notes

printf("the rear element is : %d \n",q[rear-1]);

}

else

printf("Queue is empty \n");

}

void queue_in()

{

if(rear<25)

{

printf("Enter the elements \n");

scanf("%d",&q[rear]);

++rear;

}

else

printf("Queue is full \n");

}

void queue_out()

{

if(rear!=0)

{

printf("The deleted element is : %d \n ",q[front]);

for(front=0;front<rear-1;++front)

q[front]=q[front+1];

Page 16: C and Data Structure notes

rear–;

front=0;

}

}

int main()

{

int ch=0;

clrscr();

printf("QUEUE MANIPULATION \n");

printf("creation \n");

create();

display();

getch();

do

{

clrscr();

printf("Queue operations \n");

printf("—————- \n");

printf("1. Inserting in to the Queue \n");

printf("2. Deletion from queue \n");

printf("3. Front and rear element \n");

printf("4. Displaying the queue \n");

printf("5. Exit \n");

Page 17: C and Data Structure notes

printf("Enter your choice \n");

scanf("%d\n",&ch);

switch(ch)

{

case 1 :queue_in();

display();

break;

case 2 :queue_out();

display();

break;

case 3 :front_rear();

break;

case 4 :display();

break;

case 5 :printf("END \n");

break;

default: printf("Invalid Entry \n");

}

getch();

}

while(ch!=5);

return (0);

}

Page 18: C and Data Structure notes

Sample output:

QUEUE MANIPULATION

Creation

Enter the number of elements in the queue

4

Enter the elements

1

2

3

4

The elements in queue

1234

Queue operations

—————-

1. Inserting in to the Queue

2. Deletion from queue

3. Front and rear element

4. Displaying the queue

5. Exit

Enter your choice

1

5

Enter the elements

Page 19: C and Data Structure notes

The elements in queue

12345

Enter your choice

3

the front element is : 1

the rear element is : 5

Enter your choice

4

The elements in queue

12345

Enter your choice

2

The deleted element is: 1

The elements in queue

2345

Enter your choice

5

END

QUEUE USING LINKED LIST

#include<stdio.h>

typedef struct node

{

Page 20: C and Data Structure notes

int data;

struct node*link;

}queue;

queue*getnode();

void releasenode(queue*p);

int isfull();

int isempty(queue*front);

void enqueue(queue**frontptr,queue**rearptr,int value);

void dequeue(queue**frontptr,queue**rearptr,int*value);

void peek(queue*front,int*value);

void view(queue*front);

int size(queue*front);

void displaymenu(void);

void main()

{

queue*front=NULL,*rear=NULL;

int choice,item;

displaymenu();

while(1)

{

printf("\n?");

scanf("%d",&choice);

switch(choice)

Page 21: C and Data Structure notes

{

case 1:

if(isfull())

printf("\nQueue overflow on ENQUEUE");

else

{

printf("\nEnter the element:");

fflush(stdin);

scanf("%d",&item);

enqueue(&front,&rear,item);

}

break;

case 2:

if(isempty(front))

printf("\n Queue underflow on DEQUEUE");

else

{

dequeue(&front,&rear,&item);

printf("\nThe dequeued value is %d",item);

}

break;

case 3:

if(!isempty(front))

Page 22: C and Data Structure notes

{

peek(front,&item);

printf("\nThe front value is %d",item);

}

else

printf("\nQueue is empty");

break;

case 4:

printf("\nCount of queue elements=%d",size(front));

break;

case 5:

view(front);

break;

default:

printf("\nEnd of run of your program..");

exit(0);

}

}

}

void displaymenu()

{

printf("\nRepresentation of queue using linked list…");

printf("\n\t1.enqueue");

Page 23: C and Data Structure notes

printf("\n\t2.dequeue");

printf("\n\t3.peek");

printf("\n\t4.size");

printf("\n\t5.view");

printf("\n\t6.exit");

}

void releasenode(queue*p)

{

free(p);

}

queue*getnode()

{

int size;

queue*newnode;

size=sizeof(queue);

newnode=(queue*)malloc(size);

return(newnode);

}

int isempty(queue*front)

{

if(front==NULL)

return 1;

else

Page 24: C and Data Structure notes

return 0;

}

int isfull()

{

queue*newnode;

newnode=getnode();

if(newnode==NULL)

return 1;

releasenode(newnode);

return 0;

}

void enqueue(queue**frontptr,queue**rearptr,int value)

{

queue*newnode;

if(isfull())

{

printf("\nMemory not available");

return;

}

newnode=getnode();

newnode->data=value;

newnode->link=NULL;

if(*frontptr==NULL)

Page 25: C and Data Structure notes

*frontptr=newnode;

else

(*rearptr)->link=newnode;

*rearptr=newnode;

}

void dequeue(queue**frontptr,queue**rearptr,int*value)

{

queue*tempnode;

if(isempty(*frontptr))

return;

tempnode=*frontptr;

*frontptr=(*frontptr)->link;

if(*frontptr==NULL)

*rearptr=NULL;

*value=tempnode->data;

releasenode(tempnode);

}

void peek(queue*front,int*value)

{

if(isempty(front))

{

printf("\nThe queue is empty!!!");

return;

Page 26: C and Data Structure notes

}

*value=front->data;

}

void view(queue*front)

{

if(isempty(front))

{

printf("\nThe queue is empty!!!");

return;

}

printf("\nQueue contains…front->");

while(front!=NULL)

{

printf("%d–>",front->data);

front=front->link;

}

printf("rear/n");

}

int size(queue*front)

{

int count=0;

if(front==NULL)

return count;

Page 27: C and Data Structure notes

for(;front!=NULL;)

{

count++;

front=front->link;

}

return count;

}

Sample output:

Representation of queue using linked list..

1. enqueue

2. dequeue

3. peek

4. size

5. view

6. exit

?2

Queue underflow on DEQUEUE

?3

Queue is empty

?4

Count of queue elements=0

?5

The queue is empty!!!

Page 28: C and Data Structure notes

?1

Enter the element:11

?1

Enter the element:22

?1

Enter the element:33

?5

Queue contains…front->11–>22–>33–>rear/n

?4

Count of queue elements=3

?2

The dequeued value is 11

?6

//STACK IMPLEMENTATION USING ARRAYS\\

#include<stdio.h>

#include<conio.h>

void create(void);

void push(0void);

void pop(void);

void display(void);

void topelement(void);

int a[25];

int top;

Page 29: C and Data Structure notes

void create()

{

int i;

printf("Enter the number of elements in the stack \n");

scanf("%d",&top);

printf("Enter the elements \n");

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

{

scanf("%d",&a[i]);

}

return;

}

void display()

{

int i;

if((top!=0) && (top!=25))

{

printf("The elements in the stack are \n");

for(i=top-1;i>=0;–i)

printf("%d",a[i]);

getch();

}

}

Page 30: C and Data Structure notes

void push()

{

if(top==25)

{

printf("The stack is full \n");

}

else

{

printf("Enter the element \n");

scanf("%d",&aTop);

top++;

}

return;

}

void pop()

{

if(top==0)

{

printf("the stack is empty \n");

}

else

{

printf("The popped element is %d",a[--top]);

Page 31: C and Data Structure notes

}

return;

}

void topelement()

{

int t;

if(top==0)

{

printf("There is no top element \n");

}

else

{

t=top-1;

printf("The top element is %d\n",a[t]);

}

return;

}

void main()

{

int ch;

clrscr();

create();

display();

Page 32: C and Data Structure notes

do{

clrscr();

printf("Stack operations \n");

printf("—————-\n");

printf("1. PUSH \n");

printf("2. POP \n");

printf("3. TOP ELEMENT \n");

printf("4. Displaying the stack \n");

printf("5. Quit \n");

printf("Enter your choice \n");

scanf("%d\n",&ch);

switch(ch)

{

case 1: push();

display();

break;

case 2: pop();

display();

break;

case 3: topelement();

// display();

break;

case 4: display();

Page 33: C and Data Structure notes

break;

case 5: printf("END \n");

break;

default: printf("Invalid Entry \n");

}

getch();

}while(ch !=5);

}

Sample output:

Enter the number of elements in the stack

4

Enter the elements

1

2

3

4

The elements in the stack are

4321

Stack operations

—————-

1. PUSH

2. POP

3. TOP ELEMENT

Page 34: C and Data Structure notes

4. Displaying the stack

5. Quit

Enter your choice

1

0

Enter the element

The elements in the stack are

04321

Enter your choice

3

The top element is 0

Enter your choice

2

The popped element is 0

Enter your choice

1

Enter the element

1

The elements in the stack are

14321

Enter your choice

5

END

Page 35: C and Data Structure notes

STACK USING LINKED LIST

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

typedef struct node

{

int data;

struct node *next;

}stack;

int size();

void view();

int isFull();

int isEmpty();

void displayMenu();

int push(int value);

int pop(int *value);

int peek(int *value);

stack *getNode();

void releaseNode(stack *newnode);

stack *topstk=NULL;

void main()

{

Page 36: C and Data Structure notes

int data,status,choice;

displayMenu();

while(1)

{

printf("\n\n ?");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("\nenter the element:");

fflush(stdin);

scanf("%d",&data);

status=push(data);

if(status == -1)

printf("\n memory is not available…..");

break;

case 2:

status=pop(&data);

if(status == -1)

printf("\nstack is underflow on pop");

else

printf("\n the popped elementis %d",data);

break;

Page 37: C and Data Structure notes

case 3:

status=peek(&data);

if(status == -1)

printf("\nstack is empty!!!");

else

printf("\n the top element is %d",data);

break;

case 4:

printf("\ncurrent stack element = %d",size());

break;

case 5:

view();

break;

default:

printf("\n end of run program");

exit(0);

}

}

}

void displayMenu()

{

printf("\nrepresentation of stack using single linked list");

printf("\n\t1.push");

Page 38: C and Data Structure notes

printf("\n\t2.pop");

printf("\n\t3.peek");

printf("\n\t4.size");

printf("\n\t5.view");

printf("\n\t6.exit");

}

stack *getNode()

{

return((stack *)malloc(sizeof(stack)));

}

void releaseNode(stack *newnode)

{

free(newnode);

}

int push(int value)

{

extern stack *topstk;

stack *newptr;

if(isFull())

return -1;

newptr=getNode();

newptr->data=value;

newptr->next=topstk;

Page 39: C and Data Structure notes

topstk=newptr;

return 0;

}

int pop(int *value)

{

extern stack *topstk;

stack *temp;

if(isEmpty())

return -1;

temp=topstk;

topstk=topstk->next;

*value=temp->data;

releaseNode(temp);

return 0;

}

int peek(int *value)

{

extern stack *topstk;

stack *temp;

if(isEmpty())

return -1;

temp=topstk;

*value=temp->data;

Page 40: C and Data Structure notes

return 0;

}

int isEmpty()

{

extern stack *topstk;

if(topstk==NULL)

return 1;

else

return 0;

}

int isFull()

{

extern stack *topstk;

stack *temp;

temp=getNode();

if(temp==NULL)

return 1;

else

{

releaseNode(temp);

return 0;

}

}

Page 41: C and Data Structure notes

int size()

{

extern stack *topstk;

stack *top;

int count=0;

for(top=topstk;top!=NULL;top=top->next)

count++;

return count;

}

void view()

{

extern stack *topstk;

stack *top;

if(isEmpty())

{

printf("\nthe stack is empty!!!");

return;

}

printf("\n the content of the stack is……TOP");

for(top=topstk;top!=NULL;top=top->next)

printf("–>%d",top->data);

}

Sample output:

Page 42: C and Data Structure notes

Representation of stack using linked list

1. Push

2. Pop

3. Peek

4. Size

5. View

6. Exit

?2

stack underflow on POP

?3

stack is empty

?4

stack is elements=0

?1

enter the element:11

?1

enter the element:22

?5

the content of the stack is……TOPà22à11

?4

stack elements=2

?2

the popped value is 22

Page 43: C and Data Structure notes

?6

end of run of your program

TRAVERSALS

#include<stdio.h>

#include<conio.h>

typedef struct node

{

int data;

struct node*left;

struct node*right;

}tree;

tree* createtree();

tree* insert(tree*,tree*);

void preorder(tree*);

void inorder(tree*);

void postorder(tree*);

void main()

{

int ch;

tree*bt;

clrscr();

bt=createtree();

printf("\n \tBINARY TREE TRAVERSAL \n");

Page 44: C and Data Structure notes

printf("\n \t1.traversals \n");

printf("\n \t2.exit\n");

printf("\n \tenter your choice \n");

scanf("%d",&ch);

if(bt==NULL)

{

printf("\nbinary tree is empty\n");

return;

}

switch(ch)

{

case 1:

printf("\n \t preorder traversal \n");

preorder(bt);

printf("\n \t inorder traversal \n");

inorder(bt);

printf("\n \t postorder traversal \n");

postorder(bt);

break;

case 2:

printf("\n exit");

exit(0);

}

Page 45: C and Data Structure notes

getch();

free(bt);

}

tree* createtree()

{

char ch;

tree *bt=NULL,*temp;

do

{

temp=(tree*)malloc(sizeof(struct node));

printf("\n enter the data\n");

scanf("%d",&temp->data);

temp->left=NULL;

temp->right=NULL;

bt=insert(bt,temp);

fflush(stdin);

printf("\n want to add more data y/n :");

scanf("%c",&ch);

}while((ch==’y')||(ch==’Y'));

return bt;

}

tree* insert(tree *bt,tree *temp)

{

Page 46: C and Data Structure notes

if(bt==NULL)

return temp;

else if(temp->data<bt->data)

bt->left=insert(bt->left,temp);

else if(temp->data>bt->data)

bt->right=insert(bt->right,temp);

else if(temp->data==bt->data)

{

printf("\n data is already existing \n");

return bt;

}

return bt;

}

void inorder(tree *bt)

{

if(bt)

{

inorder(bt->left);

printf("%d",bt->data);

inorder(bt->right);

}

}

void preorder(tree *bt)

Page 47: C and Data Structure notes

{

if(bt)

{

printf("%d",bt->data);

preorder(bt->left);

preorder(bt->right);

}

}

void postorder(tree *bt)

{

if(bt)

{

postorder(bt->left);

postorder(bt->right);

printf("%d",bt->data);

}

}

SAMPLE OUTPUT:

Enter the data

5

Want to add more data y/n: Y

Enter the data

2

Page 48: C and Data Structure notes

Want to add more data y/n: Y

Enter the data

3

Want to add more data y/n: Y

Enter the data

4

Want to add more data y/n: N

BINARY TREE TRAVERSAL

1.traversals

2.exit

Enter your choice

1

Preorder traversal 5234

Inorder traversal 2345

Postorder traversal 4325

QUICK SORT

#include<stdio.h>

#include<conio.h>

#define MAX 25

void input();

void output();

void sort();

void qsort(int a[MAX], int *low, int *up);

Page 49: C and Data Structure notes

int n,i;

int a[MAX];

void input()

{

printf("\nenter the number of elements \n");

scanf("%d",&n);

printf("enter the elements \n");

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

scanf("%d",&a[i]);

}

void output()

{

int i;

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

printf("%d \n",a[i]);

}

void sort()

{

int low=0;

int up=n-1;

qsort(a,&low,&up);

}

void qsort(int a[MAX], int *low, int *up)

Page 50: C and Data Structure notes

{

int i,j,t,mid;

i=*low;

j=*up;

mid=a[(*low+*up)/2];

while(i<=j)

{

while(a[i]<mid)

i++;

while(mid<a[j])

j–;

if(i<=j)

{

t=a[i];

a[i]=a[j];

a[j]=t;

i++;

j–;

}

}

if(*low<j)

qsort(a,low,&j);

if(i<*up)

Page 51: C and Data Structure notes

qsort(a,&i,up);

}

void main()

{

clrscr();

printf("\t \t QUICK SORT ALGORITHM");

input();

printf("\n THE ELEMENTS BEF SORTING ARE \n");

output();

printf("\n THE ELEMENTS AFT SORTING ARE \n");

sort();

output();

getch();

}

SAMPLE OUTPUT:

QUICK SORT ALGORITHM

Enter the number of elements

3

Enter the elements

25

12

09

THE ELEMENTS BEF SORTING ARE

Page 52: C and Data Structure notes

25

12

9

THE ELEMENTS AFT SORTING ARE

9

12

25

DATA STRUCTURES – Q & A

Define Abstract Data Type (ADT).

Abstract data type is a collection of value definitions and the operations on those values.

o Value definitions

o Operator definitions

Define a sequence.

A sequence is an ordered set of elements.

S=<s0,s1,s2…….sn-1>

Write short notes on structures in C.

A basic data structure in C is called as the structure. A Structure is a group of items in which

each item is identified by its own identifier.

Example:

struct nametype

{

char first[10];

int roll;

}sname, ename;

Page 53: C and Data Structure notes

What is the difference between a structure and union in C.

1. Same memory is used for all the members of union. At any time only one member can be

accessed.

2. Individual memory is used for structure members.

Define a Stack.

A stack is an ordered collection of items into which new items may be inserted and from which

items may be deleted at one end called the top of the stack.

It is also called as Last In First Out (LIFO).

What are the primitive operations that are performed on a Stack?

The primitive operations are

Push- inserting an element at the top of the stack

Push(s,i);

Pop – removing an element at the top of the stack

i=pop(s);

How do you implement the Stack definition in C. Use array implementation?

#define STACKSIZE

struct stack

{

int top;

int items[STACKSIZE];

};

Write the steps for implementing the pop operation.

o If the stack is empty, print a warning message and halt execution

o Remove the top element from the stack.

o Return this element to the calling program

What is recursive definition?

Page 54: C and Data Structure notes

An object in terms of simpler case of itself is called recursive definition.

Examples:

· To find the factorial of a given number

· To print the Fibonacci series.

Define a queue.

A queue is a ordered collection of items from which itesm may be deleted at one end (called the

front of the queue) and into which items may be inserted at the other end (called the rear of the

queue). It is also called as First in First out (FIFO).

How the queue is represented in C?

#define MAXQUEUE 100

struct queue

{

int items[MAXQUEUE];

int front, rear;

}q;

Define priority queue. What are the types of Priority queue?

The priority queue is a data structure in which the intrinsic ordering of the elements does

determine the results. There are two types

· ascending priority queue

is a collection of items into which items can be inserted arbitrarily and from which only the

smallest item can be removed.

· descending priority queue

What is the purpose of header node?

Sometimes it is desirable to keep an extra node at the front of the list. Such a node does not

represent an item in the list and is called the header node or a list header.

How to create and delete a dynamic variable in C?

Page 55: C and Data Structure notes

malloc() is a function helpful for creating a dynamic variable.

free() is a function helpful for deleting a dynamic variable.

How to create a node of a singly linked list using dynamic variables?

struct node

{

int info;

struct node *next;

};

typedef struct node *NODEPTR;

16. How to create a node of a Doubly linked list using dynamic variables?

struct node

{

int info;

struct node *next, *previous;

};

typedef struct node *NODEPTR;

17. Define a binary tree.

A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint

subsets. One subset is the left and one subset of the right and the third subset is the root of the

tree.

18. What is a strictly binary tree?

If every non leaf node in a binary tree has nonempty left and right subtrees, the tree is named as

the strictly binary tree.

19. Define traversal in tree and what is the different type of traversals in tree?

To pass through the tree, enumerating each of its nodes once.

Page 56: C and Data Structure notes

Three types of traversals

o preorder traversal

visit the root

Traverse the left subtree in preorder.

Traverse the right subtree in preorder

o inorder traversal

Traverse the left subtree in preorder.

visit the root

Traverse the right subtree in preorder

o postorder traversal

Traverse the left subtree in preorder.

Traverse the right subtree in preorder

visit the root

20. How the binary tree node is represented dynamically?

struct nodetype

{

int info;

struct nodetype *left;

struct nodetype *right;

};

21. What are called leaf nodes?

The nodes which don’t have any sons are called as leaf nodes.

22. What are called internal and external nodes?

The leaf nodes are called as external nodes and the non leaf nodes are called as internal nodes.

23. Define O notation.

To capture the concept of one function becoming proportional to another as it grows, a notation

is which is called as O notation.

24. Define a graph.

A graph consists of a set of nodes (or vertices) and set of arcs (or edges).

Page 57: C and Data Structure notes

25. Define weighted graph.

A number is associated with each arc of a graph is called a weighted graph. The number

associated with the arc is called the weight.

26. Define minimum spanning tree.

Given a connected weighted graph G, it is often desired to create a spanning tree T for G such

that the sum of the weights of the tree edges in T is as small as possible. Such a tree is called

minimum spanning tree.

27. Define Depth First Traversal.

Visits the successors of a visited node before visiting any of its brothers.

In DFT, each visited node is placed in the stack

28. Define Breadth First Traversal.

Visits all successors of a visited node before visiting any successors of any of those successors.

In BFT, each visited node is placed on the queue.

29. What are called Dynamic data structures?

Structures which grow or shrink as the data they hold changes.

Example: Lists

30. Define Binary Search.

A technique for searching an ordered list in which we first check the middle item and – based on

that comparison – "discard" half the data. The same procedure is then applied to the remaining

half until a match is found or there are no more items left.

31. What are the advantages of linked lists?

· Overflow can never occur unless the memory is actually full.

· Insertions and deletions are easier than for contiguous (array) lists.

· With large records, moving pointers is easier and faster than moving the items th

emselves.

32. What are the disadvantages of linked lists?

Page 58: C and Data Structure notes

· The pointers require extra space.

· Linked lists do not allow random access.

· Time must be spent traversing and changing the pointers.

· Programming is typically trickier with pointers.

33. Define Binary Search Trees.

A binary search tree is a binary tree where each node contains a key such that:

· All keys in the left subtree lesser than the key in the root.

· All keys in the right subtree greater the key in the root.

· The left and right subtrees of the root are again binary search trees.

34. What is the difference Data types vs. Data Structures?

· A data type is a well-defined collection of data with a well-defined set of operations on it.

· A data structure is an actual implementation of a particular abstract data type.

Rahul Singh Rathore

Email: [email protected] Blog: http://techtouchindia.blogspot.com Twitter: http://twitter.com/rahul_sr Facebook: http://facebook.com/rahulsr1