-
-
Save ibelgin/f00c3fe54aaa5007b8ebdae94d704a48 to your computer and use it in GitHub Desktop.
Lab Manual Programs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Single Linked List */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <conio.h> | |
#include <process.h> | |
#include <string.h> | |
struct node | |
{ | |
int label; | |
struct node *next; | |
}; | |
int main() | |
{ | |
int ch, fou=0; | |
int k; | |
clrscr(); | |
struct node *h, *temp, *head, *h1; | |
/* Head node construction */ | |
head = (struct node*) malloc(sizeof(struct node)); | |
head->label = -1; | |
head->next = NULL; | |
while(-1) | |
{ | |
printf("\n\n SINGLY LINKED LIST OPERATIONS \n"); | |
printf("1->Add "); | |
printf("2->Delete "); | |
printf("3->View "); | |
printf("4->Exit \n"); | |
printf("Enter your choice : "); | |
scanf("%d", &ch); | |
switch(ch) | |
{ | |
/* Add a node at any intermediate location */ | |
case 1: | |
printf("\n Enter label after which to add the element: "); | |
scanf("%d", &k); | |
h = head; | |
fou = 0; | |
if (h->label == k) | |
fou = 1; | |
while(h->next != NULL) | |
{ | |
if (h->label == k) | |
{ | |
fou=1; | |
break; | |
} | |
h = h->next; | |
} | |
if (h->label == k) | |
fou = 1; | |
if (fou != 1) | |
printf("Node not found\n"); | |
else | |
{ | |
temp=(struct node *)(malloc(sizeof(struct node))); | |
printf("Enter label for new node : "); | |
scanf("%d", &temp->label); | |
temp->next = h->next; | |
h->next = temp; | |
} | |
break; | |
/* Delete any intermediate node */ | |
case 2: | |
printf("Enter label of node to be deleted: "); | |
scanf("%d", &k); | |
fou = 0; | |
h = h1 = head; | |
while (h->next != NULL) | |
{ | |
h = h->next; | |
if (h->label == k) | |
{ | |
fou = 1; | |
break; | |
} | |
} | |
if (fou == 0) | |
printf("Sorry Node not found\n"); | |
else | |
{ | |
while (h1->next != h) | |
h1 = h1->next; | |
h1->next = h->next; | |
free(h); | |
printf("Node deleted successfully \n"); | |
} | |
break; | |
case 3: | |
printf("\n\n HEAD -> "); | |
h=head; | |
while (h->next != NULL) | |
{ | |
h = h->next; | |
printf("%d -> ",h->label); | |
} | |
printf("NULL"); | |
break; | |
case 4: | |
exit(0); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Stack using Single Linked List */ | |
#include <stdio.h> | |
#include <conio.h> | |
#include <process.h> | |
#include <stdlib.h> | |
struct node | |
{ | |
int label; | |
struct node *next; | |
}; | |
int main() | |
{ | |
int ch; | |
int k; | |
clrscr(); | |
struct node *h, *temp, *head; | |
/* Head node construction */ | |
head = (struct node*) malloc(sizeof(struct node)); | |
head->next = NULL; | |
while(1) | |
{ | |
printf("\n Stack using Linked List \n"); | |
printf("1->Push "); | |
printf(" 2->Pop "); | |
printf(" 3->View "); | |
printf(" 4->Exit \n"); | |
printf("Enter your choice : "); | |
scanf("%d", &ch); | |
switch(ch) | |
{ | |
case 1: | |
/* Create a new node */ | |
temp=(struct node *)(malloc(sizeof(struct node))); | |
printf("Enter label for new node : "); | |
scanf("%d", &temp->label); | |
h = head; | |
temp->next = h->next; | |
h->next = temp; | |
break; | |
case 2: | |
/* Delink the first node */ | |
h = head->next; | |
head->next = h->next; | |
printf("The element %d is deleted\n", h->label); | |
free(h); | |
break; | |
case 3: | |
printf("\n HEAD -> "); | |
h = head; | |
/* Loop till last node */ | |
while(h->next != NULL) | |
{ | |
h = h->next; | |
printf("%d -> ",h->label); | |
} | |
printf("NULL \n"); | |
break; | |
case 4: | |
exit(0); | |
default: | |
printf("Please enter a valid choice\n"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Queue using Single Linked List */ | |
#include <stdio.h> | |
#include <conio.h> | |
#include <process.h> | |
#include <stdlib.h> | |
struct node | |
{ | |
int label; | |
struct node *next; | |
}; | |
int main() | |
{ | |
int ch; | |
int k; | |
clrscr(); | |
struct node *h, *temp, *head; | |
/* Head node construction */ | |
head = (struct node*) malloc(sizeof(struct node)); | |
head->next = NULL; | |
while(1) | |
{ | |
printf("\n Queue using Linked List \n"); | |
printf("1->Insert "); | |
printf("2->Delete "); | |
printf("3->View "); | |
printf("4->Exit \n"); | |
printf("Enter your choice : "); | |
scanf("%d", &ch); | |
switch(ch) | |
{ | |
case 1: | |
/* Create a new node */ | |
temp=(struct node *)(malloc(sizeof(struct node))); | |
printf("Enter label for new node : "); | |
scanf("%d", &temp->label); | |
/* Reorganize the links */ | |
h = head; | |
while (h->next != NULL) | |
h = h->next; | |
h->next = temp; | |
temp->next = NULL; | |
break; | |
case 2: | |
/* Delink the first node */ | |
if(head->next == NULL) | |
{ | |
printf("Stack is UnderFlow"); | |
} | |
else | |
{ | |
h = head->next; | |
head->next = h->next; | |
printf("The element %d is deleted \n",h->label); | |
free(h); | |
} | |
break; | |
case 3: | |
printf("\n\nHEAD -> "); | |
h=head; | |
while (h->next!=NULL) | |
{ | |
h = h->next; | |
printf("%d -> ",h->label); | |
} | |
printf("NULL \n"); | |
break; | |
case 4: | |
exit(0); | |
default: | |
printf("Please enter a valid choice"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
typedef struct link { | |
int coeff; | |
int pow; | |
struct link * next; | |
} my_poly; | |
void my_create_poly(my_poly **); | |
void my_show_poly(my_poly *); | |
void my_add_poly(my_poly **, my_poly *, my_poly *); | |
int main(void) { | |
int ch; | |
do { | |
my_poly * poly1, * poly2, * poly3; | |
printf("\nCreate 1st expression\n"); | |
my_create_poly(&poly1); | |
printf("\nStored the 1st expression"); | |
my_show_poly(poly1); | |
printf("\nCreate 2nd expression\n"); | |
my_create_poly(&poly2); | |
printf("\nStored the 2nd expression"); | |
my_show_poly(poly2); | |
my_add_poly(&poly3, poly1, poly2); | |
my_show_poly(poly3); | |
printf("\nAdd two more expressions? (Y = 1/N = 0): "); | |
scanf("%d", &ch); | |
} while (ch); | |
return 0; | |
} | |
void my_create_poly(my_poly ** node) { | |
int flag; | |
int coeff, pow; | |
my_poly * tmp_node; | |
tmp_node = (my_poly *) malloc(sizeof(my_poly)); | |
*node = tmp_node; | |
do { | |
printf("\nEnter Coeff:"); | |
scanf("%d", &coeff); | |
tmp_node->coeff = coeff; | |
printf("\nEnter Pow:"); | |
scanf("%d", &pow); | |
tmp_node->pow = pow; | |
tmp_node->next = NULL; | |
printf("\nContinue adding more terms to the polynomial list?(Y = 1/N = 0): "); | |
scanf("%d", &flag); | |
if(flag) { | |
tmp_node->next = (my_poly *) malloc(sizeof(my_poly)); //Grow the list | |
tmp_node = tmp_node->next; | |
tmp_node->next = NULL; | |
} | |
} while (flag); | |
} | |
void my_show_poly(my_poly * node) { | |
printf("\nThe polynomial expression is:\n"); | |
while(node != NULL) { | |
printf("%dx^%d", node->coeff, node->pow); | |
node = node->next; | |
if(node != NULL) | |
printf(" + "); | |
} | |
} | |
void my_add_poly(my_poly ** result, my_poly * poly1, my_poly * poly2) { | |
my_poly * tmp_node; //Temporary storage for the linked list | |
tmp_node = (my_poly *) malloc(sizeof(my_poly)); | |
tmp_node->next = NULL; | |
*result = tmp_node; | |
while(poly1 && poly2) { | |
if (poly1->pow > poly2->pow) { | |
tmp_node->pow = poly1->pow; | |
tmp_node->coeff = poly1->coeff; | |
poly1 = poly1->next; | |
} | |
else if (poly1->pow < poly2->pow) { | |
tmp_node->pow = poly2->pow; | |
tmp_node->coeff = poly2->coeff; | |
poly2 = poly2->next; | |
} | |
else { | |
tmp_node->pow = poly1->pow; | |
tmp_node->coeff = poly1->coeff + poly2->coeff; | |
poly1 = poly1->next; | |
poly2 = poly2->next; | |
} | |
if(poly1 && poly2) { | |
tmp_node->next = (my_poly *) malloc(sizeof(my_poly)); | |
tmp_node = tmp_node->next; | |
tmp_node->next = NULL; | |
} | |
} | |
while(poly1 || poly2) { | |
tmp_node->next = (my_poly *) malloc(sizeof(my_poly)); | |
tmp_node = tmp_node->next; | |
tmp_node->next = NULL; | |
if(poly1) { | |
tmp_node->pow = poly1->pow; | |
tmp_node->coeff = poly1->coeff; | |
poly1 = poly1->next; | |
} | |
if(poly2) { | |
tmp_node->pow = poly2->pow; | |
tmp_node->coeff = poly2->coeff; | |
poly2 = poly2->next; | |
} | |
} | |
printf("\nAddition Complete"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<ctype.h> | |
#include<string.h> | |
#define SIZE 100 | |
char stack[SIZE]; | |
int top = -1; | |
void push(char item) | |
{ | |
if(top >= SIZE-1) | |
{ | |
printf("\nStack Overflow."); | |
} | |
else | |
{ | |
top = top+1; | |
stack[top] = item; | |
} | |
} | |
char pop() | |
{ | |
char item ; | |
if(top <0) | |
{ | |
printf("stack under flow: invalid infix expression"); | |
getchar(); | |
exit(1); | |
} | |
else | |
{ | |
item = stack[top]; | |
top = top-1; | |
return(item); | |
} | |
} | |
int is_operator(char symbol) | |
{ | |
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-') | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
int precedence(char symbol) | |
{ | |
if(symbol == '^') | |
{ | |
return(3); | |
} | |
else if(symbol == '*' || symbol == '/') | |
{ | |
return(2); | |
} | |
else if(symbol == '+' || symbol == '-') | |
{ | |
return(1); | |
} | |
else | |
{ | |
return(0); | |
} | |
} | |
void InfixToPostfix(char infix_exp[], char postfix_exp[]) | |
{ | |
int i, j; | |
char item; | |
char x; | |
push('('); | |
strcat(infix_exp,")"); | |
i=0; | |
j=0; | |
item=infix_exp[i]; | |
while(item != '\0') | |
{ | |
if(item == '(') | |
{ | |
push(item); | |
} | |
else if( isdigit(item) || isalpha(item)) | |
{ | |
postfix_exp[j] = item; | |
j++; | |
} | |
else if(is_operator(item) == 1) | |
{ | |
x=pop(); | |
while(is_operator(x) == 1 && precedence(x)>= precedence(item)) | |
{ | |
postfix_exp[j] = x; | |
j++; | |
x = pop(); | |
} | |
push(x); | |
push(item); | |
} | |
else if(item == ')') | |
{ | |
x = pop(); | |
while(x != '(') | |
{ | |
postfix_exp[j] = x; | |
j++; | |
x = pop(); | |
} | |
} | |
else | |
{ | |
printf("\nInvalid infix Expression.\n"); | |
getchar(); | |
exit(1); | |
} | |
i++; | |
item = infix_exp[i]; | |
if(top>0) | |
{ | |
printf("\nInvalid infix Expression.\n"); | |
getchar(); | |
exit(1); | |
} | |
if(top>0) | |
{ | |
printf("\nInvalid infix Expression.\n"); | |
getchar(); | |
exit(1); | |
} | |
postfix_exp[j] = '\0'; | |
} | |
int main() | |
{ | |
char infix[SIZE], postfix[SIZE]; | |
printf("ASSUMPTION: The infix expression contains single letter variables and single digit constants only.\n"); | |
printf("\nEnter Infix expression : "); | |
gets(infix); | |
InfixToPostfix(infix,postfix); | |
printf("Postfix Expression: "); | |
puts(postfix); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment