Boost PC performance: How more available memory can improve productivity
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("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
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("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 )
6. int main() { struct node *head=NULL; int ch; while(1) { printf("1.Append"); printf("2.Display All"); printf("3.Insert after a specified node"); printf("4.Insert before a specified node"); printf("5.Delete a node"); printf("6.Search for a node"); printf("7.Distroy the list"); printf("8.Exit program"); printf("Enter your choice : "); scanf("%d",&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( "Wrong Choice, Enter correct one : "); } } } /* 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
11. void insert_before(struct node **h) { struct node *new,*temp; 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; 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("Enter the data of node to be removed : "); scanf("%d",&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("Enter the data to be searched : "); scanf("%d",&k); temp=h; while(temp!=NULL && temp->data!=k) temp=temp->next; if (temp==NULL) printf("=>Node does not exist") else 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******"); } int main() { struct node *head=NULL; int ch; while(1) { printf("1.Append"); printf("2.Display All"); printf("3.Insert after a specified node"); printf("4.Insert before a specified node"); printf("5.Delete a node"); printf("6.Search for a node"); printf("7.Distroy the list"); printf("8.Exit program"); printf("Enter your choice : "); scanf("%d",&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("Wrong Choice, Enter correct choice : "); } } } 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("Enter data of node after which node : "); scanf("%d",&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("Node does not exist.."); } 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("Enter the data : "); scanf("%d",&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("Contents of the List : "); do { printf("%d",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("Enter data of node before which node : "); scanf("%d",&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("Node does not exist.."); } void delnode(struct node **t) { struct node *temp,*prev,*head; int k,found=0; if(*t==NULL) return; printf("Enter the data of node to be removed : "); scanf("%d",&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("Node does not exist.."); } Implementing Circular Singly Linked List ( continued )
16. int main() { struct node *tail=NULL; int ch; while(1) { printf("1.Append"); printf("2.Display All"); printf("3.Insert after a specified node"); printf("4.Insert before a specified node"); printf("5.Delete a node"); printf("6.Exit program"); printf("Enter your choice : "); scanf("%d",&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("Stack is full"); else stack[++top]= n; } int pop( ) { if(top== -1) { printf("Stack is empty"); return -1; } else return stack[top--]; } void display( ) { int i; if(top== -1) printf("Stack is empty."); else { printf("Elements are : "); for(i=0;i<=top;i++) printf("%5d ",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("Menu1.Push.2.Pop."); printf("3.Peek.4.Display.5.Exit."); printf("Your Choice: "); scanf("%d",&choice); switch(choice) { case 1:printf("Enter the element to push : "); scanf("%d",&item); push(item); break; case 2:item = pop(); printf("Element poped : %d",item); printf("Press a key to continue..."); getche(); break; case 3:item = peek(); printf("Element at top : %d",item); printf("Press a key to continue..."); getche(); break; case 4:display(); printf("Press a key to continue..."); 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("STACK is Empty."); getch(); } else { int i=stack->data; temp = stack ; stack=stack->link; free(temp); return (i); } } int peek( ) { if(stack==NULL) { printf("STACK is Empty."); getch(); } else return (stack->data); } void display() { struct s_node *temp=stack; while(temp!=NULL) { printf("%d",temp->data); temp=temp->link; } } void main() { int choice,num,i; while(1) { printf(" MENU1. Push2. Pop3. Peek"); printf("4. Elements in Stack5. Exit"); printf("Enter your choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("Element to be pushed:"); scanf("%d",&num); push(num); break; case 2: num=pop(); printf("Element popped: %d ",num); getch(); break; case 3: num=peek(); printf("Element peeked : %d ",num); getch(); break; case 4: printf("Elements present in stack : “ ): display();getch(); break; case 5: exit(1); default: printf("n Invalid Choice "); 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("Queue is full"); return ; } rear++ ; queue [ rear ] = item ; if ( front == -1 )front = 0 ; } int delq( ){ int data ; if ( front == -1 ) { printf("Queue is Empty"); 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("Queue is empty."); else { printf("Elements are : "); for (i=front;i<=rear;i++) printf("%5d",queue[i]); } } int main() { int ch,num; init_queue(); do { printf("MENU1. Add to Queue”); printf(“2. Delete form Queue"); printf("3. Display Queue4. Exit."); printf("Your Choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter an element : "); scanf("%d",&num); addq(num);break; case 2: num=delq(); printf("Element deleted : %d",num); break; case 3: display(); break; case 4: exit(0); default: printf("Invalid option.."); } }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("Queue is empty."); 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("Queue is empty."); else { printf("Elements in Queue :"); while(temp!=NULL) { printf("%5d",temp->data); temp=temp->next; } } } int main() { int ch,num; init_queue(); do { printf("MENU1. Add2. Delete"); printf("3. Display Queue4. Exit."); printf("Your Choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter an element : "); scanf("%d",&num); addq(num);break; case 2: num=delq(); printf("Element deleted : %d",num); break; case 3: display(); break; case 4: exit(0); default:printf("Invalid option.."); } }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("Stack is full"); exit(1); } else sptr->items[++sptr->top]= ps; } char pop(STACK *sptr) { if(sptr->top == -1) { printf("Stack is empty"); 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("Enter the Infix Expression:"); scanf("%s",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("%c",x); else if(x == '(') push(&s ,x); else if( x == ')‘ ){ y=pop(&s) ; while(y != '(') { printf("%c",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("%c", pop(&s)); push(&s ,x); } else if ( y=='+' || y=='-') /* precedence of y is equal to x*/ if( x=='+' || x=='-') { printf("%c", pop(&s)); push(&s ,x); } /* precedence of y is less than x*/ else push(&s ,x); } } } while(s.top != -1) printf("%c",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("Enter a post fix expression : "); gets(expr); while(expr[j]!='') { if(isalpha(expr[j])) { fflush(stdin); printf("Enter number for %c : ",expr[j]); scanf("%f",&number[j]); } j++; } result = evaluate(expr,number); printf("The result of %s is %f",expr,result); } Evaluation of Post-Fix Expression