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

INTRODUCTION TO DATA STRUCTURES

With a single variable, it is an unfeasible task to store a large amount of related


data. And also we need an organized medium of storage to handle any correlated
information. Thus the concept of data structures was introduced.
Arrays are considered as good example for implementing simple data structures.
Arrays can be effectively used for random access of fixed amount of data.
However, if the data structure requires frequent insertions and deletions then
arrays are not much efficient because even a single insertion or deletion
operation forces many other data elements to be moved to new locations.
Also, there is always an upper limit to the number of elements that can be stored
in the array-based data structure.

Searching:
It is a mechanism of finding whether the element is existing in the
list or not. The 2 types of searching techniques are
1) Linear search
2) Binary search
Linear search:
This is the simplest technique, assuming that the search list is an
array, the linear search approach iterates through all the elements
until a match is found.
Ex: 5 8 9 15 43 2 17 65 23 11
Suppose we want to search the element 17 in the list, the linear
search first compares the first element with the key and check
whether it is matching or not. If the key is not matching, it moves
to the second element,if element is existing it terminates by
specifying its position. If element is not existing, it searches the
entire list and terminates the unsuccessful search.
#include<stdio.h>
main()
{
int n,i,s,c=0;
printf("enter size of array and the number to be searched");
scanf("%d",&s,&n);
int x[s];
for(i=0;i<s;i++)
{
printf("enter elements of the array");
scanf("%d",&x[i]);
}
for(i=0;i<s;i++)
{
if(x[i]==n)
{
c++;
break;
}
}
if(c!=0)
printf("element found at %d",i);
else
printf("element not found");
}

Binary Search:
The pre-requisite of binary search is the element should be in
stored order. In binary search it starts with looking at the mid value
of the list by taking low index and high index divided by 2.
#include<stdio.h>
void sort(int []);
main()
{
int n,l,i,f=0,m;
printf("enter the number to search; enter the size of array");
scanf("%d",&n,&l);
int x[l];
for(i=0;i<l;i++)
{
printf("enter the elements of the array");
scanf("%d",&x[i]);
}
sort(x);
while(f<=l)
{
m=(l+f)/2;
if(x[m]==n)
break;
if(x[m]<n)
f=m;
if(x[m]>n)
l=m;
}
printf("found at %d",m);
}
void sort(int x[])
{
int i,l=3,j,t=0;
for(i=0;i<l;i++)
{
for(j=0;j<l;j++)
{
if(x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
}

SORTING
It is one of the most important operations by the computer. Sorting
a set of values means arranging them in a fixed order. Usually in
ascending and descending. The two sorting techniques are 1.
Selection sort 2. Bubble sort.
BUBBLE SORT:
It is also called as exchange sort. It compares the adjacent element
in a list and swap them if they are not In order. Each pair of
adjacent element is compared and swapped until the largest or
smallest element to bubble up at the top.
#include<stdio.h>
main()
{
int i, j, t = 0 ;
int x[10]={1,2,5,6,7,8,3,4,0,4};
for(i=0;i<10-1;i++)
{
for(j=0;j<10-1-i;j++)
{
if(x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
for(i=0;i<10;i++)
{
printf("%d,x[i]);
}
}

SELECTION SORT:
#include<stdio.h>
main()
{
int i,j,s,p,t=0;
int x[10]={1,5,8,9,7,2,0,8,9,6};
for(i=0;i<10;i++)
{
s=x[ i ];
p=i;
for(j=i+1;j<10;j++)
{
if(x[j]<x[p])
{
t=x[j];
x[j]=x[p];
x[p]=t;
}
}
}
for(i=0;i<10;i++)
printf("%d" ,x[i]);
}

LINKED LIST
#include<stdio.h>
struct node{
int data;
struct node *next;
}*start=NULL;
void create();
void display();
void insertatbeginning();
void deletefrombeginning();
void insertatend();
void deletefromend();
void main()
{
create();
display();
insertatbeginning();
deletefrombeginning();
insertatend();
deletefromend();
}
void create()
{
int s;
char c;
struct node *newnode,*currentnode;
do{
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the values");
scanf("%d",&s);
newnode->data=s;
newnode->next=NULL;
if(start==NULL)
{
start = newnode;
currentnode=newnode;
}
else
{
currentnode->next=newnode;
currentnode=newnode;
}
printf("enter y to continue or n to stop");
scanf(" %c",&c);
}while(c!='n');
}
void display()
{
struct node *temp;
temp=start;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
void insertatbeginning()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=5;
newnode->next=start;
start=newnode;
display();
}
void deletefrombeginning()
{
struct node *temp;
temp=start;
start=temp->next;
free (temp);
display();
}
void insertatend()
{
struct node *temp,*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode->data=7;
newnode->next=NULL;
temp->next=newnode;
display();
}
void deletefromend()
{
struct node *temp,*y;
temp=start;
while(temp->next!=NULL)
{
y=temp;
temp=temp->next;
}
y->next=NULL;
free(temp->next);
display();
}

STACK:
It is a data structure where in insertion and deletion is done from
one end. This data structure is used to store the elements in an
order. It is a linear data structure which can be implemented using
an array or linked list.
The best examples of stacks are pile of plates where in if u want to
add a plate u place it at the top most position and if u want to
remove a plate we remove the top most plate first. Insertion and
deletion happens from one end called top. It was a concept called
LIFO (last in first out).
Operations performed :
1. PUSH
2. POP
#include<stdio.h>
struct node{
int data;
struct node *next;
}*top=NULL;
void create();
void peep();
void push();
void pop();
void main()
{
create();
peep();
push();
pop();
}
void create()
{
int s;
char c;
struct node *newnode,*currentnode;
do{
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the values");
scanf("%d",&s);
newnode->data=s;
newnode->next=NULL;
if(top==NULL)
{
top = newnode;
currentnode=newnode;
}
else
{
currentnode->next=newnode;
currentnode=newnode;
}
printf("enter y to continue or n to stop");
scanf(" %c",&c);
}while(c!='n');
}
void peep()
{
struct node *temp;
temp=top;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
void push()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=5;
newnode->next=top;
top=newnode;
peep();
}
void pop()
{
struct node *temp;
temp=top;
top=temp->next;
free (temp);
peep();
}

QUEUE:
Queue is a data structure for storing the collection of elements in a
particular order .It follows FIFO(First in First out) approach. The
queue has two end points where the insertion is done at rear end
and deletion is done at first end.
The operations performed on Queue are Insertion, Deletion and
Display. The operations that are used for adding an element at the
rear end is called Enqueue. The operation that perform deletion at
the front is called Dequeue.
#include<stdio.h>
struct node{
int data;
struct node *next;
}*front=NULL,*rear=NULL;
void create();
void peep();
void deletefrombeginningdequeue();
void insertatendenqueue();
void main()
{
create();
peep();
deletefrombeginningdequeue();
insertatendenqueue();
}
void create()
{
int s;
char c;
struct node *newnode;
do{
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the values");
scanf("%d",&s);
newnode->data=s;
newnode->next=NULL;
if(front==NULL)
{
front= newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
printf("enter y to continue or n to stop");
scanf(" %c",&c);
}while(c!='n');
}
void peep()
{
struct node *temp;
if(front ==NULL)
{
printf("queue is empty");
return;
}
else if(front->next==NULL)
{
printf("%d\n",front->data);
return;
}
else
{
temp=front;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
return;
}
}
void deletefrombeginningdequeue()
{
struct node *temp;
if(front==NULL)
printf("queue is empty");
else if(front->next==NULL)
{
temp=front;
free (temp);
front=NULL;
rear=NULL;
}
else
{
temp=front;
front=front->next;
free(temp);
}
peep();
}
void insertatendenqueue()
{
struct node *temp,*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
temp=front;
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode->data=7;
newnode->next=NULL;
temp->next=newnode;
peep();
}

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