Professional Documents
Culture Documents
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,+,*
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
Insertion
Deletion
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)
Creating 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));
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); }
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); }
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; } } }
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); } } }
/* 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;
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) {
/* 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);
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; }
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; } }
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; } }