Linear and Non-Linear Data Structures: Linked lists, stacks and queues.
Data structure types
Structures A compound user defined data type containing elements of basic data types
Problem 1 main(){ char name[3][10] ; float price[3] ; int pages[3],i ; printf("\nEnter names, prices, pages of 3 books\n" ) ; for(i=0;i<3;i++) scanf("%s %f %d",&name[i],&price[i],&pages[i] ); printf("\n You entered\n" ) ; printf("%s %f %d\n",name[i],price[i],pages[i]); }
Output Enter names, prices, pages of 3 books letusC 120 410 Physics 515 800 Math 300 660 You entered letusC 120.000000 410 Physics 515.000000 800 Math 300.000000 660
Defining and using structure struct book{ char name[10]; float price; int pages; }; struct book b[3];
main( ){ struct book{ char name[10]; float price; int pages; }; struct book b[3]; int i; printf ( "\nEnter names,prices,pages of 3 books\n" ) ; for(i=0;i<3;i++) scanf("%s%f%d",&b[i].name, &b[i].price, &b[i].pages ); printf ( "\nAnd this is what you entered" ) ; printf("\n%s %f %d", b[i].name, b[i].price, b[i].pages ) ; } Q2
Output Enter names,prices,pages of 3 books letusc 120 410 math 200 500 physics 80 300 And this is what you entered letusc 120.000000 410 math 200.000000 500 physics 80.000000 300
Initialization struct book b1 = { "Basic", 130.00, 550 } ; printf("%s %f %d", b1.name, b1.price, b1.pages );
Assignment operator struct book b1 = { "Basic", 130.00, 550 },b2=b1; struct book b3; strcpy ( b3.name, b2.name ) ; B3.price = b1.price;
Nested structures main(){ struct Inner{ int i; }; struct Outer{ struct Inner I; struct Outer O; O.I.i = 999; printf(" %d",O.I.i); }
Dot versus arrow operator Dot operator is performed on structure variables Arrow operator is performed on structure pointers
. Versus -> operator main(){ struct A{ int i; }; struct A a, *b; a.i=99; b=&a; printf("%d and %d",a.i, b->i); }
Structures in functions struct A{ int i; float f; }; main(){ struct A a={-5,2.43}; fun(a); } fun(struct A a){ printf("%d and %f",a.i, a.f);
Program exercise WAP to swap structures using functions and pointers
Linear Data structures Stack – last in first out (LIFO) Queue – first in first out (FIFO) Link list – Insertion/deletion at any given location
Insertion is done on the top Insertion is called push Deletion is called pop
Queue First in first out Insertion is called enqueue Deletion is called dequeue Two positions are maintained – first and last
Linked list General name for a list of linked elements using pointers Dynamic memory allocation Insertion and deletion can be done at any given location Need to save the address of first node (element)
A general linked list
Linked list versus stacks and queue A linked list can be a stack or a queue depending upon the nature of operations
Node – the basic element
Example of a linked list
Define the node outside main() struct Node{ int data; struct Node *next; }; struct Node* first = NULL; main(){}
Create and delete a node struct Node *anode = (struct Node*)malloc(sizeof(struct Node)); anode->data = somedata; anode->next = somepointer; …. free(anode);
Traverse/display stack void Traverse(struct Node *n) { while (n != NULL) { printf(" %d ",n->data); n = n->next; }
Stack Implementation struct Node{ int data; struct Node *next; }; struct Node* first = NULL; main() { int i; for(i=0;i<4;i++) { Push(i); Traverse(first);} for(i=0;i<4;i++) { Pop(); Traverse(first); }}
Stack continued.. void Push(int data){ struct Node *anode = (struct Node*)malloc(sizeof(struct Node)); anode->data = data; anode->next = first; first=anode; } void Pop(){ struct Node *temp; temp = first->next; printf(“\n Popped element = %d”,first->data); free(first); first = temp;
Stack continued.. void Traverse(struct Node *n) { printf("\n Stack is: "); while (n != NULL) { printf(" %d ",n->data); n = n->next; } }
Queue implementation struct Node{ int data; struct Node *next; }; struct Node *first=NULL, *last=NULL;
Queue continued… main(){ int i; for(i=0;i<5;i++){ Enqueue(i); Traverse(); } Dequeue(); Traverse(); }}
Queue continued… Enqueue(int i){ struct Node *temp = (struct Node* ) malloc(sizeof(struct Node)); temp->data = i; temp->next = NULL; if(first==NULL) first = last = temp; else {last->next = temp; last = temp;} }
Queue continued… Dequeue(){ struct Node *temp; temp = first->next; printf(“\n Dequeued = %d”,first->data); free(first); first = temp; }
Queue continued… Traverse(){ struct Node*temp = first; printf("\n Queue is - "); while(temp!=NULL){ printf(" %d",temp->data); temp=temp->next;} }
Link list WAP program to insert and delete node at any given location
Insertion and deletion
Link list insertion – 3 cases Insert at the beginning Insertion at the end Insertion in between
Insertion (1) #include<stdio.h> struct Node{ int data; struct Node *next; }; struct Node *first=NULL; int count=0; //number of nodes count
Insertion (2) main(){ int pos=2; //choose pos for insertion Insert(10,1);Traverse(); Insert(20,2);Traverse(); Insert(30,3);Traverse(); Insert(555,pos);Traverse(); }
Insert(int data, int pos){ int i=1; struct Node Insert(int data, int pos){ int i=1; struct Node *temp=first; struct Node *anode = (struct Node*)malloc(sizeof(struct Node)); anode->data = data; if(count==0){anode->next=NULL; first = anode; } else if(pos==1){ anode->next = first; first = anode;} else if(pos==count+1){ while(temp->next!=NULL) temp=temp->next; anode->next = NULL; temp->next = anode; } else if(pos>1&&pos<=count){ while(i++<pos-1) {temp=temp->next; } anode->next = temp->next; temp->next = anode; //printf("\nHERE"); }count++; } Insertion(3)
Insertion (4) Traverse(){ struct Node*temp = first; printf("\nLinklist - "); while(temp!=NULL){ printf(" %d",temp->data); temp=temp->next;} }
Exercise: Delete a given node Consider cases for node to be deleted: first, last or other node. Find previous node of the node to be deleted. Change the next of previous node. Free memory for the node to be deleted.