Professional Documents
Culture Documents
data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.
Data structures provide a means to manage large amounts of data efficiently, such as large databases and internet
indexing services. Usually, efficient data structures are a key to designing efficient algorithms. Some formal design
methods and programming languages emphasize data structures, rather than algorithms, as the key organizing factor
in software design. Storing and retrieving can be carried out on data stored in both main memory and in secondary
memory.
What is Stack ?
a stack is a particular kind of abstract data type or collection in which the principal (or only) operations on the
[1]
collection are the addition of an entity to the collection, known as push and removal of an entity, known as pop. The
relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure. In a
LIFO data structure, the last element added to the structure must be the first one to be removed. This is equivalent to
the requirement that, considered as a linear data structure, or more abstractly a sequential collection, the push and
pop operations occur only at one end of the structure, referred to as the top of the stack. Often
a peek or top operation is also implemented, returning the value of the top element without removing it.
What is push and pop ?
A typical stack is an area of computer memory with a fixed origin and a variable size. Initially the size of the stack is
zero. A stack pointer, usually in the form of a hardware register, points to the most recently referenced location on the
stack; when the stack has a size of zero, the stack pointer points to the origin of the stack.
The two operations applicable to all stacks are:
a push operation, in which a data item is placed at the location pointed to by the stack pointer, and the
address in the stack pointer is adjusted by the size of the data item;
a pop or pull operation: a data item at the current location pointed to by the stack pointer is removed, and
the stack pointer is adjusted by the size of the data item.
STACK :
/* Program implements array as a stack. */
#include <stdio.h>
#include <conio.h>
#define MAX 10
struct stack
{
int arr[MAX] ;
int top ;
};
void initstack ( struct stack * ) ;
void push ( struct stack *, int item ) ;
int pop ( struct stack * ) ;
void main( )
{
struct stack s ;
int i ;
clrscr( ) ;
initstack ( &s ) ;
push ( &s, 11 ) ;
push ( &s, 23 ) ;
push ( &s, -8 ) ;
push ( &s, 16 ) ;
push ( &s, 27 ) ;
push ( &s, 14 ) ;
push ( &s, 20 ) ;
push ( &s, 39 ) ;
push ( &s, 2 ) ;
push ( &s, 15 ) ;
push ( &s, 7 ) ;
i = pop ( &s ) ;
printf ( "\n\nItem popped: %d", i ) ;
i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;
i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;
i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;
i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;
getch( ) ;
}
/* intializes the stack */
void initstack ( struct stack *s )
{
s -> top = -1 ;
}
/* adds an element to the stack */
void push ( struct stack *s, int item )
{
if ( s -> top == MAX - 1 )
{
printf ( "\nStack is full." ) ;
return ;
}
s -> top++ ;
s -> arr[s ->top] = item ;
}
/* removes an element from the stack */
int pop ( struct stack *s )
{
int data ;
if ( s -> top == -1 )
{
printf ( "\nStack is empty." ) ;
return NULL ;
}
data = s -> arr[s -> top] ;
s -> top-- ;
return data ;
}
Matrix Multiplication :
/* Program to perform operations like addition, multiplication, etc. on matrix. */
#include <stdio.h>
#include <conio.h>
#define MAX 3
void create ( int [3][3] ) ;
void display ( int [3][3] ) ;
void matadd ( int [3][3], int [3][3], int [3][3] ) ;
void matmul ( int [3][3], int [3][3], int [3][3] ) ;
void transpose ( int [3][3], int [3][3] ) ;
void main( )
{
int mat1[3][3], mat2[3][3], mat3[3][3], mat4[3][3], mat5[3][3] ;
clrscr( ) ;
printf ( "\nEnter elements for first array: \n\n" ) ;
create ( mat1 ) ;
In computer science, a linked list is a data structure consisting of a group of nodes which together represent a
sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the
next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or
removal of elements from any position in the sequence.
A linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to
signify the end of the list.
Linked lists are among the simplest and most common data structures. They can be used to implement several other
common abstract data types, including lists (the abstract data type), stacks, queues, associative arrays, and Sexpressions, though it is not uncommon to implement the other data structures directly without using a list as the
basis of implementation.
The principal benefit of a linked list over a conventional array is that the list elements can easily be inserted or
removed without reallocation or reorganization of the entire structure because the data items need not be stored
contiguously in memory or on disk. Linked lists allow insertion and removal of nodes at any point in the list, and can
do so with a constant number of operations if the link previous to the link being added or removed is maintained
during list traversal.
append ( &p, 14 ) ;
append ( &p, 30 ) ;
append ( &p, 25 ) ;
append ( &p, 42 ) ;
append ( &p, 17 ) ;
display ( p ) ;
addatbeg ( &p, 999 ) ;
addatbeg ( &p, 888 ) ;
addatbeg ( &p, 777 ) ;
display ( p ) ;
addafter ( p, 7, 0 ) ;
addafter ( p, 2, 1 ) ;
addafter ( p, 5, 99 ) ;
display ( p ) ;
printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;
delete ( &p, 99 ) ;
delete ( &p, 1 ) ;
delete ( &p, 10 ) ;
display ( p ) ;
printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;
}
/* adds a node at the end of a linked list */
void append ( struct node **q, int num )
{
struct node *temp, *r ;
if ( *q == NULL ) /* if the list is empty, create first node */
{
temp = malloc ( sizeof ( struct node ) ) ;
temp -> data = num ;
temp -> link = NULL ;
*q = temp ;
}
else
{
temp = *q ;
/* go to last node */
data = arr[*pfront] ;
arr[*pfront] = 0 ;
if ( *pfront == *prear )
*pfront = *prear = -1 ;
else
( *pfront )++ ;
return data ;
}
Bubble Sort :
/* CH9PR4.C: Bubble sort. */
#include <stdio.h>
#include <conio.h>
void main( )
{
int arr[5] = { 25, 17, 31, 13, 2 } ;
int i, j, temp ;
clrscr( ) ;
printf ( "Bubble sort.\n" ) ;
printf ( "\nArray before sorting:\n") ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;
for ( i = 0 ; i <= 3 ; i++ )
{
for ( j = 0 ; j <= 3 - i ; j++ )
{
if ( arr[j] > arr[j + 1] )
{
temp = arr[j] ;
arr[j] = arr[j + 1] ;
arr[j + 1] = temp ;
}
}
}
printf ( "\n\nArray after sorting:\n") ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;
getch( ) ;
}