Download presentation
Presentation is loading. Please wait.
Published byΕλεφθέριος Λαγός Modified over 6 years ago
1
Linear and Non-Linear Data Structures: Linked lists, stacks and queues.
2
Data structure types
4
Structures A compound user defined data type containing elements of basic data types
5
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]); }
6
Output Enter names, prices, pages of 3 books letusC Physics Math You entered letusC Physics Math
7
Defining and using structure
struct book{ char name[10]; float price; int pages; }; struct book b[3];
8
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
9
Output Enter names,prices,pages of 3 books letusc math physics And this is what you entered letusc math physics
10
Initialization struct book b1 = { "Basic", , 550 } ; printf("%s %f %d", b1.name, b1.price, b1.pages );
11
Assignment operator struct book b1 = { "Basic", , 550 },b2=b1; struct book b3; strcpy ( b3.name, b2.name ) ; B3.price = b1.price;
12
Nested structures main(){ struct Inner{ int i; }; struct Outer{ struct Inner I; struct Outer O; O.I.i = 999; printf(" %d",O.I.i); }
13
Dot versus arrow operator
Dot operator is performed on structure variables Arrow operator is performed on structure pointers
14
. Versus -> operator main(){ struct A{ int i; }; struct A a, *b; a.i=99; b=&a; printf("%d and %d",a.i, b->i); }
15
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);
16
Program exercise WAP to swap structures using functions and pointers
17
Linear Data structures
Stack – last in first out (LIFO) Queue – first in first out (FIFO) Link list – Insertion/deletion at any given location
18
Insertion is done on the top
Insertion is called push Deletion is called pop
20
Queue First in first out Insertion is called enqueue
Deletion is called dequeue Two positions are maintained – first and last
23
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)
24
A general linked list
25
Linked list versus stacks and queue
A linked list can be a stack or a queue depending upon the nature of operations
26
Node – the basic element
27
Example of a linked list
28
Define the node outside main()
struct Node{ int data; struct Node *next; }; struct Node* first = NULL; main(){}
29
Create and delete a node
struct Node *anode = (struct Node*)malloc(sizeof(struct Node)); anode->data = somedata; anode->next = somepointer; …. free(anode);
30
Traverse/display stack
void Traverse(struct Node *n) { while (n != NULL) { printf(" %d ",n->data); n = n->next; }
31
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); }}
32
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;
33
Stack continued.. void Traverse(struct Node *n) { printf("\n Stack is: "); while (n != NULL) { printf(" %d ",n->data); n = n->next; } }
34
Queue implementation struct Node{ int data; struct Node *next; }; struct Node *first=NULL, *last=NULL;
35
Queue continued… main(){ int i; for(i=0;i<5;i++){ Enqueue(i); Traverse(); } Dequeue(); Traverse(); }}
36
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;} }
37
Queue continued… Dequeue(){ struct Node *temp; temp = first->next; printf(“\n Dequeued = %d”,first->data); free(first); first = temp; }
38
Queue continued… Traverse(){ struct Node*temp = first; printf("\n Queue is - "); while(temp!=NULL){ printf(" %d",temp->data); temp=temp->next;} }
39
Link list WAP program to insert and delete node at any given location
40
Insertion and deletion
41
Link list insertion – 3 cases
Insert at the beginning Insertion at the end Insertion in between
42
Insertion (1) #include<stdio.h> struct Node{ int data; struct Node *next; }; struct Node *first=NULL; int count=0; //number of nodes count
43
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(); }
44
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)
45
Insertion (4) Traverse(){ struct Node*temp = first; printf("\nLinklist - "); while(temp!=NULL){ printf(" %d",temp->data); temp=temp->next;} }
46
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.