You are on page 1of 5

Data Structure

I. Link List:

 A link list consists of a sequence of records.


 A record is a set of values assigned to the field of a structure.

struct node
{
int data;
struct node *next;
};

 The next of the last inserted record must be NULL.


 A pointer (sptr) must be maintained to access the first node of the
list.
 Any element of the link list should be accessed starting from (sptr).

Remark: declaration of pointer to pointer for (sptr) has 2 methods:

1st method 2nd method

void Create(struct node **sptr) void Create(struct node *&sptr)


{ {
...... if (*sptr == NULL) ...... ...... if (sptr == NULL) ......
} }

void main() void main()


{ {
struct node *sptr = NULL; struct node *sptr = NULL;

Create(&sptr); Create(sptr);
} }

1
II. Recursion:

 We have two approaches to solve a problem:


o Interactive: execute actions in loops.
o Recursive: reduce the problem into smaller sub-problems.

 A recursive algorithm is an algorithm which calls itself.

 Every recursion must have at least one base case.

 The general case must be reduced to become a base case.

 The base case stops the recursion.

int factorial (int n)


Example: Factorial of (n). {
int f;
 Base Case: if (n == 1)
n = 1  factorial(1) = 1 {
f = 1;
}
 General Case:
else
else  {
factorial(n) = n * factorial(n-1) f = n * factorial(n-1);
}
return f;
}

 Every recursive call has:


 Its own parameters.
 Its own local variables.
 Its own code.

2
III. Doubly Link List:

 It is an extension of the simple link list.


 Each node in a double link list has the following structure:

struct node
{ sptr
int data;
struct node *next;
struct node *prev; 1 2 3
};

*next is a pointer to the next node.


*prev is a pointer the previous node.

 We can move in both directions in a double link list.

IV. Circular Link List:

 In a circular link list the next of the last inserted node must point
to the first node.

 The circular link list is handled by a pointer (tail) which points to


the last inserted node.

tail

1 2 3

3
V. Stack:

 The stack is characterized by LIFO: Last In | First Out.


 The difference between a simple link list and a stack is that some
operations on the link list are not supported on the stack.

stack

1 2 3

 To manipulate a stack we have only the following operations:


 One Insert Operation: push
 One Delete Operation: pop
 One operation to return the last inserted node: top
 One operation to test if stack is empty: is_empty

 Symbols Balance Algorithm:

 Make an empty stack


 Read the characters till the end of file:
o If it is an opening character, ex: ( [ {
 push(stack, symbol);
o If it is a closing character, ex: ) ] }
 x = top(stack);
pop(stack);
o If x doesn’t correspond to the closing symbol  error
o If the stack is empty on closing  error
o At the end of file, if the stack is not empty  error

 Post fix calculator:

Infix calculator: a*(b-c) we use operators between the operands.

Postfix calculator: abc-* we don’t have to use brackets, the operands


are followed by the operator.

 Algorithm:

 Make an empty stack


 If the character is an operand  push into the stack
 If the character is an operator:
o top and pop two or more operands according to the operator
o perform the operation
o push the new value into the stack

c
abc-* b-c a*(b-c)
b b-c
a a ans
4
 Stack using arrays:

The stack in arrays must have two additional variables:

 nb_max: which represents the maximum number of values that can be


stored in the stack.
 nb: which represents the current number of values stored in the
stack.
 nb = 0  stack is empty
 nb = nb_max  stack is full

VI. Queue:

 The Queue is characterized by FIFO: First In | First Out.


 To manipulate a queue we must use two pointers:
 Head pointer: which points to the first node to delete.
 Tail pointer: which points to the last inserted node.

head tail

1 2 3

 To manipulate a queue we have only the following operations:


 One insert operation: enqueue
 One operation that returns the first inserted node and deletes
this node: dequeue
 One operation to test if queue is empty: is_empty

You might also like