Data structure types. Linear and Non-Linear Data Structures: Linked lists, stacks and queues.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Data Structure HKOI training /4/2010 So Pak Yeung.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Linked List Spring 2013Programming and Data Structure1.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.
Introduction to Data Structures Systems Programming.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CSC2100B Tutorial 2 List and stack implementation.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Queues.
Programming Practice 3 - Dynamic Data Structure
Introduction Queue is a linear Data Structure in which the operations are performed based on FIFO (First In First Out) principle.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Linked List. Insert a node at the beginning #include struct node { int data; struct node* next; }; struct node* head; void Insert(int x) { node *temp.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
STACKS & QUEUES for CLASS XII ( C++).
Data Structure By Amee Trivedi.
Linked List Insert as a first node Insert as a last node
Lists, Stacks and Queues in C
More on Linked Lists, Stacks, and Queues
Chapter 12 – Data Structures
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
UNIT – I Linked Lists.
Chapter 15 Lists Objectives
Program to search an element of array using linear search.
Linked list.
Queue data structure.
CHAPTER 12 LINKED LIST SRM-MCA.
Stack and Queue APURBO DATTA.
Introduction to Data Structures
Linked List Sudeshna Sarkar.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
LINKED LISTS.
Popping Items Off a Stack Lesson xx
ليست هاي پيوندي.
Programming Linked Lists.
Review & Lab assignments
Queues FIFO Enqueue Dequeue Peek.
Linked List.
LINKED LIST.
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Stacks and Queues.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Programming and Data Structure
EE 312 Final Exam Review.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
CSCS-200 Data Structure and Algorithms
Data Structures.
Presentation transcript:

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.