Vous êtes sur la page 1sur 44

Applications of Stack

Expression Conversion
Infix to Postfix Ex:
a+b a+b*c (a + b) * (c - d) A+(B*C-(D/E-F)*G)*H A*(B+(C+D)*(E+F)/G)*H ab+ abc*ab+cd-* ABC*DE/F-G*-H*+
Operator Precedence () */% + -

Expression Evaluation
Postfix Evaluation Ex:
50,40,+,18,14,-,4,*,+ 100,40,8,+,20,10,-,+,* 5,6,9,+,80,5,*,-,/ 20,45,+,20,10,-,15,+,*

Algorithm for POSTFIX


Let P be the expression in POSTFIX notation Add ; at the end to mark end Expression is Evaluation from left to right Scan the postfix expression from left to right taking one element at a time while (element !=";") repeat the following steps { if(element = operand) it is PUSHed in STACK if(element == operator) { POP first element from TOP call it A POP second element from TOP call it B Perform OPERATION B operator A PUSH result in STACK } Pop the value from stack TOP which is result of the expression }

Infix to Prefix Conversion


Step 1. Push ) onto STACK, and add ( to end of the A Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty Step 3. If an operand is encountered add it to B Step 4. If a right parenthesis is encountered push it onto STACK Step 5. If an operator is encountered then: a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or higher precedence than the operator. b. Add operator to STACK Step 6. If left parenthesis is encontered then a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd) b. Remove the left parenthesis Step 7. Exit

Example
Expression = (A+B^C)*D+E^5 Step 1. Reverse the infix expression. 5^E+D*)C^B+A( Step 2. Make Every '(' as ')' and every ')' as '(' 5^E+D*(C^B+A) Step 3. Convert expression to prefix form. 5E^DCB^A+*+ Step 4. Reverse the string +*+A^BCD^E5

Reverse String

Linked List

A linked list is a data structure which can change during execution Successive elements are connected by pointers. Last element points to NULL It can grow or shrink in size during execution of a program. It can be made just as long as required. It does not waste memory space

Keeping track of a linked list:


Must know the pointer to the first element of the list (called start , head, etc.)

Linked lists provide flexibility in allowing the items to be rearranged efficiently


Insert an element Delete an element

Insertion

Deletion

Array versus Linked Lists


Arrays are suitable for:
Inserting/deleting an element at the end. Randomly accessing any element Searching the list for a particular value Linked lists are suitable for: Inserting an element Deleting an element Applications where sequential access is required In situations where the number of elements cannot be predicted beforehand

Types of Lists
Depending on the way in which the links are used to maintain adjacency, several different types of linked lists are possible
Linear singly-linked list (or simply linear list)

Circular linked list


The pointer from the last element in the list points back to the first element.

Doubly linked list


Pointers exist between adjacent nodes in both directions The list can be traversed either forward or backward Usually two pointers are maintained to keep track of the list, head and tail

Creating a list

Basic Operations on a List

struct stud {
int roll; struct stud *next;

};
/* A user-defined data type called node */
typedef struct stud node; node *head;

To start with, we have to create a node (the first node), and make head point to it head = (node *) malloc(sizeof(node));

If there are n number of nodes in the initial linked list:


Allocate n records, one by one Read in the fields of the records Modify the links of the records so that the chain is formed

struct stud { int roll; struct stud *next; To be called from main() function as: }; Struct stud *head; struct stud *create_list() ......... { head = create_list(); int k, n; struct stud *p, *head; printf ("\n How many elements to enter?"); scanf ("%d", &n); for (k=0; k<n; k++) { if (k == 0) { head = (struct stud *) malloc(sizeof(struct stud )); p = head; } else { p->next = (struct stud *) malloc(sizeof(struct stud)); p = p->next; } scanf ("%d, &p->roll); } p->next = NULL; return (head); }

To be called from main() function as:

node *head; ......... head = create_list();

Traversing the List


Once the linked list has been constructed and head points to the first node of the list, Follow the pointers Display the contents of the nodes as they are traversed Stop when the next pointer points to NULL

void display (node *head) { int count = 1; node *p; p = head; while (p != NULL) { printf ("\nNode %d: %d", count, p->roll); count++; p = p->next; } node *head; printf ("\n"); ......... display (head); }

Inserting a Node in a List


When a node is added at the beginning,
Only one next pointer needs to be modified
Head is made to point to the new node. New node points to the previously first element.

When a node is added at the end,


Two next pointers need to be modified.
Last node now points to the new node. New node points to NULL

When a node is added in the middle,


Two next pointers need to be modified.
Previous node now points to the new node. New node points to the next node

void insert (node **head) { int k = 0, rno; node *p, *q, *new; new = (node *) malloc(sizeof(node)); printf ("\nData to be inserted: "); scanf ("%d %s %d", &new->roll, new->name, &new->age); printf ("\nInsert before roll (-ve for end):"); scanf ("%d", &rno); p = *head; if (p->roll == rno) /* At the beginning */ { new->next = p; *head = new; }

else { while ((p != NULL) && (p->roll != rno)) { q = p; p = p->next; } if (p == NULL) /* At the end */ { q->next = new; new->next = NULL; } else if (p->roll == rno) /* In the middle */ { q->next = new; new->next = p; } } }

Deleting a node from the list


Here also we are required to delete a specified node
Say, the node whose roll field is given

Here also three conditions arise:


Deleting the first node. Deleting the last node. Deleting an intermediate node

void delete (node **head) { int rno; node *p, *q; printf ("\nDelete for roll :"); scanf ("%d", &rno); p = *head; if (p->roll == rno) /* Delete the first element */ { *head = p->next; free (p); }

else { while ((p != NULL) && (p->roll != rno)) { q = p; p = p->next; } if (p == NULL) /* Element not found */ printf ("\nNo match :: deletion failed"); else if (p->roll == rno) /* Delete any other element */ { q->next = p->next; free (p); } } }

Circular linked list

typedef struct Node { int data; struct Node *next; }node;

/* start always points to the first node of the linked list. temp is used to point to the last node of the linked list.*/

node *start,*temp; start = (node *)malloc(sizeof(node)); temp = start; temp -> next = start;

void insert(node *head, int data) { node *start = head;


/* Iterate through the list till we encounter the last node.*/

while(head ->next!=start) { head = head -> next; }


/* Allocate memory for the new node and put data in it.*/

head ->next = (node *)malloc(sizeof(node)); head = head ->next; head ->data = data; head ->next = start;

void print(node *start, node *head) { if(head ==start) { return; } printf("%d ", head ->data); print(start, head ->next); }
print(start,start->next);

void delete(node *head, int data) { node *start = head ; /* Go to the node for which the node next to it has to be deleted */ while(head ->next != start && (head ->next) -> data != data) { head = head -> next; } if(head ->next==start) {

printf("Element %d is not present in the list\n",data); return; }

/* Now pointer points to a node and the node next to it has to be removed */
node *temp; temp = head -> next; /*temp points to the node which has to be removed*/ head ->next = temp->next; /*We removed the node which is next to the pointer (which is also temp) */ free(temp);

Double Linked List

struct node { struct node *previous; int data; struct node *next; }*head=NULL, *last;

Insertion
void insert_begning(int value) { struct node *var,*temp; var=(struct node *)malloc(sizeof(struct node)); var->data=value; if(head==NULL) { head=var; head->previous=NULL; head->next=NULL; last=head; } else { temp=var; temp->previous=NULL; temp->next=head; head->previous=temp; head=temp; } }

void insert_end(int value) { struct node *var,*temp; var=(struct node *)malloc(sizeof(struct node)); var->data=value; if(head==NULL){ head=var; head->previous=NULL; head->next=NULL; last=head; } else{ last=head; while(last!=NULL) { temp=last; last=last->next; } last=var; temp->next=last; last->previous=temp; last->next=NULL; } }

int insert_after(int value, int loc) { struct node *temp,*var,*temp1; var=(struct node *)malloc(sizeof(struct node)); var->data=value; if(head==NULL) { head=var; head->previous=NULL; head->next=NULL; }else{ temp=head; while(temp!=NULL && temp->data!=loc) { temp=temp->next; } if(temp==NULL) { printf("\n%d is not present in list ",loc); } else

else
{ temp1=temp->next; temp->next=var; var->previous=temp; var->next=temp1; temp1->previous=var; }

} last=head; while(last->next!=NULL) { last=last->next; }


}

Deletion
int delete_from_end() { struct node *temp; temp=last; if(temp->previous==NULL) { free(temp); head=NULL; last=NULL; return 0; } printf("\nData deleted from list is %d \n",last->data); last=temp->previous; last->next=NULL; free(temp); return 0; }

int delete_from_middle(int value) { struct node *temp,*var,*t, *temp1; temp=head; while(temp!=NULL) { if(temp->data == value) { if(temp->previous==NULL) { free(temp); head=NULL; last=NULL; return 0; } else { var->next=temp1; temp1->previous=var; free(temp); return 0; } }

else { var=temp; temp=temp->next; temp1=temp->next; } } printf("data deleted from list is %d",value); }

Display
void display() { struct node *temp; temp=head; if(temp==NULL) { printf("List is Empty"); } while(temp!=NULL) { printf("-> %d ",temp->data); temp=temp->next; } }

Vous aimerez peut-être aussi