0% found this document useful (0 votes)
14 views65 pages

Linked List

Uploaded by

zeelsoni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views65 pages

Linked List

Uploaded by

zeelsoni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

Linked List (Dynamic Memory Allocation)

• Singly Linked List


• Doubly Linked list
• Circular linked list
• Linked implementation of Stack
• Linked implementation of Queue
• Applications of linked list
Why Linked List? (Advantages over Array)
 Arrays uses static memory allocation.
 int X[100] – occupies 100 memory blocks of 2 bytes.
 If in our program we are not using all 100 locations we are wasting memory.
 Which may be used for another tasks by operating system.
 More page replacements required with static allocation.
 Even with only one task executing at a time, dynamic control of
memory can prove useful.
 During one part of a task, a large amount of memory may be needed for some
purpose
 which can later be released and then allocated again for another purpose.
 A program can start small and grow only as necessary,
 so that when it is small, it can run more efficiently
 and, when necessary, it can grow to the limits of the computer system.
Ashish Patel @ SVMIT 2
What is Linked List?

Data Part Pointer Part Single Node

Ashish Patel @ SVMIT 3


Contd…
The linked list is an alternative to the array for storing data.
The linked list is implemented using pointers.
 Thus, an element (or node) of a linked list contains the actual data
to be stored and a pointer to the next node.

Recall that a pointer


 is simply the address in memory of the next node.
 Thus, a key difference from arrays is that a linked list does not
have to be stored contiguously in memory.

Ashish Patel @ SVMIT 4


Basic Structure of Linked List
struct Node {
// data members
Node_Data data; //data part
Node *next; //pointer part
}

 In linked list, each node contains a pointer to the next node in the list.
 The last node in the list contains a null (or zero) pointer.
 A circular list is identical to a chain except that the last node contains a
pointer to the first node.
 A doubly linked list differs from the chain and the circular list in that
each node contains two pointers.
 One points to the next node (as before), while the other points to the
previous node.

Ashish Patel @ SVMIT 5


Examples…

Doubly Linked List

Circular Linked List


Ashish Patel @ SVMIT 6
Linked List Implementation/Coding Issues in C

 We can define structures with pointer fields that refer to the structure
type containing them

struct list { data next


int data;
struct list *next;
}

 The pointer variable next is called a link. Each structure is linked to a


succeeding structure by way of the field next. The pointer variable
next contains an address of either the location in memory of the
successor struct list element or the special value NULL.

Ashish Patel @ SVMIT 7


Example
struct list a, b, c;

a.data = 1;
b.data = 2;
c.data = 3;
a.next = b.next = c.next = NULL;

a b c
1 NULL 2 NULL 3 NULL

data next data next data next

Ashish Patel @ SVMIT 8


Example continues
 a.next = &b
 b.next = &c
 c.next = ?
 a.data = 1
 b.data = 2
 c.data = 3
 a.next→data 2
 b.next→data 3
 c.next→data ?

a b c
1 2 3

Ashish Patel @ SVMIT 9


Sample Questions – GTU Question Papers

 Write a function in any programming language to insert an element


in an ordered list.
 Write a program to count number of nodes in a linked list.
 Write an algorithm to delete an element from a singly linked list.
 Write an algorithm/program to implement following operations in the
“Singly Linked List”.
 Insert the node at end
 Delete the node whose value = Y.

Ashish Patel @ SVMIT 10


Dynamic Memory Allocation
 Creating and maintaining dynamic data structures requires
dynamic memory allocation – the ability for a program to
obtain more memory space at execution time to hold new
values, and to release space no longer needed.

 In C, functions malloc and free, and operator sizeof are


essential to dynamic memory allocation.

Ashish Patel @ SVMIT 11


Dynamic Memory Operators sizeof and malloc
 Unary operator sizeof is used to determine the size in bytes
of any data type.
sizeof(double) sizeof(int)

 Function malloc takes as an argument the number of bytes


to be allocated and return a pointer of type void * to the
allocated memory. (A void * pointer may be assigned to a
variable of any pointer type. ) It is normally used with the
sizeof operator.
malloc(sizeof(struct ))

Ashish Patel @ SVMIT 12


Example of malloc()
#include <stdio.h>
#include <string.h> /* copy "Hello" into string */
#include <alloc.h> strcpy(str, "Hello");
#include <process.h>
/* display string */
int main(void) printf("String is %s\n", str);
{
char *str; /* free memory */
/* allocate memory for string */ free(str);
if ((str = (char *) malloc(10)) == NULL)
{ return 0;
printf("Not enough memory to allocate }
buffer\n");
exit(1); /* terminate program if out of
memory */
}

Ashish Patel @ SVMIT 13


Example of calloc()
#include <stdio.h> /* display string */
#include <alloc.h> printf("String is %s\n", str);
#include <string.h>
/* free memory */
int main(void) free(str);
{
char *str = NULL; return 0;
}
/* allocate memory for string */
str = (char *) calloc(10, sizeof(char));

/* copy "Hello" into string */


strcpy(str, "Hello");

Ashish Patel @ SVMIT 14


calloc malloc
Function allocates a region of memory allocates "size" bytes of memory.
large enough to hold "n
elements" of "size" bytes each.

Number of 2 1
arguments
Syntax void *calloc void *malloc (size_in_bytes);
(number_of_blocks,
size_of_each_block_in_bytes);

Contents of The allocated region is The contents of allocated memory


allocated initialized to zero. are not changed. i.e., the memory
memory contains unpredictable or garbage
values. This presents a risk.

Return value void pointer (void *). If the void pointer (void *). If the
allocation succeeds, a pointer allocation succeeds, a pointer to
to the block of memory is the block of memory is returned.
returned. If the allocation of If the allocation of memory fails, a
memory fails, a NULL pointer is NULL pointer is returned.
returned.
Ashish Patel @ SVMIT 15
Dynamic Memory Operators in C Example
struct node{
int data;
struct node *next;
};
struct node *ptr;

ptr = (struct node *) /*type casting */


malloc(sizeof(struct node));

ptr
?

Ashish Patel @ SVMIT 16


The Free Operator in C
 Function free deallocates memory- i.e. the memory is
returned to the system so that the memory can be
reallocated in the future.

free(ptr);
?
ptr

Ashish Patel @ SVMIT 17


Examples of the Nodes of a Linked List
 A node in a linked list is a structure that has at
least two fields. One of the fields is a data field;
the other is a pointer that contains the address of
the next node in the sequence.

 A node with one data field:


struct node{ number link

int number;
struct node * link;
};

Ashish Patel @ SVMIT 18


The Nodes of a Linked List – Examples

 A node with three data fields:

struct student
{
char name[20]; Name id grdPts next_student
int id;
double grdPts;
struct student *next_student;
}

Ashish Patel @ SVMIT 19


Basic Operations on a Linked List

1. Add a node.
2. Delete a node.
3. Search for a node.
4. Traverse (walk) the list. Useful for counting
operations or aggregate operations.

Ashish Patel @ SVMIT 20


Adding Nodes to a Linked List

There are four steps to add a node to a linked list:

 Allocate memory for the new node.


 Determine the insertion point (you need to know only the new
node’s predecessor
 Point the new node to its successor.
 Point the predecessor to the new node.

Pointer to the predecessor can be in one of two states:


 it can contain the address of a node (i.e. you are adding somewhere
after the first node – in the middle or at the end)
 it can be NULL (i.e. you are adding either to an empty list or at the
beginning of the list)

Ashish Patel @ SVMIT 21


Deleting a Node from a Linked List

 Deleting a node requires that we logically remove the node from the
list by changing various links and then physically deleting the node
from the list (i.e., return it to the heap).
 Any node in the list can be deleted. Note that if the only node in the
list is to be deleted, an empty list will result. In this case the head
pointer will be set to NULL.
 To physically delete a node:
 First locate the node itself and its logical predecessor.
 Change the predecessor’s link field to point to the deleted node’s
successor.
 Free the memory using the free() function.

Ashish Patel @ SVMIT 22


Example : C Program for Stack using Linked List - 1
 Creating Structure for linked list

1. include<stdio.h> 9. main()
2. struct link_list 10. {
3. { 11. int ch;
4. int data; 12.
5. struct link_list *next; 13. top = (node*)malloc(sizeof(node));
6. }; 14. top->next = NULL;
7. typedef struct link_list node;
8. node *top;

NOdata next NULL

top

Ashish Patel @ SVMIT 23


Example : C Program for Stack using Linked List - 2
 Display MENU with infinite WHILE loop

16. while(1)
17. {
18. printf("\n1.push\n2.pop\n3.display\n4.exit\n");
19. printf("Enter your Choice:\n");
20. scanf("%d",&ch);
21. switch(ch)
22. {
23. case 1: push(); break;
24. case 2: pop(); break;
25. case 3: display(top); break;
26. case 4: exit();
27. }
28. }
29. }

Ashish Patel @ SVMIT 24


Example : C Program for Stack using Linked List - 3
 PUSH Function

NOdata next NULL


30. push()
31. {
32. node *list; top
33. list = (node*)malloc(sizeof(node));
34. printf("Enter Data:\n"); data next
35. scanf("%d",&list->data);
36. list->next = top;
37. top = list; list
38. } top

Ashish Patel @ SVMIT 25


Example : C Program for Stack using Linked List - 4
 POP Function (logical delete)

NOdata next NULL


39. pop()
40. {
41. if(top->next != NULL)
42. {
43. printf("Popped Element is %d\n",top->data); top
44. top = top->next;
45. }
46. else
47. printf("Empty\n");
48. }
30 next 20 next 10 next

top
Ashish Patel @ SVMIT 26
Example : C Program for Stack using Linked List - 5
 DISPLAY Function

NOdata next NULL


49. display(node *list)
50. {
51. while(list->next != NULL)
52. {
53. printf("%d ==> ",list->data);
54. list = list->next;
55. }
56. }

30 next 20 next 10 next

list = top
Ashish Patel @ SVMIT 27
Singly Linked List - Operations
#include<stdio.h> while(1)
#include<stdlib.h> {
printf("\n---------Menu---------\n");
struct linked_list printf(“Give Options");
{ printf("\nEnter choice:");
int data; scanf("%d",&ch);
struct linked_list *next; switch(ch)
}; {
typedef struct linked_list node; case 1: begin_insert(); break;
node *head; case 2: end_insert(); break;
void begin_insert(); case 3: insert_at_loc(); break;
void end_insert(); case 4: begin_delete(); break;
void insert_at_loc(); case 5: end_delete(); break;
void begin_delete(); case 6: del_at_loc(); break;
void end_delete(); case 7: search_item(); break;
void del_at_loc(); case 8: display(); break;
void search_item() case 9: exit(0); break;
void display(); default: printf("\nEnter valid choice ");
int main () { } //switch
int ch; } //while
} //main

Ashish Patel @ SVMIT 28


begin_insert()

void begin_insert()
{
node *ptr;
int no;

ptr = (node *) malloc(sizeof(node));


printf("\nEnter number to insert at begin:");
scanf("%d", &no);
ptr->data = no;
ptr->next = head;
head = ptr;
}

Ashish Patel @ SVMIT 29


end_insert()
void end_insert() else
{ {
node *ptr, *temp; temp = head;
int no; while (temp->next != NULL)
{
ptr = (node *) malloc(sizeof(node)); temp = temp->next;
printf("\nEnter number to insert at end:"); }
scanf("%d",&no); temp->next = ptr;
ptr->data = no; ptr->next = NULL;
}
if(head == NULL) }
{
ptr->next = head;
head = ptr;
}

Ashish Patel @ SVMIT 30


insert_at_loc()

void insert_at_loc() for(i=0;i<loc-1;i++)


{ {
int i, loc, no; temp = temp->next;
node *ptr, *temp; if(temp == NULL)
{
ptr = (node *) malloc(sizeof(node)); printf("can't insert\n");
return;
printf("\nEnter number to insert: "); }
scanf("%d", &no); }
ptr->data = no; ptr->next = temp->next;
printf("\nEnter the location to insert: "); temp->next = ptr;
scanf("%d",&loc); }
temp = head;

Ashish Patel @ SVMIT 31


begin_delete()
void begin_delete()
{
node *ptr;

if(head == NULL)
{
printf("\nList is Empty");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nBegining node deleted");
}
}

Ashish Patel @ SVMIT 32


end_delete()
void end_delete() else
{ {
node *ptr, *temp; ptr = head;
while(ptr->next != NULL)
if(head == NULL) {
{ temp = ptr;
printf("\nList is Empty"); ptr = ptr ->next;
} }
else if (head -> next == NULL) temp->next = NULL;
{ free(ptr);
head = NULL; printf("\nLast node deleted\n");
free(head); }
printf("Only available node deleted"); }
}

Ashish Patel @ SVMIT 33


del_at_loc()
void del_at_loc()
{
node *ptr, *temp;
int loc, i;
printf("\nEnter the location after which you want to delete:");
scanf("%d", &loc);
ptr = head;
for(i=0; i<loc; i++)
{
temp = ptr;
ptr = ptr->next;
}
temp ->next = ptr ->next;
free(ptr);
printf("\nNode %d Deleted", loc+1);
}

Ashish Patel @ SVMIT 34


search_item()
void search_item() if(ptr->data == item)
{ {
node *ptr; printf(“Item is at location %d ", count+1);
int item, flag, count=0; flag = 0;
ptr = head; break;
if(ptr == NULL) }
{ else
printf("\nList is Empty"); flag = 1;
}
else count++;
{ ptr = ptr -> next;
printf("\nEnter item to search:"); } //while
scanf("%d", &item); if(flag == 1)
while (ptr != NULL) printf("Item not found\n");
{ } //else
} //function

Ashish Patel @ SVMIT 35


display()
void display()
{
node *ptr;
ptr = head;
if(ptr == NULL)
printf("\nList is Empty");
else
{
while (ptr != NULL)
{
printf("%d => ", ptr->data);
ptr = ptr->next;
}
}
}

Ashish Patel @ SVMIT 36


Singly Linked List : Example(1)
1. #include<stdio.h> 17. printf("Enter Location for Insert :\n");
2. struct link_list { 18. scanf("%d",&loc);
3. int info; 19. insert_at(head,loc);
4. struct link_list *next; 20. printf("After insertion :\n\n");
5. }; 21. display(head);
6. typedef struct link_list node; 22. printf("Enter Location for Delete :\n");
7. 23. scanf("%d",&del);
8. main() { 24. del_at(head,del);
9. int no,loc,del; 25. printf("After Deletion :\n\n");
10. node *head; 26. display(head);
11. head = (node *)malloc(sizeof(node)); 27. free(head);
12. creat(head); 28. }
13. display(head);
14. printf("Enter no for Search :\n");
15. scanf("%d",&no);
16. search(head,no);

Ashish Patel @ SVMIT 37


Singly Linked List : Example(2)
31. void creat(node *head) {
32. printf("Enter info (-1 for exit)\n");
33. scanf("%d", &head->info);
34. if(head->info != -1) {
35. head->next = (node *)malloc(sizeof(node));
36. creat(head->next);
37. }
38. else
39. head->next = NULL;
40. }

41. void display(node *head) {


42. if(head->next != NULL) {
43. printf("%d\n", head->info);
44. display(head->next);
45. }
46. }
Ashish Patel @ SVMIT 38
47. search(node *head, int no)
48. {
49. int count=0;
50. while(head->next != NULL)
51. {
52. count++;
53. if(head->info == no)
54. {
55. printf("value at %d",count);
56. return;
57. }
58. head = head->next;
59. }
60. }
Ashish Patel @ SVMIT 39
Singly Linked List : Example(3)
67. void del_at(node *head, int del) {
68. node *temp;
69. int count=0;
70. while(head->next != NULL) {
71. count++;
72. if(count == del-1)
73. {
74. temp = head->next;
75. head->next = head->next->next;
76. free(temp);
77. }
78. else
79. head = head->next;
80. }
81. }
Ashish Patel @ SVMIT 40
Doubly Linked List

 A node in a doubly linked list differs from that in a singly linked list in
that it has two pointers

 One points to the next node, while the other points to the previous
node

 This makes it possible to traverse the list in both directions

NULL Prev Data Next Prev Data Next Prev Data Next NULL

Node 1 Node 2 Node N

Ashish Patel @ SVMIT 41


Doubly Linked List - Structure

struct link_list
{
int info;
struct link_list *next;
struct link_list *prev;
};

typedef struct link_list node;


node *head;

Prev Info Next

head

Ashish Patel @ SVMIT 42


Doubly Linked List - Operations
1. Insert Next
2. Insert Previous

3. Delete Next
4. Delete Previous

5. Display
6. Search
7. Replace

8. Insert at Location
9. Delete from Location

Prev Info Next

head

Ashish Patel @ SVMIT 43


Doubly Linked List – main()
void main() case 1: ins_next();
{ break;
int ch; case 2: ins_prev();
head = (node*)malloc(sizeof(node)); break;
head->info=NULL; case 3: del_next();
head->next=NULL; break;
head->prev=NULL; case 4: del_prev();
while(1) break;
{ case 5: display();
printf ("\n\n------Main Menu--------\n\n"); break;
printf ("1.insert next\n"); case 6: exit();
printf ("2.insert previous\n"); default: printf ("Invalid \n");
printf ("3.delete next\n"); }
printf ("4.delete previous\n"); }
printf ("5.display\n"); }
printf ("6.Exit\n");
printf ("Enter Choice :"); NULL Prev NULL next NULL
scanf("%d",&ch);
switch(ch)
{ head
Ashish Patel @ SVMIT 44
Doubly Linked List – Insert Next

void ins_next() list =


{ (node*)malloc(sizeof(node));
node *list; printf("Enter Value(next)\n");
if(head->info == NULL) //First Insertion scanf("%d",&list->info);
{ list->prev = head;
printf("Enter Value(next)\n"); list->next = NULL;
scanf("%d",&head->info); head->next = list;
head->next = NULL; head = list;
head->prev = NULL; }
} }
else //Already link has nodes
{ while(head->next != NULL) Prev NOdata next NULL
{
head = head->next; //traverse to the end
} List

Ashish Patel @ SVMIT 45


Doubly Linked List – Insert Previous

void ins_prev() list =


{ (node*)malloc(sizeof(node));
node *list; printf("Enter Value(next)\n");
if(head->info == NULL) //first insertion scanf("%d",&list->info);
{ list->next = head;
printf("Enter Value(next)\n"); list->prev = NULL;
scanf("%d",&head->info); head->prev = list;
head->next = NULL; head = list;
head->prev = NULL; }
} }
else Prev NOdata next NULL
{ while(head->prev != NULL)
{
List
head = head->prev;
} //traverse up to first with prev

Ashish Patel @ SVMIT 46


Doubly Linked List – Delete Next

void del_next()
{
node *list;
while(head->next != NULL)
{
head = head->next;
}
list = head->prev;
printf("Deleted Element is %d\n",head->info);
free(head);
head = list; Prev NOdata next NULL
list->next = NULL;
display();
List
}

Ashish Patel @ SVMIT 47


Doubly Linked List – Delete Previous

void del_prev()
{
node *list;
while(head->prev != NULL)
{
head = head->prev;
}
list = head->next;
printf("Deleted Element is %d\n",head->info);
free(head);
head = list; Prev NOdata next NULL
list->prev = NULL;
display();
List
}

Ashish Patel @ SVMIT 48


Doubly Linked List – Display
void display()
{
while(head->prev != NULL)
{
head = head->prev;
}
while(head->next != NULL)
{
printf("%d\n",head->info);
head = head->next;
}
Prev NOdata next NULL
if(head->info != NULL)
{
printf("%d\n",head->info); List
}
}

Ashish Patel @ SVMIT 49


Circular Linked List
What is it?
 In linear linked lists if a list is traversed (all the elements visited) an
external pointer to the list must be preserved in order to be able to
reference the list again.
 Circular linked lists can be used to help the traverse the same list
again and again if needed. A circular list is very similar to the linear
list where in the circular list the pointer of the last node points not
NULL but the first node.

Data Next Data Next Data Next

Ashish Patel @ SVMIT 51


 In a circular linked list there are two methods to know if a node is the
first node or not.
 Either a external pointer, list, points the first node or
 A header node is placed as the first node of the circular list.

 The header node can be separated from the others by either heaving
a sentinel value as the info part or having a dedicated flag variable
to specify if the node is a header node or not

Ashish Patel @ SVMIT 52


Prımıtıve Functıons In Cırcular Lısts
 The structure definition of the circular linked lists and
the linear linked list is the same:
struct linked_list
{
int info;
struct linked_list *next;
};
typedef struct linked_list node;
node *head,*tail;

Ashish Patel @ SVMIT 53


Count Function
int count()
{
int count=1;
node *temp;
temp = head;
while(temp->next!=head)
{
count++;
temp=temp->next;
}
return count;
}

Ashish Patel @ SVMIT 54


Insert at last Function

void insert_last(node *newnode)


{
newnode->next=head;
tail->next=newnode;
tail=newnode;
}

Ashish Patel @ SVMIT 55


Insert at specific position
1. void insert_at(node *newnode) 10. Else
2. { 11. {
3. int loc,i;
12. for(i=1;i<loc;i++)
4. node *temp;
5. temp = head; 13. {
6. printf("Enter the Location"); 14. temp=temp->next;
7. scanf("%d",&loc); 15. }
8. if(loc==0 || loc>count() || head==NULL)
16. newnode->next=temp->next;
9. printf("cannot insert node \n");
17. temp->next=newnode;
18. if(temp==tail)
19. tail=newnode;
20. }
21. }

Ashish Patel @ SVMIT 56


Queue Using Linked List
Structure for Queue
typedef struct linklist
{
int info;
struct linklist *next;
} node;

node *rear;
node *front;

void insert(node *);


void deletion(node *);
void display(node *);
Ashish Patel @ SVMIT 58
main() function
1.m ain() 14. {
2.{ 15. case 1: insert(head);
3. int c; 16. break;
4. node *head; 17. case 2: deletion(front);
5. head=(node *)malloc(sizeof(node)); 18. break;
6. head->next=NULL; 19. case 3: if(front->next==NULL)
7. front=head; 20. printf("EMPTY”);
8.d o 21. else
9.{ 22. display(front);
10. printf("\nmain menu\n\n[1] for insert\ 23. break;
n[2] for delete\n[3] for list\n[4] for 24. case 4: exit();
exit"); 25. }
11. printf("\nenter your choice: "); 26. }while(c>0);
12. scanf("%d",&c); 27.}

13.switch(c)
Ashish Patel @ SVMIT 59
Insertion In Queue
void insert(node *rear)
{ else
node *temp; { while(rear->next!=NULL)
temp=(node *)malloc(sizeof(node)); {
rear=rear->next;
if(rear->next==NULL) }
{ printf("\n\nENTER THE VALUE: ");
printf("\n\nENTER THE VALUE: "); scanf("%d",&temp->info);
scanf("%d",&temp->info);
temp->next=NULL;
temp->next=NULL; rear->next=temp;
rear->next=temp; }
} }

Ashish Patel @ SVMIT 60


Delete from Queue
void deletion(node *ptr) {
node *temp;
if(ptr->next!=NULL) {
temp=ptr->next;
printf("%d",temp->info);
ptr=temp->next;
front->next=ptr;
free(temp);
}
else
printf("\nLIST IS EMPTY");
}

Ashish Patel @ SVMIT 61


Display Elements - Queue
void display(node *list)
{
while(list->next!=NULL)
{
printf("%d-->",list->next->info);
list=list->next;
}
}

Ashish Patel @ SVMIT 62


Evaluating Postfix Expression – Using Linked List

1. #include<stdio.h> 13. main()


2. #include<conio.h> 14. {
3. #include<math.h> 15. top = (node*)malloc(sizeof(node));
4. char s[20]; 16. top = NULL;
5. int len; 17. printf("Enter Postfix Exp:\n");
18. gets(s);
6. struct link_list 19. len = strlen(s);
7. { 20. s[len]=')'; //marker
8. int data; 21. evaluate();
9. struct link_list *next; 22. }
10. };

11. typedef struct link_list node;


12. node *top;

Ashish Patel @ SVMIT 63


Push() & Pop()
1. push(int n)
2. { 9. int pop()
3. node *list; 10. {
4. list = 11. int a;
(node*)malloc(sizeof(node));
12. node *list;
5. list->data = n; 13. list = top;
6. list->next = top; 14. a = list->data;
7. top = list; 15. top = list->next;
8. } 16. free(list);
17. return a;
18. }

Ashish Patel @ SVMIT 64


1. evaluate() 24. case '/' : opnd2 = pop();
2. { 25. opnd1 = pop();
3. int a=0,opnd1,opnd2,res,no; 26. res = opnd1 / opnd2;
4. while(s[a]!=')') 27. push(res);
5. { 28. break;
6. switch(s[a]) 29. case '^' : opnd2 = pop();
7. { 30. opnd1 = pop();
8. case '+': opnd2 = pop(); 31. res = pow(opnd1,opnd2);
9. opnd1 = pop(); 32. push(res);
10. res = opnd1 + opnd2; 33. break;
11. push(res);
12. break; 34. default : printf ("Enter value for %c",s[a]);
13. case '-': opnd2 = pop(); 35. scanf("%d",&no);
14. opnd1 = pop(); 36. push(no);
15. res = opnd1 - opnd2; 37. break;
16. push(res); 38. }
17. break; 39. a++;
18. case '*': opnd2 = pop(); 40. }
19. opnd1 = pop(); 41. printf ("Result = %d \n",top->data);
20. res = opnd1 * opnd2; 42. }
21. push(res);
22. break;
23.
Ashish Patel @ SVMIT 65

You might also like