Sie sind auf Seite 1von 21

ROBOTICS LAB FILE

Submitted By :
NAME : ` CLASS : Roll No. :

INDEX
S.No. 1. 2. PROGRAM REMARKS Program in C to create a list of five elements and display it using linked list Program in C to implement push, pop, full and empty operations in a stack using link list Program in C to implement add, delete, and empty operations in a queue using link list Program in C to implement a binary tree of n elements and performing the following operations a)Display BT graphically b)Add an element to BT c)Search an element in BT d)Display number of nodes, sum of all nodes and depth of BT Program in C to create a graph of n vertices using adjacency list and perform following operations a)read and display

3.

4.

5.

Linked List

Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers. Why Linked List? Arrays can be used to store linear data of similar types, but arrays have following limitations. 1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage. 2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted. For example, in a system if we maintain a sorted list of IDs in an array id[]. id[] = [1000, 1010, 1050, 2000, 2040]. And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000). Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved. Advantages over arrays 1) Dynamic size 2) Ease of insertion/deletion Drawbacks: 1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists. 2) Extra memory space for a pointer is required with each element of the list. Representation in C: A linked list is represented by a pointer to the first node of the linked list. The first node is called head. If the linked list is empty, then value of head is NULL. Each node in a list consists of at least two parts: 1) data 2) pointer to the next node In C, we can represent a node using structures. Below is an example of a linked list node with an integer data.
struct node { int data; struct node *next; };

EXPERIMENT NO.- 1

#include<conio.h> #include<stdio.h> #include<alloc.h> struct node { int info; struct node *next; }; typedef struct node nodeptr; nodeptr * ptr,*temp,*start=NULL,*end,*s1,*e1,*s2,*e2,*back; int create() { int x; do { temp=(nodeptr*)malloc(sizeof(nodeptr)); printf("\n ENTER VALUE : "); scanf("%d",&temp->info); if(start==NULL) { start=temp; end=start; } else { end->next=temp; end=end->next; end->next=NULL; } printf("\nEnter 999 to terminate "); scanf("%d",&x); } while(x!=999); return 0; }

//header files for code

//structure for defining list

//function for creating list.

//exiting on number 999

int traverse() { if(start==NULL) printf("\nNO ELEMENTS"); else { printf("\nLIST : "); ptr=start; while(ptr!=NULL) { printf(" %d\t",ptr->info); ptr=ptr->next; } } return 0; } int main() { int i,j; clrscr(); create(); traverse(); return 0; } // function to create link list // function to print link list //checking condition for termination

OUTPUT

Stack

Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Mainly the following three basic operations are performed in the stack: Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. Peek: Get the topmost item. How to understand a stack practically? There are many real life examples of stack. Consider the simple example of plates stacked over one another in canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO/FILO order.

EXPERIMENT NO.- 2
#include<stdio.h> #include<stdlib.h> #define MAX 5 int stack[MAX]; void push(); int pop(); void traverse(); int is_empty(); int top_element(); int top = -1; int main() { int element, choice; while(1) { printf("Stack Operations.\n"); printf("1. Insert into stack (Push operation).\n"); printf("2. Delete from stack (Pop operation).\n"); printf("3. Print top element of stack.\n"); printf("4. Check if stack is empty.\n"); printf("5. Traverse stack.\n"); printf("6. Exit.\n"); printf("Enter your choice.\n"); scanf("%d",&choice); switch ( choice ) { case 1: if ( top == MAX_SIZE - 1 ) printf("Error: Overflow\n\n"); else { printf("Enter the value to insert.\n"); scanf("%d",&element); push(element); } break; case 2: if ( top == -1 ) printf("Error: Underflow.\n\n"); else { element = pop(); printf("Element removed from stack is %d.\n", element); } break; case 3: if(!is_empty()) { element = top_element(); printf("Element at the top of stack is %d\n\n", element); } else //for choosing type of operation //header files for code //defining maximum size //function for push operation //function for pop operation //function for traversing stack //checking for emptiness of stack

printf("Stack is empty.\n\n"); break; case 4: if(is_empty()) printf("Stack is empty.\n\n"); else printf("Stack is not empty.\n\n"); break; case 5: traverse(); break; case 6: exit(0); } } } void push(int value) { top++; stack[top] = value; } int pop() { int element; if ( top == -1 ) return top; element = stack[top]; top--; return element; } void traverse() { int d; if ( top == - 1 ) { printf("Stack is empty.\n\n"); return; } printf("There are %d elements in stack.\n", top+1); for ( d = top ; d >= 0 ; d-- ) printf("%d\n", stack[d]); printf("\n"); } int is_empty() { if ( top == - 1 ) return 1;

else return 0; } int top_element() { return stack[top]; }

OUTPUT

Queue

Like Stack, Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of queue is any queue of consumers for a resource where the consumer that came first is served first. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added. Operations on Queue: Mainly the following four basic operations are performed on queue: Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition. Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition. Front: Get the front item from queue. Rear: Get the last item from queue. Applications of Queue: Queue is used when things dont have to be processed immediatly, but have to be processed in First InFirst Out order like Breadth First Search. This property of Queue makes it also useful in following kind of scenarios. 1) When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling. 2) When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.

EXPERIMENT NO. 3

#include<stdio.h> #include<conio.h> #define MAX 10 int queue[MAX],front=-1,rear=-1; void insert_element(); void delete_element(); void display_queue(); int main() { int option; printf(">>> c program to implement queue operations <<<"); do { printf("\n\n 1.Insert an element"); printf("\n 2.Delete an element"); printf("\n 3.Display queue"); printf("\n 4.Exit"); printf("\n Enter your choice: "); scanf("%d",&option); switch(option) { case 1: insert_element(); break; case 2: delete_element(); break; case 3: display_queue(); break; case 4: return 0; } }while(option!=4); } void insert_element() { int num; printf("\n Enter the number to be inserted: "); scanf("%d",&num); if(front==0 && rear==MAX-1)

printf("\n Queue OverFlow Occured"); else if(front==-1&&rear==-1) { front=rear=0; queue[rear]=num; } else if(rear==MAX-1 && front!=0) { rear=0; queue[rear]=num; } else { rear++; queue[rear]=num; } } void delete_element() { int element; if(front==-1) { printf("\n Underflow"); element=queue[front]; if(front==rear) front=rear=-1; } else { if(front==MAX-1) front=0; else front++; printf("\n The deleted element is: %d",element); } } void display_queue() { int i;

if(front==-1) printf("\n No elements to display"); else { printf("\n The queue elements are:\n "); for(i=front;i<=rear;i++) { printf("\t %d",queue[i]); } } }

OUTPUT

Binary Search Tree

Trees: Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are hierarchical data structures. Tree Vocabulary: The topmost node is called root of the tree. The elements that are directly under an element are called its children. The element directly above something is called its parent. For example, a is a child of f and f is the parent of a. Finally, elements with no children are called leaves.
tree ---j / \ f / a \ h k \ z <-- leaves

<-- root

Why Trees? 1. One reason to use trees might be because you want to store information that naturally forms a hierarchy. For example, the file system on a computer 2. Trees (with some ordering e.g., BST) provide moderate access/search (quicker than Linked List and slower than arrays). 3. Trees provide moderate insertion/deletion (quicker than Arrays and slower than Unordered Linked Lists). 4. Like Linked Lists and unlike Arrays, Trees dont have an upper limit on number of nodes as nodes are linked using pointers.

Main applications of trees include: 1. Manipulate hierarchical data. 2. Make information easy to search (see tree traversal). 3. Manipulate sorted lists of data. 4. As a workflow for compositing digital images for visual effects. 5. Router algorithms 6. Form of a multi-stage decision-making (see business chess). Binary Tree: A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child. Binary Tree Representation in C: A tree is represented by a pointer to the topmost node in tree. If the tree is empty, then value of root is NULL. A Tree node contains following parts. 1. Data 2. Pointer to left child 3. Pointer to right child

EXPERIMENT NO.- 4
#include<iostream> #include<vector> using namespace std; struct tree { tree* left=NULL; tree* right=NULL; int data; }; int sum ,co,s; void create(int d,tree *temp) { //variables for sum and no. of nodes //function to create tree //defining tree //header file for code

if(temp->left!=NULL && temp->data>=d) create(d,temp->left); else if(temp->right!=NULL && temp->data<=d) create(d,temp->right); else { if(temp->data>=d) { temp->left=new tree; temp->left->data=d; } else { temp->right=new tree; temp->right->data=d; } } } int depth(tree *temp) { int l,r; if(temp==NULL) return 0; else { //measuring depth of tree

l=depth(temp->left); r=depth(temp->right); if(l>r) return (l+1); else return (r+1);

} } void disp(tree *temp) { if(temp->left!=NULL) disp(temp->left); cout<<temp->data<<"-->"; sum=sum+temp->data; co++; //displaying tree(inorder)

if(temp->right!=NULL) disp(temp->right); } void insert(int d,tree *temp) { if(temp->left==NULL) create(d,temp); else if(temp->right==NULL) create(d,temp); else { if(temp->data>=d) insert(d,temp->left); else insert(d,temp->right); } } char comtree(int c) { int y; y=2^(c)-1; if(y==co) return 'y'; //checking whether tree is complete or not //inserting elements

else return 'n'; } int search(tree *temp,int d) { if(temp->data==d) { return 1; } else { if(temp->data>d) search(temp->left,d); else search(temp->right,d); } } int main() { int n,k,c,y; char x; cout<<"enter no of element->"; cin>>n; vector<int>t(n); for(int i=0;i<n;i++) { cin>>t[i]; } tree *base; base=new tree; base->data=t[0]; for(int i=1;i<n;i++) { create(t[i],base); } disp(base); sum=0;co=0; cout<<endl<<"element for insertion-->"; cin>>k; insert(k,base); //searching element

cout<<"enter element for searching-->"; cin>>y; s=search(base,y); if(s==1) cout<<endl<<"element found"<<endl; else cout<<endl<<"element not found"<<endl;

c=depth(base); disp(base); x=comtree(c); cout<<endl<<"Depth of tree-->"<<c-1; cout<<endl<<"sum of nodes-->"<<sum; cout<<endl<<"no of nodes-->"<<co; if(x=='y') cout<<endl<<"Complete Binary Tree"; else cout<<endl<<"Not Complete Binary Tree"; return 0; }

OUTPUT

Adjacency List

An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be stored in nodes of linked lists. Pros: Saves space O(|V|+|E|) . In the worst case, there can be C(V, 2) number of edges in a graph thus consuming O(V^2) space. Adding a vertex is easier. Cons: Queries like whether there is an edge from vertex u to vertex v are not efficient and can be done O(V).

EXPERIMENT NO.- 5
#include<stdio.h> #include<stdlib.h> struct graph { int data; struct graph *next; }; typedef struct graph node; node *a[10]; int visit[10]; void create(int x,int y) { if(a[x-1]==NULL) { a[x-1]=(node*)malloc(sizeof(node)); a[x-1]->data=y; a[x-1]->next=NULL; } else { node *temp; temp=a[x-1]; while(temp->next!=NULL) { temp=temp->next; } temp->next=(node*)malloc(sizeof(node)); temp->next->data=y; temp->next->next=NULL; }

} int main() { int i=0,j,k,v,e,x,y; printf("enter no of vertices-->"); scanf("%d",&v); printf("\nenter no of edges-->"); scanf("%d",&e);

node *temp;

while(i!=e) { printf("enter connecting vertices-->"); scanf("%d %d",&x,&y); create(x,y); i++;

} for(i=0;i<v;i++) { temp=a[i]; printf("\n"); printf("%d-->",i+1); while(temp!=NULL) { printf("%d-->",temp->data); temp=temp->next;

} } return 0; }

OUTPUT

Das könnte Ihnen auch gefallen