You are on page 1of 11

a[0]=a[i];

a[i]=tmp;
percdown(a,0,i);
}
}

DOUBLY LINKED LIST :


 In a singly linked list one can move from the header node to any node in one
direction only (left-right).
 A doubly linked list is a two-way list because one can move in either
direction i.e., either from left to right or from right to left.
 It maintains two links or pointers. Hence, it is called as doubly linked list.
 Where, DATA field stores the element or data, PREV contains the address of
its previous node, NEXT contains the address of its next node.

struct node
{
int data;
struct node *next; // Pointer to next node in DLL
struct node *prev; // Pointer to previous node in DLL
};

void create()
{
int item;
struct node *new1;
char resp;
printf("\nCreating the Double Linked List...");
do
{
printf("\nEnter the value:");
scanf("%d",&item);
new1=(struct node *)malloc(sizeof(struct node));
new1->prev=NULL;
new1->data=item;
new1->next=NULL;
if(start==NULL)
{
start=new1;
ptr=new1;
}
else
{
ptr->next=new1;
new1->prev=ptr;
ptr=new1;
}
printf("\nDo you want to continue?Enter Y or N:");
resp=getche();
}while(resp=='y' || resp=='Y');
}

void insert_beg(int data)


{
struct node *new1;
new1=(struct node *)malloc(sizeof(struct node));
new1->data=data;
new1->prev=NULL;
new1->next=NULL;
new1->next=start;
start->prev=new1;
start=new1;
}

void insert_end(int data)


{
struct node *new1;
new1=(struct node *)malloc(sizeof(struct node));
new1->data=data;
new1->prev=NULL;
new1->next=NULL;
if(start==NULL) //If no node is created earlier
start=new1;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=new1;
new1->prev=ptr;
}
}

void insert_bet(int data,int loc)


{
struct node *new1,*temp;
int i;
new1=(struct node *)malloc(sizeof(struct node));
new1->data=data;
new1->prev=NULL;
new1->next=NULL;
ptr=start;
for(i=1;i<loc;i++)
ptr=ptr->next;
new1->next=ptr;
new1->prev=ptr->prev;
ptr->prev->next=new1;
ptr->prev=new1;
}

void del_beg()
{
if(start==NULL)
printf("\nThe DLL is empty.So cannot delete anything.");
else
{
start=start->next;
start->prev=NULL;
}
}

void del_end()
{
if(start==NULL)
printf("\nThe DLL is empty.So cannot delete anything.");
else if(start->next==NULL)
start=NULL;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->prev->next=NULL;
ptr->prev=NULL;
}
}

void del_bet(int loc)


{
int i;
if(start==NULL)
printf("\nThe DLL is empty.So cannot delete anything.");
else
{
ptr=start;
for(i=1;i<loc;i++)
ptr=ptr->next;
ptr->prev->next=ptr->next;
ptr->next->prev=ptr->prev;
}
}
void display_f()
{
if(start==NULL)
{
printf("\nThe DLL is empty.So no elements to display.");
}
else
{
ptr=start;
printf("\nThe elements in the list traversing forward are:");
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
}
}

STACK IMPLEMENTATION USING LINKED LIST


 Implementing stacks as linked lists provides a feasibility on the number of
nodes by dynamically growing stacks, as a linked list is a dynamic data
structure.
 The stack can grow or shrink as the program demands it to.
 Disadvantage of using an array to implement a stack or queue is the wastage
of space.
 A variable top always points to top element of the stack.
 top = NULL specifies stack is empty.
Example:
 The following list consists of five cells, each of which holds a data object
and a link to another cell

Push Function:
void push()
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("\n enter data of item:");
scanf("%d",&temp->data);
temp->link=top;
top=temp;
}
POP function
void pop()
{
struct node*temp;
if(top==NULL)
printf("\n stack is empty \n");
else
{
temp=top;
printf("popped item is %d \n",temp->data);
top=top->link;
free(temp);
}
}
void display()
{
struct node*temp;
temp=top;
if(top==NULL)
printf("stack is empty \n");
else
{
printf("stack elements:\n");
while(temp!=NULL)
{
printf("->%d",temp->data);
temp=temp->link;
}
}
}
TYPES OF EXPRESSIONS
Arithmetic expressions can be represented in three ways:
Infix notation: The operator is in between the two operands.
Examples: A+B 2+3
Prefix (Polish notation): The operator precedes the operands.
Examples: +AB +23
Postfix (Reverse Polish Notation): The operator follows the operands.
Examples: AB+ 23+

Consider the infix expression: 2 + 3 * (5 – 7) / 9


Let us insert implicit parentheses
(2 + ((3 * (5 – 7)) / 9))
Transfer the operators to the beginning of parentheses
(+ 2 (/ (* 3 (– 5 7)) 9))
Remove the parentheses: + 2 / * 3 – 5 7 9
This is the equivalent prefix expression.
Transfer the operators to the end of parentheses
(2 ((3 (5 7 –) *) 9 /) +)
Remove the parentheses: 2 3 5 7 – * 9 / +
This is the equivalent postfix expression.

Algorithm to convert infix expression to Postfix:


1. Initialize an empty stack.
2. Repeat the following process until the end of the infix expression.
l Get next input token (constant, variable, arithmetic operator, left
parenthesis, right parenthesis) in the infix expression.
l If the token is
l A left parenthesis: Push it onto the stack.
l A right parenthesis:
l Pop and display stack elements until a left parenthesis
is on the top of the stack.
l Pop the left parenthesis also, but do not display it.
l An operator:
l While the stack is nonempty and token has higher or
equal priority than the stack’s top element, pop and
display.
l Push token onto the stack.
l An operand: Display it.
3. When the infix expression is reached to the end, pop and display stack items
until the stack is empty.
(Note: Left parenthesis in the stack has lowest priority)

Postfix conversion function


void postfix(char inf[15])
{
char x;
for(i=0;inf[i]!='\0';i++)
{
if(inf[i]=='(')
push(inf[i]);
else if(isalpha(inf[i]))
pf[j++]=inf[i];
else if(inf[i]==')')
{
while(stack[top]!='(')
pf[j++]=pop();
x=pop();
}
else
{
while(prec(stack[top]>=prec(inf[i])))
pf[j++]=pop();
push(inf[i]);
}
}//end of for
while(top!=-1)
{
pf[j++]=pop();
pf[j]='\0';
}//end of postfix()
}

void push(int y)
{
stack[++top]=y;
}//end of push()

int pop()
{
return(stack[top--]);
}//end of pop()

int prec(char c)
{
if(c=='*' || c=='/')
return(5);
else if(c=='+' || c=='-')
return(3);
else if(c=='(')
return(1);
}//end of prec()
Algorithm to evaluate Postfix Expression:
1. Initialize an empty stack.
2. Do
l Get next token (constant, arithmetic operator) in postfix expression.
l If token is an operand, push it on the stack.
l If token is an operator do the following:
l Pop top two values from the stack. (If the stack does not
contain two items, report error.)
l Apply operator token to these two values.
l Push the resulting value onto the stack.
3. Continue the process until the end of the expression is encountered.
4. The value of the expression is on the top of the stack (and stack should
contain only this value).
Postfix evaluation function
void evaluate(char pf[15])
{
float a,b,x;
for(i=0;pf[i]!='\0';i++)
{
if(isalpha(pf[i]))
{
printf("enter value of %c",pf[i]);
scanf("%f",&x);
push(x);
}//end of if
else//if operator
{
b=pop();
a=pop();
switch(pf[i])
{
case '+':push(a+b);
break;

case '-':push(a-b);
break;
case '*':push(a*b);
break;
case '/':push(a/b);
break;
default: printf("invalid operator\n");
}//end of switch
}//end of else
}//end of for
printf("value of expression %f",stack[top--]);
}//end of evaluate()
void push(float y)
{
stack[++top]=y;
}
float pop()
{
return(stack[top--]);
}

Symbol Balancing:
void main()
{
char exp[MAX],temp;
int i, flag=1;
clrscr();
printf("Enter an expression : ");
gets(exp);
for(i=0;i<strlen(exp);i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push(exp[i]);
if(exp[i]==')' || exp[i]=='}'|| exp[i]==']')
{
if(top == -1)
flag=0;
else
{
temp=pop();
if(exp[i]==')' && (temp=='{' || temp=='['))
flag=0;
if(exp[i]=='}' && (temp=='(' || temp=='['))
flag=0;
if(exp[i]==']' && (temp=='(' || temp=='{'))
flag=0;
}
}
}

if(top>=0)
flag=0;
if(flag==1)
printf("\n Valid expression");
else printf("\n Invalid expression");
}

void push(char c)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
top=top+1; stk[top] = c;
}
}

char pop()
{
if(top == -1)
printf("\n Stack Underflow");
else
return(stk[top--]);
}

Queues Linked List


• There two basic functions in queue using linked list
1) Enqueue
2) Dequeue
Enqueue
• when we insert data into a queue, the queue’s front and rear pointers must
be set to the new node
• When we insert data into a queue with data already in it, we must point both
the link field in the last node and rear pointer to the new node.
• If insert is successful, it returns true else if no memory left for the new node,
it returns false
Dequeue
• If the queue is empty, we have underflow and return false
• We first ensure that the queue contains data.
• If the queue contains data, then set the front pointer to the next item in the
queue
• If we dequeue the last item, the queue front pointer automatically becomes
null pointer. it also assigns null pointer to link filed of the last node.

void enqueue()
{
int item;
struct queue *temp;
/*No need of checking for Overflow as it is DMA*/
temp=(struct queue *)malloc(sizeof(struct queue));
printf("\nEnter the item to enqueue:");
scanf("%d",&item);
temp->data=item;
temp->ptr=NULL;
if(f==NULL && r==NULL)
{
f=temp;
r=temp;
}
else
{
r->ptr=temp;
r=temp;
}
}

void dequeue()
{
if(r==NULL && f==NULL)
{
printf("\nQueue is underflown i.e no nodes to be deleted.");
}
else if(r==f)
{
printf("\nThe deleted element is:%d",f->data);
f=NULL;
r=NULL;
}
else
{
printf("\nThe deleted element is:%d",f->data);
f=f->ptr;
}
}

void display()
{
if(r==NULL)
printf("\nQueue is underflown i.e no nodes to be displayed.");
else
{
struct queue *i=f;
printf("\nThe elements in the queue are:");
while(i!=NULL)
{
printf("%d\t",i->data);
i=i->ptr;
}
}
}

You might also like