SlideShare a Scribd company logo
Data Structures -- Data processing often involves in processing huge volumes of data. Many Companies handle million records of data stored in database. Many ways are formulated to handle data efficiently. -- An User-defined data type is a combination of different primary data types, which represents a complex entity. -- An Abstract Data Type ( A D T ) not only represents a set of complex data objects, but also includes a set of operations to be performed on these objects, defines that how the data objects are organized. -- The group of methods implements a set rules, which defines a logical way of handling data. -- The complex entity along with its group of methods is called Abstract Data Type ( A D T ) . --  Data structure is described as an instance of Abstract Data Type ( ADT ). -- We can define that Data structure is a kind of representation of logical relationship between related data elements. In data structure, decision on the operations such as storage, retrieval and access must be carried out between the logically related data elements. Data Structure Linear Non-Linear Stacks Queues Trees Graphs Linear Lists Some Data structures  Arrays Strings Lists Stacks Queues Trees Graphs Dictionaries Maps Hash Tables Sets Lattice Neural-Nets Some Common Operations on Data structures  Insertion :  adding a new element to the collection. Deletion :  removing an element from a collection. Traversal :  access and examine each element in collection. Search :  find whether an element is present or not.  Sorting :  rearranging elements in a particular order. Merging :  combining two collections into one collection.
Arrays – Linked Lists  What is a Linked List The elements of a linked list are not constrained to be stored in adjacent locations. The individual elements are stored “somewhere” in memory, rather like a family dispersed, but still bound together. The order of the elements is maintained by explicit links between them.  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],The Linked List is a collection of elements called nodes, each node of which stores two items of information, i.e., data part and link field. -- The data part of each node consists  the data record of an entity. -- The link field is a pointer and contains the address of next node. -- The beginning of the linked list is stored in a pointer termed as head which points to the first node.  --  The head pointer will be passed as a parameter to any method, to perform an operation.  -- First node contains a pointer to second node, second node contains a pointer to the third node and so on. -- The last node in the list has its next field set to NULL to mark the end of the list.
struct node { int rollno; struct node *next; }; int main() { struct node *head,*n1,*n2,*n3,*n4; /* creating a new node */ n1=(struct node *) malloc(sizeof(struct node)); n1->rollno=101;  n1->next = NULL; /* referencing the first node to head pointer */ head = n1; /* creating a new node */ n2=(struct node *)malloc(sizeof(struct node)); n2->rollno=102;  n2->next = NULL; /* linking  the second node after first node */ n1->next = n2; /* creating a new node * / n3=(struct node *)malloc(sizeof(struct node));  n3->rollno=104;  n3->next=NULL; /* linking the third node after second node */ n2->next = n3; /* creating a new node */ n4=(struct node *)malloc (sizeof (struct node)); n4->rollno=103;  n4->next=NULL; /* inserting the new node between second node and third node */ n2->next = n4; n4->next = n3; /* deleting n2 node */ n1->next = n4; free(n2); } Creating a Singly Linked List  150 head 150 400 720 910 n1-node n2-node n4-node n3-node 150 n1-node 150 head 150 150 n1-node n2-node 720 150 150 n1-node n2-node 720 910 n3-node 150 head 150 400 720 910 n1-node n2-node n4-node n3-node 101 400 102 720 103 910 104 NULL 101 NULL 101 720 102 NULL 101 720 102 910 104 NULL 101 720 102 720 103 910 104 NULL
struct node  { int data; struct node *next; }; struct node *createnode() { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf("Enter the data : "); scanf("%d",&new->data); new->next = NULL; return new; } void append(struct node **h) { struct node *new,*temp; new = createnode(); if(*h == NULL)  {  *h = new;  return;  } temp = *h; while(temp->next!=NULL) temp = temp->next; temp->next = new; } void display(struct node *p)  { printf("Contents of the List : "); while(p!=NULL)  {  printf("%d",p->data);  p = p->next;  }  } void insert_after(struct node **h)  {  struct node *new,*temp; int k; if(*h == NULL) return;  printf("Enter data of node after which node : "); scanf("%d",&k); temp = *h; while(temp!=NULL && temp->data!=k) temp = temp->next; if(temp!=NULL)  { new=createnode(); new->next = temp->next; temp->next = new; } } void insert_before(struct node **h) { struct node *new,*temp,*prev ;  int k; if(*h==NULL) return; printf("Enter data of node before which node : "); scanf("%d",&k); if((*h)->data == k)  { new = createnode(); new->next = *h; *h = new; return; }  temp = (*h)->next; prev = *h;  Implementing Singly Linked List
while(temp!=NULL && temp->data!=k) { prev=temp;  temp=temp->next; } if(temp!=NULL)  {  new = createnode(); new->next = temp; prev->next = new; } } void delnode(struct node **h)  { struct node *temp,*prev;  int k; if(*h==NULL) return; printf("Enter the data of node to be removed : "); scanf("%d",&k); if((*h)->data==k)  { temp=*h;  *h=(*h)->next; free(temp);  return; } temp=(*h)->next; prev=*h; while(temp!=NULL && temp->data!=k)  { prev=temp; temp=temp->next; }  if(temp!=NULL)  {  prev->next = temp->next; free(temp); } } void search(struct node *h)  { struct node *temp;  int k; if(h==NULL)return; printf("Enter the data to be searched : "); scanf("%d",&k); temp=h;  while(temp!=NULL && temp->data!=k) temp=temp->next; (temp==NULL)?  printf("=>Node does not exist") : printf("=>Node exists");  } void destroy(struct node **h)  { struct node  *p; if(*h==NULL) return; while(*h!=NULL)  { p = (*h)->next; free(*h); *h=p; } printf("  ******Linked List is destroyed******"); } Implementing Singly Linked List ( continued )
int main()  { struct node *head=NULL; int ch; while(1)  { printf(&quot;1.Append&quot;); printf(&quot;2.Display All&quot;); printf(&quot;3.Insert after a specified node&quot;); printf(&quot;4.Insert before a specified node&quot;); printf(&quot;5.Delete a node&quot;); printf(&quot;6.Search for a node&quot;); printf(&quot;7.Distroy the list&quot;); printf(&quot;8.Exit program&quot;); printf(&quot;Enter your choice : &quot;); scanf(&quot;%d&quot;,&ch); switch(ch)  { case 1:append(&head);break; case 2:display(head);break; case 3:insert_after(&head);break; case 4:insert_before(&head);break; case 5:delnode(&head);break; case 6:search(head);break; case 7:destroy(&head);break; case 8:exit(0);break; default :  printf( &quot;Wrong Choice, Enter correct one : &quot;); } } } /* function to sort linked list */ void sort(struct node *h) { struct node *p,*temp; int i, j, n, t, sorted=0; temp=h; for(n=0 ; temp!=NULL ; temp=temp->next) n++; for(i=0;i<n-1&&!sorted;i++)  { p=h; sorted=1; for(j=0;j<n-(i+1);j++)  { if ( p->data > ( p->next )->data )  { t=p->data; p->data =(p->next)->data; (p->next)->data = t; sorted=0; } p=p->next; } } } /* function to count number of node in the list */ int count ( struct node *h) { int i; for( i=0 ; h!=NULL ; h=h->next) i++; return i; } Implementing Singly Linked List ( continued )
Add_Polynomial( list p, list q ) set p, q to point to the two first nodes (no headers) initialize a linked list r for a zero polynomial while p != null and q != null if p.exp > q.exp  create a node storing p.coeff and p.exp insert at the end of list r advance p else if q.exp > p.exp  create a node storing q.coeff and q.exp insert at the end of list r advance q else if p.exp == q.exp  if p.coeff + q.coeff != 0 create a node storing p.coeff + q.coeff and p.exp insert at the end of list r advance p, q end while if p != null  copy the remaining terms of p to end of r else if q != null  copy the remaining terms of q to end of r Algorithm for adding two polynomials in linked lists
[object Object],[object Object],[object Object],[object Object],[object Object],Doubly Linked List A Doubly Linked List is a data structure having an ordered list of nodes, in which each node consists of two pointers. One pointer is to store the address of next node like in singly linked list. The second pointer stores the address of previous node. It is also known as two-way list. The specialty of DLL is that the list can be traversed in forward as well as backward directions. The concept of DLL is also used to representing tree data structures. A B C head tail /* a node in doubly linked list */ struct node  { struct node *prev; int data ; struct node *next; } Tree structure using  Doubly Linked List
A B C D A B D C q p q A B C D p A B C Insertion of node in Doubly Linked List Deletion of node in Doubly Linked List
struct node  { struct node *prev; int data; struct node *next; }; struct node *createnode()  { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf(&quot;Enter the data : &quot;); scanf(&quot;%d&quot;,&new->data); new->prev = NULL; new->next = NULL; return new; } void append(struct node **h)  { struct node *new,*temp; new = createnode(); if(*h == NULL)  { *h = new; return; } temp = *h; while(temp->next!=NULL) temp = temp->next; temp->next = new; new->prev = temp; } void forward_display(struct node *p)  { printf(&quot;Contents of the List : &quot;); while(p!=NULL) { printf(&quot;%d&quot;,p->data); p = p->next; } printf(&quot;&quot;); } void insert_after(struct node **h) { struct node *new,*temp; int k; if(*h == NULL) return; printf(&quot;Enter data of node after which node : &quot;); scanf(&quot;%d&quot;,&k); temp = *h; while(temp!=NULL && temp->data!=k) temp = temp->next; if(temp!=NULL)  { new=createnode(); new->next = temp->next; temp->next = new; new->prev = temp; if(new->next != NULL)   new->next->prev = new; } } Implementing Doubly Linked List
void insert_before(struct node **h) { struct node *new,*temp; int k; if(*h==NULL) return; printf(&quot;Enter data of node before which node : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data == k)  { new = createnode(); new->next = *h; new->next->prev=new; *h = new; return; } temp = *h; while(temp!=NULL && temp->data!=k) { temp=temp->next; } if(temp!=NULL) { new = createnode(); new->next = temp; new->prev = temp->prev; new->prev->next = new; temp->prev = new; } } void delnode(struct node **h) { struct node *temp; int k; if(*h==NULL)  return; printf(&quot;Enter the data of node to be removed : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data==k) { temp=*h; *h=(*h)->next; (*h)->prev=NULL; free(temp); return; } temp=*h; while(temp!=NULL && temp->data!=k) { temp=temp->next; } if(temp!=NULL) { temp->next->prev = temp->prev; temp->prev->next = temp->next; free(temp); } } Implementing Doubly Linked List ( continued )
void search(struct node *h) { struct node *temp; int k; if(h==NULL) return; printf(&quot;Enter the data to be searched : &quot;); scanf(&quot;%d&quot;,&k); temp=h; while(temp!=NULL && temp->data!=k) temp=temp->next; if  (temp==NULL) printf(&quot;=>Node does not exist&quot;) else  printf(&quot;=>Node exists&quot;); } void destroy(struct node **h) { struct node  *p; if(*h==NULL) return; while(*h!=NULL) { p = (*h)->next; free(*h); *h=p; } printf(&quot;  ******Linked List is destroyed******&quot;); } int main()  { struct node *head=NULL; int ch; while(1)  { printf(&quot;1.Append&quot;); printf(&quot;2.Display All&quot;); printf(&quot;3.Insert after a specified node&quot;); printf(&quot;4.Insert before a specified node&quot;); printf(&quot;5.Delete a node&quot;); printf(&quot;6.Search for a node&quot;); printf(&quot;7.Distroy the list&quot;); printf(&quot;8.Exit program&quot;); printf(&quot;Enter your choice : &quot;); scanf(&quot;%d&quot;,&ch); switch(ch)  { case 1:append(&head);break; case 2:forward_display(head);break; case 3:insert_after(&head);break; case 4:insert_before(&head);break; case 5:delnode(&head);break; case 6:search(head);break; case 7:destroy(&head);break; case 8:exit(0);break; default : printf(&quot;Wrong Choice, Enter correct choice : &quot;); } } } Implementing Doubly Linked List ( continued )
Circular Singly Linked List 910 tail 150 400 720 910 n1-node n2-node n3-node n4-node -- Singly Linked List has a major drawback. From a specified node, it is not possible to reach any of the preceding nodes in the list. To overcome the drawback, a small change is made to the SLL so that the next field of the last node is pointing to the first node rather than NULL. Such a linked list is called a circular linked list. -- Because it is a circular linked list, it is possible to reach any node in the list from a particular node. -- There is no natural first node or last node because by virtue of the list is circular. -- Therefore, one convention is to let the external pointer of the circular linked list, tail, point to the last node and to allow the following node to be the first node. -- If the tail pointer refers to NULL, means the circular linked list is empty. Circular Doubly Linked List -- A Circular Doubly Linked List ( CDL ) is a doubly linked list with first node linked to last node and vice-versa. -- The ‘ prev ’ link of first node contains the address of last node and ‘ next ’ link of last node contains the address of first node. -- Traversal through Circular Singly Linked List is possible only in one direction. -- The main advantage of  Circular Doubly Linked List  ( CDL ) is that, a node can be inserted into list without searching the complete list for finding the address of previous node. -- We can also traversed through CDL in both directions, from first node to last node and vice-versa. 101 400 102 720 103 910 104 150 prev data next prev data next prev data next prev data next
void insert_after(struct node **t) { struct node *new,*temp; int k, found=0; if(*t == NULL) return; printf(&quot;Enter data of node after which node : &quot;); scanf(&quot;%d&quot;,&k); if((*t)->data==k)  { new = createnode(); new->next = (*t)->next; (*t)->next = new; *t=new; return; } temp=(*t)->next; while(temp!=*t)  { if(temp->data == k)  { new = createnode(); new->next = temp->next; temp->next = new; found=1; break; } temp=temp->next; } if(found==0) printf(&quot;Node does not exist..&quot;); } Implementing Circular Singly Linked List struct node  { int data;  struct node *next; }; struct node *createnode()  { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf(&quot;Enter the data : &quot;); scanf(&quot;%d&quot;,&new->data); new->next = NULL; return new; } void append(struct node **t)  { struct node *new,*head; new = createnode(); if(*t == NULL)  { *t = new;  new->next = *t; return; } head = (*t)->next; (*t)->next = new; new->next = head; *t = new; } void display(struct node *t) { struct node *temp = t->next, *head=t->next; printf(&quot;Contents of the List : &quot;);  do  { printf(&quot;%d&quot;,temp->data);temp = temp->next; }while(temp!=head); printf(“”); }
void insert_before(struct node **t)  { struct node *new,*temp,*prev,*head; int k,found=0; if(*t==NULL) return; printf(&quot;Enter data of node before which node : &quot;); scanf(&quot;%d&quot;,&k); head=(*t)->next; if(head->data == k)  { new = createnode(); new->next = head; (*t)->next = new; return; } temp = head->next; prev = head; while(temp!=head)  { if(temp->data==k)  { new = createnode(); prev->next = new; new->next = temp; found=1; break; } else {   prev=temp;   temp=temp->next; } } if(found==0)  printf(&quot;Node does not exist..&quot;); } void delnode(struct node **t) { struct node *temp,*prev,*head; int k,found=0; if(*t==NULL) return; printf(&quot;Enter the data of node to be removed : &quot;); scanf(&quot;%d&quot;,&k); head=(*t)->next; if(head->data==k)  { temp=head; if(temp->next!=head)  (*t)->next=head->next; else  *t = NULL; free(temp); return; } temp=head->next;  prev=head; while(temp!=head)  { if(temp->data == k)  { prev->next = temp->next; if(temp==*t)  *t = prev; free(temp); found=1; break; }  else  { prev=temp; temp=temp->next; } } if(found==0)  printf(&quot;Node does not exist..&quot;); } Implementing Circular Singly Linked List ( continued )
int main()  { struct node *tail=NULL; int ch; while(1)  { printf(&quot;1.Append&quot;); printf(&quot;2.Display All&quot;); printf(&quot;3.Insert after a specified node&quot;); printf(&quot;4.Insert before a specified node&quot;); printf(&quot;5.Delete a node&quot;); printf(&quot;6.Exit program&quot;); printf(&quot;Enter your choice : &quot;); scanf(&quot;%d&quot;,&ch); switch(ch) { case 1:append(&tail);break; case 2:display(tail);break; case 3:insert_after(&tail);break; case 4:insert_before(&tail);break; case 5:delnode(&tail);break; case 6:exit(0);break; default :  printf(“Wrong Choice… “); } } } Implementing Circular Singly Linked List  ( continued ) Data structures are classified in several ways : Linear :  Elements are arranged in sequential fashion. Ex : Array, Linear list, stack, queue Non-Linear :  Elements are not arranged in  sequence. Ex : trees, graphs Homogenous :  All Elements are belongs to same data type. Ex : Arrays Non-Homogenous :  Different types of Elements are grouped and form a data structure. Ex: classes Dynamic :  Memory allocation of each element in the data structure is done before their usage using D.M.A functions Ex : Linked Lists Static :  All elements of a data structure are created at the beginning of the program. They cannot be resized. Ex : Arrays Types of Data Structures
Stacks -- Stack is an ordered collection of data elements into which new elements may be inserted and from which elements may be deleted at one end called the “TOP” of stack. -- A stack is a last-in-first-out ( LIFO ) structure. -- Insertion operation is referred as “PUSH” and deletion operation is referred as “POP”. -- The most accessible element in the stack is the element at the position “TOP”. -- Stack must be created as empty. -- Whenever an element is pushed into stack, it must be checked  whether the stack is full or not. -- Whenever an element is popped form stack, it must be checked whether the stack is empty or not. -- We can implement the stack ADT either with array or linked list. ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],Stack’s contents TOP value 1. Init_stack( )    <empty>  -1 2. Push( ‘a’ ) a 0 3. Push( ‘b’ ) a b 1 4. Push( ‘c’ ) a b c 2 5. Pop( ) a b 1 6. Push( ‘d’ ) a b d 2 7. Push( ‘e’ ) a b d e 3 8. Pop( ) a b d 2 9. Pop( ) a b 1 10. Pop( ) a 0 11. Pop( ) <empty> -1 Output c c c c e c e d c e d b c e d b a a a b d e a b a b c a b a b d a b d a b a Push(a) Push(b) Push(c) Pop( ) Push(d) Push(e) Pop( ) Pop( ) Pop( ) Pop( ) Operations on Stack
#define SIZE 50 int stack[SIZE]; int top; void init_stack() { top=-1; } void push( int n ) { if( top==SIZE-1) printf(&quot;Stack is full&quot;); else  stack[++top]= n; } int pop( ) { if(top== -1)  {  printf(&quot;Stack is empty&quot;); return -1; }  else  return stack[top--]; } void display( )  { int i; if(top== -1) printf(&quot;Stack is empty.&quot;); else  { printf(&quot;Elements are : &quot;); for(i=0;i<=top;i++)  printf(&quot;%5d &quot;,stack[i]); } } int isEmpty( ) { if ( top== -1 )  return 1; else  return 0; } int peek( ){  return stack[top];  } int main()  { int choice,item; init_stack(); do  { printf(&quot;Menu1.Push.2.Pop.&quot;); printf(&quot;3.Peek.4.Display.5.Exit.&quot;); printf(&quot;Your Choice: &quot;); scanf(&quot;%d&quot;,&choice); switch(choice)  { case 1:printf(&quot;Enter the element to push : &quot;); scanf(&quot;%d&quot;,&item); push(item); break;  case 2:item = pop(); printf(&quot;Element poped : %d&quot;,item); printf(&quot;Press a key to continue...&quot;);     getche();  break; case 3:item = peek(); printf(&quot;Element at top : %d&quot;,item); printf(&quot;Press a key to continue...&quot;);      getche(); break;  case 4:display();     printf(&quot;Press a key to continue...&quot;);     getche();  break; case 5:exit(0); } }while(1); } Implementing Stack ADT using Array
struct s_node  { int data;  struct s_node *link; } *stack; void push(int j)  { struct s_node *m; m=(struct s_node*)malloc(sizeof(struct s_node)); m->data= j ;  m->link=stack; stack=m; return; } int pop( )  { struct s_node *temp=NULL; if(stack==NULL)  { printf(&quot;STACK is Empty.&quot;);  getch();  }  else  {  int i=stack->data; temp = stack ; stack=stack->link; free(temp);  return (i); } } int peek( )  { if(stack==NULL) { printf(&quot;STACK is Empty.&quot;); getch();  }  else  return (stack->data);  } void display()  { struct s_node *temp=stack; while(temp!=NULL)  { printf(&quot;%d&quot;,temp->data);  temp=temp->link; } } void main()  { int choice,num,i; while(1)  {  printf(&quot; MENU1. Push2. Pop3. Peek&quot;); printf(&quot;4. Elements in Stack5. Exit&quot;); printf(&quot;Enter your choice: &quot;); scanf(&quot;%d&quot;,&choice); switch(choice) { case 1: printf(&quot;Element to be pushed:&quot;); scanf(&quot;%d&quot;,&num); push(num);  break;  case 2: num=pop(); printf(&quot;Element popped: %d &quot;,num); getch(); break; case 3: num=peek(); printf(&quot;Element peeked : %d &quot;,num); getch(); break; case 4: printf(&quot;Elements present in stack : “ ): display();getch(); break; case 5: exit(1); default: printf(&quot;n Invalid  Choice &quot;); break; } } } Implementing Stack ADT using Linked List
Queues -- Queue is a linear data structure that permits insertion of new element at one end and deletion of an element at the other end. -- The end at which insertion of a new element can take place is called ‘ rear ‘ and the end at which deletion of an element take place is called ‘ front ‘. -- The first element that gets added into queue is the first one to get removed from the list, Hence Queue is also referred to as First-In-First-Out ( FIFO ) list. -- Queue must be created as empty. -- Whenever an element is inserted into queue, it must be checked  whether the queue is full or not. -- Whenever an element is deleted form queue, it must be checked whether the queue is empty or not. -- We can implement the queue ADT either with array or linked list.  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],3 6 8 2 5 4 addq (4) delq ( ) rear front 7 ,[object Object],[object Object],[object Object],[object Object],[object Object]
int queue[10] ,front, rear ; void init_queue()  {  front =  rear = -1 ; } void addq ( int item ){ if ( rear == 9 ) { printf(&quot;Queue is full&quot;); return ; } rear++ ; queue [ rear ] = item ; if ( front == -1 )front = 0 ; } int delq( ){ int data ; if ( front == -1 ) { printf(&quot;Queue is Empty&quot;); return 0; } data = queue[front] ; queue[front] = 0 ; if ( front == rear ) front = rear = -1 ; else  front++ ; return  data ; } void display()  { int i; if(front==-1) printf(&quot;Queue is empty.&quot;); else  { printf(&quot;Elements are : &quot;); for (i=front;i<=rear;i++) printf(&quot;%5d&quot;,queue[i]); } }  int main()  { int ch,num; init_queue();  do {  printf(&quot;MENU1. Add to Queue”); printf(“2. Delete form Queue&quot;); printf(&quot;3. Display Queue4. Exit.&quot;); printf(&quot;Your Choice: &quot;); scanf(&quot;%d&quot;,&ch); switch(ch) { case 1: printf(&quot;Enter an element : &quot;); scanf(&quot;%d&quot;,&num); addq(num);break; case 2: num=delq(); printf(&quot;Element deleted : %d&quot;,num);   break; case 3: display(); break; case 4: exit(0); default: printf(&quot;Invalid option..&quot;);  } }while(1); }  Implementing Queue ADT using Array
struct q_node  { int data;  struct q_node *next; }*rear,*front; void init_queue() { rear=NULL; front=NULL; }  void addq(int item)  { struct q_node *t; t=(struct q_node*)malloc(sizeof(struct q_node)); t->data=item;  t->next=NULL; if(front==NULL)  rear=front=t; else  { rear->next=t; rear=rear->next; } }  int delq()  { struct q_node *temp; if(front==NULL) { printf(&quot;Queue is empty.&quot;); return 0; } else { int num = front->data; temp = front;  front=front->next; free(temp);  return num; } } void display()  { struct q_node *temp=front; if(front==NULL) printf(&quot;Queue is empty.&quot;); else  { printf(&quot;Elements in Queue :&quot;); while(temp!=NULL)  { printf(&quot;%5d&quot;,temp->data); temp=temp->next; } } } int main()  { int ch,num; init_queue();  do  {  printf(&quot;MENU1. Add2. Delete&quot;); printf(&quot;3. Display Queue4. Exit.&quot;); printf(&quot;Your Choice: &quot;); scanf(&quot;%d&quot;,&ch); switch(ch) { case 1: printf(&quot;Enter an element : &quot;); scanf(&quot;%d&quot;,&num); addq(num);break; case 2: num=delq(); printf(&quot;Element deleted : %d&quot;,num); break; case 3: display(); break; case 4: exit(0); default:printf(&quot;Invalid option..&quot;);  } }while(1); }  Implementing Queue ADT using Liked List
Algorithm to Infix to Postfix Conversion --Arithmetic Expressions are represented using three notations infix, prefix and postfix. The prefixes ‘pre’, ‘post’, and ‘in’ refer to position of operators with respect to two operands. -- In infix notation, the operator is placed between the two operands. Ex: A + B  A * B + C (A * B) + (C * D) -- In Prefix notation, the operator is placed before the two operands. Ex: +AB   *A+BC +*AB*CD -- In Postfix notation, the operator is placed after the two operands. Ex: AB+   ABC+* AB*CD*+ ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
#define STACKSIZE 20 typedef struct  { int top;  char items[STACKSIZE]; }STACK; /*pushes ps into stack*/ void push(STACK *sptr, char ps)  { if(sptr->top == STACKSIZE-1) { printf(&quot;Stack is full&quot;); exit(1);  }  else sptr->items[++sptr->top]= ps; } char pop(STACK *sptr) { if(sptr->top == -1)  { printf(&quot;Stack is empty&quot;); exit(1);  }  else return sptr->items[sptr->top--]; }  int  main()  { int i; STACK s; char x, y, E[20] ; s.top = -1; /* Initialize the stack is */ printf(&quot;Enter the Infix Expression:&quot;); scanf(&quot;%s&quot;,E); for(i=0;E[i] != '';i++) { x= E[i]; /* Consider all lowercase letter from a to z are operands  */ if(x<='z' && x>='a')  printf(&quot;%c&quot;,x); else if(x == '(') push(&s ,x); else if( x == ')‘ ){ y=pop(&s) ; while(y != '(')  { printf(&quot;%c&quot;,y); y=pop(&s) ; } }  else { if(s.top ==-1 || s.items[s.top] == '(') push(&s ,x);  else { /* y is the top operator in the stack*/ y = s.items[s.top]; /* precedence of y is higher/equal to x*/  if( y=='*' || y=='/'){  printf(&quot;%c&quot;, pop(&s)); push(&s ,x); } else if ( y=='+' || y=='-') /* precedence of y is equal to x*/ if( x=='+' || x=='-') {  printf(&quot;%c&quot;, pop(&s)); push(&s ,x); } /* precedence of y is less than x*/ else  push(&s ,x); } } } while(s.top != -1) printf(&quot;%c&quot;,pop(&s)); } In-Fix To Post-Fix convertion
#include<stdio.h> #include<ctype.h> #include<math.h> float stack[10]; int top=-1; void push(char c)  {  stack[++top]=c; } float pop()  { float n; n=stack[top--];  return (n); } float evaluate(char expr[], float data[]) { int j=0;  float op1=0,op2=0;  char ch; while(expr[j]!='')  { ch = expr[j]; if(isalpha(expr[j]))  { push(data[j]); }  else  { op2=pop();  op1=pop(); switch(ch)  { case '+':push(op1+op2);break; case '-':push(op1-op2);break;  case '*':push(op1*op2);break; case '/':push(op1/op2);break; case '^':push(pow(op1,op2)); break; } }  j++; } return pop();  } int main()  { int j=0; char expr[20]; float number[20],result;  printf(&quot;Enter a post fix expression : &quot;); gets(expr); while(expr[j]!='') { if(isalpha(expr[j])) { fflush(stdin); printf(&quot;Enter number for %c : &quot;,expr[j]); scanf(&quot;%f&quot;,&number[j]); } j++; } result = evaluate(expr,number); printf(&quot;The result of %s is %f&quot;,expr,result);  } Evaluation of Post-Fix Expression

More Related Content

What's hot

Linked lists
Linked listsLinked lists
Single linked list
Single linked listSingle linked list
Single linked list
jasbirsingh chauhan
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
Sumathi Kv
 
Linked list
Linked listLinked list
Linked list
RahulGandhi110
 
Linked list
Linked listLinked list
Linked list
Trupti Agrawal
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structureTushar Aneyrao
 
LINKED LISTS
LINKED LISTSLINKED LISTS
LINKED LISTS
Dhrthi Nanda
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
Prof Ansari
 
Linkedlist
LinkedlistLinkedlist
Linklist
LinklistLinklist
Doubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || AlgorithmsDoubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || Algorithms
Shubham Sharma
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
Khulna University of Engineering & Tecnology
 
linked list
linked listlinked list
linked list
Shaista Qadir
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
Muhazzab Chouhadry
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
swajahatr
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
Khuram Shahzad
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
linked list using c
linked list using clinked list using c
linked list using c
Venkat Reddy
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 

What's hot (20)

Linked lists
Linked listsLinked lists
Linked lists
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
LINKED LISTS
LINKED LISTSLINKED LISTS
LINKED LISTS
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Linklist
LinklistLinklist
Linklist
 
Linked list
Linked listLinked list
Linked list
 
Doubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || AlgorithmsDoubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || Algorithms
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
linked list
linked listlinked list
linked list
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
linked list using c
linked list using clinked list using c
linked list using c
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 

Viewers also liked

Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
Advanced C Language for Engineering
Advanced C Language for EngineeringAdvanced C Language for Engineering
Advanced C Language for Engineering
Vincenzo De Florio
 
Student Data Base Using C/C++ Final Project
Student Data Base Using C/C++ Final ProjectStudent Data Base Using C/C++ Final Project
Student Data Base Using C/C++ Final Project
Haqnawaz Ch
 
Student record
Student recordStudent record
Student record
Upendra Sengar
 
C programming project by navin thapa
C programming project by navin thapaC programming project by navin thapa
C programming project by navin thapaNavinthp
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 

Viewers also liked (6)

Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Advanced C Language for Engineering
Advanced C Language for EngineeringAdvanced C Language for Engineering
Advanced C Language for Engineering
 
Student Data Base Using C/C++ Final Project
Student Data Base Using C/C++ Final ProjectStudent Data Base Using C/C++ Final Project
Student Data Base Using C/C++ Final Project
 
Student record
Student recordStudent record
Student record
 
C programming project by navin thapa
C programming project by navin thapaC programming project by navin thapa
C programming project by navin thapa
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Similar to Unit7 C

Linked list
Linked listLinked list
Linked list
A. S. M. Shafi
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
PoonamPatil120
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
Nivegeetha
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
Himadri Sen Gupta
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
AravindAnand21
 
Data structure
Data  structureData  structure
Data structure
Arvind Kumar
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
chin463670
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
chin463670
 
Linked list
Linked listLinked list
Linked list
Md. Afif Al Mamun
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
Masud Parvaze
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
KasthuriKAssistantPr
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
Programming Exam Help
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 

Similar to Unit7 C (20)

Linked list
Linked listLinked list
Linked list
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Linked list
Linked listLinked list
Linked list
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
Data structure
Data  structureData  structure
Data structure
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Linked list
Linked listLinked list
Linked list
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 

More from arnold 7490 (20)

Les14
Les14Les14
Les14
 
Les13
Les13Les13
Les13
 
Les11
Les11Les11
Les11
 
Les10
Les10Les10
Les10
 
Les09
Les09Les09
Les09
 
Les07
Les07Les07
Les07
 
Les06
Les06Les06
Les06
 
Les05
Les05Les05
Les05
 
Les04
Les04Les04
Les04
 
Les03
Les03Les03
Les03
 
Les02
Les02Les02
Les02
 
Les01
Les01Les01
Les01
 
Les12
Les12Les12
Les12
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
 

Recently uploaded

The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 

Recently uploaded (20)

The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 

Unit7 C

  • 1. Data Structures -- Data processing often involves in processing huge volumes of data. Many Companies handle million records of data stored in database. Many ways are formulated to handle data efficiently. -- An User-defined data type is a combination of different primary data types, which represents a complex entity. -- An Abstract Data Type ( A D T ) not only represents a set of complex data objects, but also includes a set of operations to be performed on these objects, defines that how the data objects are organized. -- The group of methods implements a set rules, which defines a logical way of handling data. -- The complex entity along with its group of methods is called Abstract Data Type ( A D T ) . -- Data structure is described as an instance of Abstract Data Type ( ADT ). -- We can define that Data structure is a kind of representation of logical relationship between related data elements. In data structure, decision on the operations such as storage, retrieval and access must be carried out between the logically related data elements. Data Structure Linear Non-Linear Stacks Queues Trees Graphs Linear Lists Some Data structures Arrays Strings Lists Stacks Queues Trees Graphs Dictionaries Maps Hash Tables Sets Lattice Neural-Nets Some Common Operations on Data structures Insertion : adding a new element to the collection. Deletion : removing an element from a collection. Traversal : access and examine each element in collection. Search : find whether an element is present or not. Sorting : rearranging elements in a particular order. Merging : combining two collections into one collection.
  • 2.
  • 3. struct node { int rollno; struct node *next; }; int main() { struct node *head,*n1,*n2,*n3,*n4; /* creating a new node */ n1=(struct node *) malloc(sizeof(struct node)); n1->rollno=101; n1->next = NULL; /* referencing the first node to head pointer */ head = n1; /* creating a new node */ n2=(struct node *)malloc(sizeof(struct node)); n2->rollno=102; n2->next = NULL; /* linking the second node after first node */ n1->next = n2; /* creating a new node * / n3=(struct node *)malloc(sizeof(struct node)); n3->rollno=104; n3->next=NULL; /* linking the third node after second node */ n2->next = n3; /* creating a new node */ n4=(struct node *)malloc (sizeof (struct node)); n4->rollno=103; n4->next=NULL; /* inserting the new node between second node and third node */ n2->next = n4; n4->next = n3; /* deleting n2 node */ n1->next = n4; free(n2); } Creating a Singly Linked List 150 head 150 400 720 910 n1-node n2-node n4-node n3-node 150 n1-node 150 head 150 150 n1-node n2-node 720 150 150 n1-node n2-node 720 910 n3-node 150 head 150 400 720 910 n1-node n2-node n4-node n3-node 101 400 102 720 103 910 104 NULL 101 NULL 101 720 102 NULL 101 720 102 910 104 NULL 101 720 102 720 103 910 104 NULL
  • 4. struct node { int data; struct node *next; }; struct node *createnode() { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf(&quot;Enter the data : &quot;); scanf(&quot;%d&quot;,&new->data); new->next = NULL; return new; } void append(struct node **h) { struct node *new,*temp; new = createnode(); if(*h == NULL) { *h = new; return; } temp = *h; while(temp->next!=NULL) temp = temp->next; temp->next = new; } void display(struct node *p) { printf(&quot;Contents of the List : &quot;); while(p!=NULL) { printf(&quot;%d&quot;,p->data); p = p->next; } } void insert_after(struct node **h) { struct node *new,*temp; int k; if(*h == NULL) return; printf(&quot;Enter data of node after which node : &quot;); scanf(&quot;%d&quot;,&k); temp = *h; while(temp!=NULL && temp->data!=k) temp = temp->next; if(temp!=NULL) { new=createnode(); new->next = temp->next; temp->next = new; } } void insert_before(struct node **h) { struct node *new,*temp,*prev ; int k; if(*h==NULL) return; printf(&quot;Enter data of node before which node : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data == k) { new = createnode(); new->next = *h; *h = new; return; } temp = (*h)->next; prev = *h; Implementing Singly Linked List
  • 5. while(temp!=NULL && temp->data!=k) { prev=temp; temp=temp->next; } if(temp!=NULL) { new = createnode(); new->next = temp; prev->next = new; } } void delnode(struct node **h) { struct node *temp,*prev; int k; if(*h==NULL) return; printf(&quot;Enter the data of node to be removed : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data==k) { temp=*h; *h=(*h)->next; free(temp); return; } temp=(*h)->next; prev=*h; while(temp!=NULL && temp->data!=k) { prev=temp; temp=temp->next; } if(temp!=NULL) { prev->next = temp->next; free(temp); } } void search(struct node *h) { struct node *temp; int k; if(h==NULL)return; printf(&quot;Enter the data to be searched : &quot;); scanf(&quot;%d&quot;,&k); temp=h; while(temp!=NULL && temp->data!=k) temp=temp->next; (temp==NULL)? printf(&quot;=>Node does not exist&quot;) : printf(&quot;=>Node exists&quot;); } void destroy(struct node **h) { struct node *p; if(*h==NULL) return; while(*h!=NULL) { p = (*h)->next; free(*h); *h=p; } printf(&quot; ******Linked List is destroyed******&quot;); } Implementing Singly Linked List ( continued )
  • 6. int main() { struct node *head=NULL; int ch; while(1) { printf(&quot;1.Append&quot;); printf(&quot;2.Display All&quot;); printf(&quot;3.Insert after a specified node&quot;); printf(&quot;4.Insert before a specified node&quot;); printf(&quot;5.Delete a node&quot;); printf(&quot;6.Search for a node&quot;); printf(&quot;7.Distroy the list&quot;); printf(&quot;8.Exit program&quot;); printf(&quot;Enter your choice : &quot;); scanf(&quot;%d&quot;,&ch); switch(ch) { case 1:append(&head);break; case 2:display(head);break; case 3:insert_after(&head);break; case 4:insert_before(&head);break; case 5:delnode(&head);break; case 6:search(head);break; case 7:destroy(&head);break; case 8:exit(0);break; default : printf( &quot;Wrong Choice, Enter correct one : &quot;); } } } /* function to sort linked list */ void sort(struct node *h) { struct node *p,*temp; int i, j, n, t, sorted=0; temp=h; for(n=0 ; temp!=NULL ; temp=temp->next) n++; for(i=0;i<n-1&&!sorted;i++) { p=h; sorted=1; for(j=0;j<n-(i+1);j++) { if ( p->data > ( p->next )->data ) { t=p->data; p->data =(p->next)->data; (p->next)->data = t; sorted=0; } p=p->next; } } } /* function to count number of node in the list */ int count ( struct node *h) { int i; for( i=0 ; h!=NULL ; h=h->next) i++; return i; } Implementing Singly Linked List ( continued )
  • 7. Add_Polynomial( list p, list q ) set p, q to point to the two first nodes (no headers) initialize a linked list r for a zero polynomial while p != null and q != null if p.exp > q.exp create a node storing p.coeff and p.exp insert at the end of list r advance p else if q.exp > p.exp create a node storing q.coeff and q.exp insert at the end of list r advance q else if p.exp == q.exp if p.coeff + q.coeff != 0 create a node storing p.coeff + q.coeff and p.exp insert at the end of list r advance p, q end while if p != null copy the remaining terms of p to end of r else if q != null copy the remaining terms of q to end of r Algorithm for adding two polynomials in linked lists
  • 8.
  • 9. A B C D A B D C q p q A B C D p A B C Insertion of node in Doubly Linked List Deletion of node in Doubly Linked List
  • 10. struct node { struct node *prev; int data; struct node *next; }; struct node *createnode() { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf(&quot;Enter the data : &quot;); scanf(&quot;%d&quot;,&new->data); new->prev = NULL; new->next = NULL; return new; } void append(struct node **h) { struct node *new,*temp; new = createnode(); if(*h == NULL) { *h = new; return; } temp = *h; while(temp->next!=NULL) temp = temp->next; temp->next = new; new->prev = temp; } void forward_display(struct node *p) { printf(&quot;Contents of the List : &quot;); while(p!=NULL) { printf(&quot;%d&quot;,p->data); p = p->next; } printf(&quot;&quot;); } void insert_after(struct node **h) { struct node *new,*temp; int k; if(*h == NULL) return; printf(&quot;Enter data of node after which node : &quot;); scanf(&quot;%d&quot;,&k); temp = *h; while(temp!=NULL && temp->data!=k) temp = temp->next; if(temp!=NULL) { new=createnode(); new->next = temp->next; temp->next = new; new->prev = temp; if(new->next != NULL) new->next->prev = new; } } Implementing Doubly Linked List
  • 11. void insert_before(struct node **h) { struct node *new,*temp; int k; if(*h==NULL) return; printf(&quot;Enter data of node before which node : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data == k) { new = createnode(); new->next = *h; new->next->prev=new; *h = new; return; } temp = *h; while(temp!=NULL && temp->data!=k) { temp=temp->next; } if(temp!=NULL) { new = createnode(); new->next = temp; new->prev = temp->prev; new->prev->next = new; temp->prev = new; } } void delnode(struct node **h) { struct node *temp; int k; if(*h==NULL) return; printf(&quot;Enter the data of node to be removed : &quot;); scanf(&quot;%d&quot;,&k); if((*h)->data==k) { temp=*h; *h=(*h)->next; (*h)->prev=NULL; free(temp); return; } temp=*h; while(temp!=NULL && temp->data!=k) { temp=temp->next; } if(temp!=NULL) { temp->next->prev = temp->prev; temp->prev->next = temp->next; free(temp); } } Implementing Doubly Linked List ( continued )
  • 12. void search(struct node *h) { struct node *temp; int k; if(h==NULL) return; printf(&quot;Enter the data to be searched : &quot;); scanf(&quot;%d&quot;,&k); temp=h; while(temp!=NULL && temp->data!=k) temp=temp->next; if (temp==NULL) printf(&quot;=>Node does not exist&quot;) else printf(&quot;=>Node exists&quot;); } void destroy(struct node **h) { struct node *p; if(*h==NULL) return; while(*h!=NULL) { p = (*h)->next; free(*h); *h=p; } printf(&quot; ******Linked List is destroyed******&quot;); } int main() { struct node *head=NULL; int ch; while(1) { printf(&quot;1.Append&quot;); printf(&quot;2.Display All&quot;); printf(&quot;3.Insert after a specified node&quot;); printf(&quot;4.Insert before a specified node&quot;); printf(&quot;5.Delete a node&quot;); printf(&quot;6.Search for a node&quot;); printf(&quot;7.Distroy the list&quot;); printf(&quot;8.Exit program&quot;); printf(&quot;Enter your choice : &quot;); scanf(&quot;%d&quot;,&ch); switch(ch) { case 1:append(&head);break; case 2:forward_display(head);break; case 3:insert_after(&head);break; case 4:insert_before(&head);break; case 5:delnode(&head);break; case 6:search(head);break; case 7:destroy(&head);break; case 8:exit(0);break; default : printf(&quot;Wrong Choice, Enter correct choice : &quot;); } } } Implementing Doubly Linked List ( continued )
  • 13. Circular Singly Linked List 910 tail 150 400 720 910 n1-node n2-node n3-node n4-node -- Singly Linked List has a major drawback. From a specified node, it is not possible to reach any of the preceding nodes in the list. To overcome the drawback, a small change is made to the SLL so that the next field of the last node is pointing to the first node rather than NULL. Such a linked list is called a circular linked list. -- Because it is a circular linked list, it is possible to reach any node in the list from a particular node. -- There is no natural first node or last node because by virtue of the list is circular. -- Therefore, one convention is to let the external pointer of the circular linked list, tail, point to the last node and to allow the following node to be the first node. -- If the tail pointer refers to NULL, means the circular linked list is empty. Circular Doubly Linked List -- A Circular Doubly Linked List ( CDL ) is a doubly linked list with first node linked to last node and vice-versa. -- The ‘ prev ’ link of first node contains the address of last node and ‘ next ’ link of last node contains the address of first node. -- Traversal through Circular Singly Linked List is possible only in one direction. -- The main advantage of Circular Doubly Linked List ( CDL ) is that, a node can be inserted into list without searching the complete list for finding the address of previous node. -- We can also traversed through CDL in both directions, from first node to last node and vice-versa. 101 400 102 720 103 910 104 150 prev data next prev data next prev data next prev data next
  • 14. void insert_after(struct node **t) { struct node *new,*temp; int k, found=0; if(*t == NULL) return; printf(&quot;Enter data of node after which node : &quot;); scanf(&quot;%d&quot;,&k); if((*t)->data==k) { new = createnode(); new->next = (*t)->next; (*t)->next = new; *t=new; return; } temp=(*t)->next; while(temp!=*t) { if(temp->data == k) { new = createnode(); new->next = temp->next; temp->next = new; found=1; break; } temp=temp->next; } if(found==0) printf(&quot;Node does not exist..&quot;); } Implementing Circular Singly Linked List struct node { int data; struct node *next; }; struct node *createnode() { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf(&quot;Enter the data : &quot;); scanf(&quot;%d&quot;,&new->data); new->next = NULL; return new; } void append(struct node **t) { struct node *new,*head; new = createnode(); if(*t == NULL) { *t = new; new->next = *t; return; } head = (*t)->next; (*t)->next = new; new->next = head; *t = new; } void display(struct node *t) { struct node *temp = t->next, *head=t->next; printf(&quot;Contents of the List : &quot;); do { printf(&quot;%d&quot;,temp->data);temp = temp->next; }while(temp!=head); printf(“”); }
  • 15. void insert_before(struct node **t) { struct node *new,*temp,*prev,*head; int k,found=0; if(*t==NULL) return; printf(&quot;Enter data of node before which node : &quot;); scanf(&quot;%d&quot;,&k); head=(*t)->next; if(head->data == k) { new = createnode(); new->next = head; (*t)->next = new; return; } temp = head->next; prev = head; while(temp!=head) { if(temp->data==k) { new = createnode(); prev->next = new; new->next = temp; found=1; break; } else { prev=temp; temp=temp->next; } } if(found==0) printf(&quot;Node does not exist..&quot;); } void delnode(struct node **t) { struct node *temp,*prev,*head; int k,found=0; if(*t==NULL) return; printf(&quot;Enter the data of node to be removed : &quot;); scanf(&quot;%d&quot;,&k); head=(*t)->next; if(head->data==k) { temp=head; if(temp->next!=head) (*t)->next=head->next; else *t = NULL; free(temp); return; } temp=head->next; prev=head; while(temp!=head) { if(temp->data == k) { prev->next = temp->next; if(temp==*t) *t = prev; free(temp); found=1; break; } else { prev=temp; temp=temp->next; } } if(found==0) printf(&quot;Node does not exist..&quot;); } Implementing Circular Singly Linked List ( continued )
  • 16. int main() { struct node *tail=NULL; int ch; while(1) { printf(&quot;1.Append&quot;); printf(&quot;2.Display All&quot;); printf(&quot;3.Insert after a specified node&quot;); printf(&quot;4.Insert before a specified node&quot;); printf(&quot;5.Delete a node&quot;); printf(&quot;6.Exit program&quot;); printf(&quot;Enter your choice : &quot;); scanf(&quot;%d&quot;,&ch); switch(ch) { case 1:append(&tail);break; case 2:display(tail);break; case 3:insert_after(&tail);break; case 4:insert_before(&tail);break; case 5:delnode(&tail);break; case 6:exit(0);break; default : printf(“Wrong Choice… “); } } } Implementing Circular Singly Linked List ( continued ) Data structures are classified in several ways : Linear : Elements are arranged in sequential fashion. Ex : Array, Linear list, stack, queue Non-Linear : Elements are not arranged in sequence. Ex : trees, graphs Homogenous : All Elements are belongs to same data type. Ex : Arrays Non-Homogenous : Different types of Elements are grouped and form a data structure. Ex: classes Dynamic : Memory allocation of each element in the data structure is done before their usage using D.M.A functions Ex : Linked Lists Static : All elements of a data structure are created at the beginning of the program. They cannot be resized. Ex : Arrays Types of Data Structures
  • 17.
  • 18.
  • 19. #define SIZE 50 int stack[SIZE]; int top; void init_stack() { top=-1; } void push( int n ) { if( top==SIZE-1) printf(&quot;Stack is full&quot;); else stack[++top]= n; } int pop( ) { if(top== -1) { printf(&quot;Stack is empty&quot;); return -1; } else return stack[top--]; } void display( ) { int i; if(top== -1) printf(&quot;Stack is empty.&quot;); else { printf(&quot;Elements are : &quot;); for(i=0;i<=top;i++) printf(&quot;%5d &quot;,stack[i]); } } int isEmpty( ) { if ( top== -1 ) return 1; else return 0; } int peek( ){ return stack[top]; } int main() { int choice,item; init_stack(); do { printf(&quot;Menu1.Push.2.Pop.&quot;); printf(&quot;3.Peek.4.Display.5.Exit.&quot;); printf(&quot;Your Choice: &quot;); scanf(&quot;%d&quot;,&choice); switch(choice) { case 1:printf(&quot;Enter the element to push : &quot;); scanf(&quot;%d&quot;,&item); push(item); break; case 2:item = pop(); printf(&quot;Element poped : %d&quot;,item); printf(&quot;Press a key to continue...&quot;); getche(); break; case 3:item = peek(); printf(&quot;Element at top : %d&quot;,item); printf(&quot;Press a key to continue...&quot;); getche(); break; case 4:display(); printf(&quot;Press a key to continue...&quot;); getche(); break; case 5:exit(0); } }while(1); } Implementing Stack ADT using Array
  • 20. struct s_node { int data; struct s_node *link; } *stack; void push(int j) { struct s_node *m; m=(struct s_node*)malloc(sizeof(struct s_node)); m->data= j ; m->link=stack; stack=m; return; } int pop( ) { struct s_node *temp=NULL; if(stack==NULL) { printf(&quot;STACK is Empty.&quot;); getch(); } else { int i=stack->data; temp = stack ; stack=stack->link; free(temp); return (i); } } int peek( ) { if(stack==NULL) { printf(&quot;STACK is Empty.&quot;); getch(); } else return (stack->data); } void display() { struct s_node *temp=stack; while(temp!=NULL) { printf(&quot;%d&quot;,temp->data); temp=temp->link; } } void main() { int choice,num,i; while(1) { printf(&quot; MENU1. Push2. Pop3. Peek&quot;); printf(&quot;4. Elements in Stack5. Exit&quot;); printf(&quot;Enter your choice: &quot;); scanf(&quot;%d&quot;,&choice); switch(choice) { case 1: printf(&quot;Element to be pushed:&quot;); scanf(&quot;%d&quot;,&num); push(num); break; case 2: num=pop(); printf(&quot;Element popped: %d &quot;,num); getch(); break; case 3: num=peek(); printf(&quot;Element peeked : %d &quot;,num); getch(); break; case 4: printf(&quot;Elements present in stack : “ ): display();getch(); break; case 5: exit(1); default: printf(&quot;n Invalid Choice &quot;); break; } } } Implementing Stack ADT using Linked List
  • 21.
  • 22. int queue[10] ,front, rear ; void init_queue() { front = rear = -1 ; } void addq ( int item ){ if ( rear == 9 ) { printf(&quot;Queue is full&quot;); return ; } rear++ ; queue [ rear ] = item ; if ( front == -1 )front = 0 ; } int delq( ){ int data ; if ( front == -1 ) { printf(&quot;Queue is Empty&quot;); return 0; } data = queue[front] ; queue[front] = 0 ; if ( front == rear ) front = rear = -1 ; else front++ ; return data ; } void display() { int i; if(front==-1) printf(&quot;Queue is empty.&quot;); else { printf(&quot;Elements are : &quot;); for (i=front;i<=rear;i++) printf(&quot;%5d&quot;,queue[i]); } } int main() { int ch,num; init_queue(); do { printf(&quot;MENU1. Add to Queue”); printf(“2. Delete form Queue&quot;); printf(&quot;3. Display Queue4. Exit.&quot;); printf(&quot;Your Choice: &quot;); scanf(&quot;%d&quot;,&ch); switch(ch) { case 1: printf(&quot;Enter an element : &quot;); scanf(&quot;%d&quot;,&num); addq(num);break; case 2: num=delq(); printf(&quot;Element deleted : %d&quot;,num); break; case 3: display(); break; case 4: exit(0); default: printf(&quot;Invalid option..&quot;); } }while(1); } Implementing Queue ADT using Array
  • 23. struct q_node { int data; struct q_node *next; }*rear,*front; void init_queue() { rear=NULL; front=NULL; } void addq(int item) { struct q_node *t; t=(struct q_node*)malloc(sizeof(struct q_node)); t->data=item; t->next=NULL; if(front==NULL) rear=front=t; else { rear->next=t; rear=rear->next; } } int delq() { struct q_node *temp; if(front==NULL) { printf(&quot;Queue is empty.&quot;); return 0; } else { int num = front->data; temp = front; front=front->next; free(temp); return num; } } void display() { struct q_node *temp=front; if(front==NULL) printf(&quot;Queue is empty.&quot;); else { printf(&quot;Elements in Queue :&quot;); while(temp!=NULL) { printf(&quot;%5d&quot;,temp->data); temp=temp->next; } } } int main() { int ch,num; init_queue(); do { printf(&quot;MENU1. Add2. Delete&quot;); printf(&quot;3. Display Queue4. Exit.&quot;); printf(&quot;Your Choice: &quot;); scanf(&quot;%d&quot;,&ch); switch(ch) { case 1: printf(&quot;Enter an element : &quot;); scanf(&quot;%d&quot;,&num); addq(num);break; case 2: num=delq(); printf(&quot;Element deleted : %d&quot;,num); break; case 3: display(); break; case 4: exit(0); default:printf(&quot;Invalid option..&quot;); } }while(1); } Implementing Queue ADT using Liked List
  • 24.
  • 25. #define STACKSIZE 20 typedef struct { int top; char items[STACKSIZE]; }STACK; /*pushes ps into stack*/ void push(STACK *sptr, char ps) { if(sptr->top == STACKSIZE-1) { printf(&quot;Stack is full&quot;); exit(1); } else sptr->items[++sptr->top]= ps; } char pop(STACK *sptr) { if(sptr->top == -1) { printf(&quot;Stack is empty&quot;); exit(1); } else return sptr->items[sptr->top--]; } int main() { int i; STACK s; char x, y, E[20] ; s.top = -1; /* Initialize the stack is */ printf(&quot;Enter the Infix Expression:&quot;); scanf(&quot;%s&quot;,E); for(i=0;E[i] != '';i++) { x= E[i]; /* Consider all lowercase letter from a to z are operands */ if(x<='z' && x>='a') printf(&quot;%c&quot;,x); else if(x == '(') push(&s ,x); else if( x == ')‘ ){ y=pop(&s) ; while(y != '(') { printf(&quot;%c&quot;,y); y=pop(&s) ; } } else { if(s.top ==-1 || s.items[s.top] == '(') push(&s ,x); else { /* y is the top operator in the stack*/ y = s.items[s.top]; /* precedence of y is higher/equal to x*/ if( y=='*' || y=='/'){ printf(&quot;%c&quot;, pop(&s)); push(&s ,x); } else if ( y=='+' || y=='-') /* precedence of y is equal to x*/ if( x=='+' || x=='-') { printf(&quot;%c&quot;, pop(&s)); push(&s ,x); } /* precedence of y is less than x*/ else push(&s ,x); } } } while(s.top != -1) printf(&quot;%c&quot;,pop(&s)); } In-Fix To Post-Fix convertion
  • 26. #include<stdio.h> #include<ctype.h> #include<math.h> float stack[10]; int top=-1; void push(char c) { stack[++top]=c; } float pop() { float n; n=stack[top--]; return (n); } float evaluate(char expr[], float data[]) { int j=0; float op1=0,op2=0; char ch; while(expr[j]!='') { ch = expr[j]; if(isalpha(expr[j])) { push(data[j]); } else { op2=pop(); op1=pop(); switch(ch) { case '+':push(op1+op2);break; case '-':push(op1-op2);break; case '*':push(op1*op2);break; case '/':push(op1/op2);break; case '^':push(pow(op1,op2)); break; } } j++; } return pop(); } int main() { int j=0; char expr[20]; float number[20],result; printf(&quot;Enter a post fix expression : &quot;); gets(expr); while(expr[j]!='') { if(isalpha(expr[j])) { fflush(stdin); printf(&quot;Enter number for %c : &quot;,expr[j]); scanf(&quot;%f&quot;,&number[j]); } j++; } result = evaluate(expr,number); printf(&quot;The result of %s is %f&quot;,expr,result); } Evaluation of Post-Fix Expression