Вы находитесь на странице: 1из 16

GKM COLLEGE OF ENGINEERING & TECHNOLOGY

DATA STRUCTURES (CS2201 / 141301)


Code & Name of the subject :141301/DS Unit No:1 Data Structures Introduction: Data is the basic fact of entiry that is utilized in calculation or manipulation. Types of data: Numerical data Aplhanaumeric data Definition The intimate relationship between data & programs can be traced to he beginning of computing. In any area of application the input data and o/p data may each have a unique structure. Data structure is a representation of the logical relation ship b/w individual elements of data Classifications of Data structure: Data Structure Primitive Data Structure Page: 1of 2 Period:1

Integer

Float

Char

tt

Pointer

/ : p

s c /

u t e

. e b

/ k t

Non-Primitive Data structure

Arrays

Lists

Files

Linear List Stack Queue

Non Linear List Graph Free

Abstract Data Types

(ADT)

Definition An Abstract data Type (ADT) is defined as a mathematical model of the data objects that make up a data types as well as the functions that operate on the these objects

http://csetube.weebly.com/

The List ADT List is a term used to refer to a linear collection of data items Operations performed in the list are print list , make empty find delete, Insert, find k th Next, Previous The list can be Implemented in 3 ways are * Array implementation * Linked List Implementation * Cursor Implementation. Array Implementation of List: All of the above operations of list are implemented using arrays. The elements of any elements of any element on the array can be easily computes. Linked List Definition: Linked List is the most commonly used data structure used to store similar type of data in memory. The elements of a linked list are not stored in adjacent memory locations as in arrays. Nodes: Its a linear collection of data elements called Nides where the linear orders is implemented by means of pointers. (i) Data: Which holdsalist element (ii) Which store a llink to the next node in the list single node is represented as follows node

Data next

Actual representation of the linked list A1

t t h
A2 A2 100 300

/ : p
A3 300

s c /

u t e
A4

. e b

/ k t

A3

A5

A linked list A1 1000 1000 500 A4 500 650 A5 650 0

http://csetube.weebly.com/

Lecture Plan
Code & Name of the subject :141301/DS Unit No:1

Issue : C Rev:01 Page: 2of 2 Period:2

Linked List operations: Struct node { Int data; Struct node * next; } The structure declased for linear linked list holds 2 members i) An integer type variable data which holds the elements ii) Another member of type node which has the variable next which iii) Stores the address of the next node in the list Inserting an element:Ex : 10 20 30 40

Before Insertion Inserts the element5in the beginning of the list temp

10

Deleting an element-

t t h
20

/ : p

20

s c /

u t e
30

. e b
40

/ k t
50 50

After Insertion

Tempnodeto be deleted 10 30 40 50

Before Insertion

http://csetube.weebly.com/

10

20

30

50 Temp node gets deleted 40

After deletion Creating a link list Syntax: Struct linked list { int no struct linked list* next; }; Typed of struct linked list node; Node* head; Head=(node*) malloc (sizeof (node)); The above statement obtains memory to store a node & assigns its address to head which is a painter verifies head node

no To store values in the member fields use the following statements head no=10; head next=NULL; The second node can be added as follows; heat next=(node*) malice (size of (node)); hear next no=20; heat next next=NULL; Example problems for linkedllist implementalion Function for insertion at the beginning of the lilst Void insert beg(struct node **q, int no) { Struct node * temp; temp=malloc (size of (struct node)) temp data=no; temp next=*q *q=temp; }

t t h

/ : p

s c /

u t e

. e b

/ k t

neat

http://csetube.weebly.com/

Lecture Plan
Code & Name of the subject :141301/DS Unit No:1 Function for Deleting an element Void del (strouct node **q, int no) { Struct node * old ,*temp; Temp+*q; While (temp1=Null) { If (temp data= = no) { If (temp = =*q) *q= temp next; * q= temp next, Free(temp); Return; } Else { Old=temp; Temp=temp next; } } printf (in elements %d not found , no); }

Issue : C Rev:01 Page: 2of 2 Period:3

Displaying the contents of the linked list:. Syntax void display (struct node * start) { While(start1=NULL) { Print f(%d,startdata); Start= startnext, } }

t t h

/ : p

s c /

u t e

. e b

/ k t

http://csetube.weebly.com/

Lecture Plan
Code & Name of the subject :141301/DS Unit No:1

Issue : C Rev:01 Page: 2of 2 Period:4

Cursor implementation of Linked List: Cursor implementation is useful in cases whose LL concept has to be implemented Without using pointers. Initialization configuration: Start 0 1 2 3 4 5 6 7 8 9 Element Next 1 2 3 4 5 6 7 8 9 10 0

The list will use cell 0 as a beader, the value of 0 for next is the equivalent of a NULL pointer The cursor implementation of linked lists is straight forward. For consistency, we will emplement our lists with a header node

ht

: p t
10

//

cs

u t e

. e b

/ k t

Declaration of cursor implementation: typedef int ptrToNode; typedef ptrToNode List; typedef PtrToNode Position; void InitializeCursorSpace(Void); List MakeEmpty (List L); Int IsEmpty(Const List L); Int Islast (const position P, Const List L); Position Find (ElementType X, Const List L); Void Delete(ElementTupe X, List L); Position FindPrevious(ElementType X, Const List L);

http://csetube.weebly.com/

Void Insert(ElementType X, List L, Position P); Void DeleteList (List L); Position Header (Const List L); Position First (Const List L); Position First (Const List L); Position Advance (Const Position P); ElementType Retrieve (Const position p); /* Place in the implementation file */ Struct Node { ElementType Element: Position Next: }; Struct Node CursorSpace[SpaceSize];

t t h

/ : p

s c /

u t e

. e b

/ k t

http://csetube.weebly.com/

Lecture Plan
Code & Name of the subject : 141301/DS Unit No:1 Doubly Linked List:

Issue : C Rev:01 Page: 2of 2 Period:5

Def : The doubly or two-way linked list uses double set of pointers, one pointing to the next item and other pointing to the preceding item. It can be traversed in 2 directions either from the beginning of the list to the end or in the backward direction from end of the list to the beginning.

Pointer Previous data data data

Pointer Next

Each node contains 3 parts:

An information field which contains the data A pointer field next which contains the location of the next node in the list A pointer field prev which contains the location of the preceding node in the list The structure defined for doubly linked list is : { Struct dnode int data; Struct node *next; Struct node * prev;

t t h

/ : p

s c /

u t e

. e b

/ k t

http://csetube.weebly.com/

Creating a dobuly Linked List: The list is empty, the memory will be allocated for the new node where address is sotred in using S, a NULL value is stored in Prev and next and the value of num is assigned to its data part. /*Function to add a new node at the end of the doubly linked list */ (Declaration) Function to add a new node at the beginning of the linked list*/ (Dec) Function to add a new node after a specified number of nodes*/ Deleting a node from doubly linked list Advantage o Deletion operation is easier o Finding the predecessor & Successor of a node is easier Disadvantage More memory space is required since it has 2 pointers.

t t h

/ : p

s c /

u t e

. e b

/ k t

http://csetube.weebly.com/

Lecture Plan
Code & Name of the subject : 141301/DS Unit No:1 STACK ADT

Issue : C Rev:01 Page: 2of 2 Period:6

Introduction: A stack is a linear data structure which follows last In First Out(LIFO) principle, in which both insertion and deletion occur at only one end of the list called top Fundamental operations on the stack are: PUSH equivalent to insert POP Equivalent to delete Deletes the most recently inserted element 400 300 200 100 Top

Top is a pointer which keeps track of the top element in the stack, initially top is Zero when the stack is empty

Pop(S)

t t h

/ : p

s c /
Pus(X,S)

u t e

. e b

/ k t

Stack S

Top(S) Stack can be implemented in 2 ways are (i) (ii) Array implementation Linked List implementation

Array Implementation of Stack : A linear array is used to represent the STACK and a pointer variable TOP which contains the location of TOP element. The operation of adding(pushing) an item on to a stack and the operation of removing(poping) an item can be implemented using the PUSH & POP operations.

http://csetube.weebly.com/

NOTE: Overflow: Sometimes when a new data is to be inserted into a data structure but there is no available space this situation is called overflow. Underflow: On the contrary, if one wants to delete the data from the data structure that is empty then the situation is called underflow. Stack declarations array Implementation Linked list Implementation Push operation is performed by inserting an element at the front of the list POP operation is performed by deleting at the front of the list TOP operation returns the element at the front of the list Top Empty Stack Push200 200 Push200 200 100 Push 100 100

POP 200

t t h

/ : p
200 100

s c /

u t e
100

. e b

/ k t

http://csetube.weebly.com/

Lecture Plan
Code & Name of the subject:141301/DS Unit No:1

Issue : C Rev:01 Page: 2of 2 Period:7

Applications of stack Evaluating arithmetic expression Balancing the symbols. Towers of Hanoi Function calls 8 Queen Problem Reversing the string Reorientations of arithmetic expression

There are 3 different ways of representing the algebraic Expression They are i) Infix notation ii) Postfix notation iii) Prefix notation Infix notation Expression are generally expressed in fix notation with binary operator b/w the operands. In this type . the operator precedes the operands. Eg: X+y X+y*z (x+y)*z (x+y)*(p+q).

t t h

/ : p

s c /

u t e

. e b

/ k t

Prefix notation The prefix notation is also called as polish notation. In this type also the operator precedes the operands. Eg: +xy +x*yz *+xyz *+xy+Pq.

http://csetube.weebly.com/

Post fix notation This notation is also called as reverse polish notation the operator trails the operand Balancing Symbols The common mistake which can be made frequently by the programmer is unbalance of parentheses for correct representation of mathematical expression the following precautions must be taken (i) (ii) There must be equal not of left & right parenthesis Each left parenthesis must be balanced by right parenthesis

The following steps need to be forward to 0find out the Experian has balanced Symbol or not: (i) (ii) (iii) (iv) (v) If the opening parenthesis if found in the expression its pushed into the stack. When a closing parenthesis is found, the pop operation is carried & its the deleted parenthesis corresponds to the opening parenthesis entered string is invalid. If the opening and closing parenthesis match, the program will continue otherwise the When the end of string is found, the stack must be empty If the stack contains elements, it means there is opening parenthesis that had not been closed.

t t h

/ : p

s c /

u t e

. e b

/ k t

http://csetube.weebly.com/

Lecture Plan
Code & Name of the subject : 141301/DS Unit No:1

Issue : C Rev:01 Page: 2of 2 Period:8

Queue Model: Definition: A queue is a linear data structure which follows FIFO principle, in whyich insertion is performed at rear end and deletion is performed at front end Dequeue (Q) Enqueue (Q)

Queue Q

Operations on Queue : Basic operations performed on queue are Enqueue Dequeue

Enqueue: Def: The process of inserting an element in queue Dequeu : Def: The process of deleting an element from the queue Overflow: Def: Attempt to insert an element, when the queue is full is said take overflow condition Underflow : Def : Attempt to delete an element from the queue, when the queue is empty is said to be underflow condition Implementation of Queue: Queue can be implemented using arrays, and linked list. Array implementation of queue The array Queue[] is used to implement the queu The ends of the queue are represented by the two pointers rear and front The size of the array (or) queue is size To enqueue an element X, increment the size & rear, then set Queue[Rear] = X

t t h

/ : p

s c /

u t e

. e b

/ k t

http://csetube.weebly.com/

To Dequeue an element the Queue[Front] is returned, decrement the size and infrement front -1 0 R 1 2 3 4 100 0

F Empty Queue 100 0 200 1

R, E Enqueue (100) 100

R F Empty Queue

R, E Enqueue (100)

In Dequeue operation if front= rear then reset both operation to their initial values Program to implement queue operation Disadvantage of simple queues Linked list implementation of queues

Enque operation is performed at the end of the list Dequeue operation is performed at the front of the list

10 Empty Queue 10

t t h
20

Enqueue (10)

/ : p

s c /
10 30

u t e

. e b
20

/ k t

Enqueue (20)

Enqueue (30) Dequeue 10 20 30

http://csetube.weebly.com/

Lecture Plan
Code & Name of the subject : 141301/DS Unit No:1 Circular Queue:

Issue : C Rev:01 Page: 2of 2 Period:9

In the circular queue the first element is stored immediately after last element.If the last location of the queue is occupied,the new element is inserted at the first memory location of the queue implementating an array(queue). A circular queue over comes the limitation of the simple queue by utilizing the entire space of the queue. Note: The first end of the queue always points to the first element of the queue. If the values of front and rear are equal ,the queue is empty.the values of front and rear pointers are only increased/decreased after insertion/deletion operation. Like simple queue,rear pointer is incremented by one when a new element is inserted and front pointer is incremented by one when an element is deleted.

t t h

/ : p

s c /

u t e

. e b

/ k t

http://csetube.weebly.com/

Вам также может понравиться