Data Structures
Data Structures
int info;
struct node *link;
};
p->info=start->info;
p=prt->link;
}
insertion at the end of list
struct node *addatend(struct node *start, int data)
{
struct node *p,*tmp;
tmp=(struct node*)malloc(sizeof(struct node));
tmp->info=data ;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=tmp ;
tmp->link=null;
return start;
}
insertion in b/w the list nodes //p,q are two nodes
tmp->link=p->link;
p->link=tmp;
struct node *add before(struct node *start, int data, int item)
{
struct node *tmp,*p;
if(start==null)
{
prinf(“list is empty”);
return start;
}
////if data inserted before first node//////////////////
if(item==start->info)
{
tmp=(struct node*)malloc(sizeof(struct node));
tmp->info=data ;
tmp->link=start ;
start=tmp;
return start;
}
p=start;
while(p->link!=null)
{
if(p->link->info==item)
{
tmp=(struct node*)malloc(sizeof(struct node));
tmp->info=data ;
tmp->link=p->link;
p->link=tmp;
return start;
}
p=p->link;
}
printf(%d not present in list ,item);
return start;
}
while(ptr!=null)
{
next=ptr->link;
ptr->link=prev;
prev=ptr;
ptr=next;
}
start=prev;
return start;
}
Stack
Stack is a specialized data storage structure (Abstract data type).
It has two main functions push and pop.
Insertion in a stack is done using push function and
removal from a stack is done using pop function.
Stack allows access to only the last element inserted hence, an item can be inserted or
removed from the stack from one end called the top of the stack.
, also called Last-In-First-Out (LIFO) list.
Stack has three properties: capacity stands for the maximum number of elements
stack can hold, size stands for the current size of the stack and elements is the array
of elements.
Singe Linked list
Singly linked list is the most basic linked data structure.
In this the elements can be placed anywhere in the heap memory which uses
contiguous locations.
Nodes in a linked list are linked together using a next field, which stores the address
of the next node in the next field of the previous node .
each node of the list refers to its successor and the last node contains the NULL
reference. It has a dynamic size, which can be determined only at run time.
Basic operations of a singly-linked list are:
Insert – Inserts a new element at the end of the list.
Delete – Deletes any node from the list.
Find – Finds any node in the list.
Print – Prints the list.
Performance
1. The advantage of a singly linked list is its ability to expand to accept virtually unlimited number
of nodes in a fragmented memory environment.
2. The disadvantage is its speed. Operations in a singly-linked list are slow as it uses sequential
search to locate a node.
Performance
1. The advantage of a doubly linked list is that we don’t need to keep track of the previous node for
traversal or no need of traversing the whole list for finding the previous node.
2. The disadvantage is that more pointers needs to be handled and more links need to updated.
Related Tutorials :
Queue
Queue is a specialized data storage structure (Abstract data type). . It has two main
operations enqueue and dequeue. Insertion in a queue is done using enqueue
function and removal from a queue is done using dequeue function. An item can be
inserted at the end (‘rear’) of the queue and removed from the front (‘front’) of the
queue. It is therefore, also called First-In-First-Out (FIFO) list.
Queue has five properties - capacity stands for the maximum number of elements
Queue can hold, size stands for the current size of the Queue, elements is the array of
elements, front is the index of first element (the index at which we remove the
element) and rear is the index of last element (the index at which we insert the
element).
Related Tutorials :
Last In First Out data structures
( LIFO ). Like a stack of cards First in First Out data structure (FIFO). Like
from which you pick up the one on people waiting to buy tickets in a queue - the
the top ( which is the last one to be first one to stand in the queue, gets the ticket
placed on top of the stack ). first and gets to leave the queue first.
Queu
Stacks Documentation of the various Documentation of the various operations and
es
operations and the stages a stack the stages a queue passes through as elements
passes through when elements are are inserted or deleted. C Program source
inserted or deleted. C program to code to help you get an idea of how a queue
help you get an idea of how a stack is implemented in code.
is implemented in code.
if (head!=NULL)
{
while (fast_ptr != NULL && fast_ptr->next != NULL)
{
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}
printf("The middle element is [%d]\n\n", slow_ptr->data);
}
}