Skip to content

Commit 4d9e357

Browse files
Add files via upload
0 parents  commit 4d9e357

19 files changed

+1761
-0
lines changed

8 - Postfix_Evaluation.c

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Implement Postfix Evaluation using Stack.
2+
3+
#include<stdio.h>
4+
#define max 20
5+
void push(int[],int);
6+
int pop (int[]);
7+
int top=-1;
8+
void display(int[],int);
9+
10+
void main()
11+
{
12+
int s[max],x1,x2,result,i=0,ans,len;
13+
char p[50];
14+
15+
printf("Enter Postfix Expression : ");
16+
gets(p);
17+
18+
len=strlen(p);
19+
p[len]=')';
20+
21+
puts(p);
22+
while(p[i]!=')')
23+
{
24+
switch(p[i])
25+
{
26+
case '+':
27+
x1=pop(s);
28+
x2=pop(s);
29+
result=x2+x1;
30+
push(s,result);
31+
break;
32+
33+
case '-':
34+
x1=pop(s);
35+
x2=pop(s);
36+
result=x2-x1;
37+
push(s,result);
38+
break;
39+
40+
case '*':
41+
x1=pop(s);
42+
x2=pop(s);
43+
result=x2*x1;
44+
push(s,result);
45+
break;
46+
47+
case '/':
48+
x1=pop(s);
49+
x2=pop(s);
50+
result=x2/x1;
51+
push(s,result);
52+
break;
53+
54+
case '$':
55+
x1=pop(s);
56+
x2=pop(s);
57+
result=pow(x2,x1);
58+
push(s,result);
59+
break;
60+
61+
default:
62+
push(s,p[i]-48);
63+
break;
64+
}
65+
display(s,top);
66+
i++;
67+
}
68+
ans=pop(s);
69+
printf("Postfix Evaulation is : %d",ans);
70+
}
71+
72+
void push(int s[],int y)
73+
{
74+
75+
if(top>=max)
76+
{
77+
printf("\nStack is Overflow...!!");
78+
printf("\n\n");
79+
}
80+
else
81+
{
82+
top++;
83+
s[top]=y;
84+
}
85+
}
86+
87+
int pop(int s[])
88+
{
89+
int x;
90+
if(top<0)
91+
{
92+
printf("\nStack is Underflow...!!");
93+
printf("\n\n");
94+
}
95+
else
96+
{
97+
x=s[top];
98+
top--;
99+
return x;
100+
}
101+
}
102+
103+
void display(int s[],int top)
104+
{
105+
int i;
106+
printf("\nStack has Following Elements : \n");
107+
for(i=0;i<=top;i++)
108+
{
109+
printf("\t%d",s[i]);
110+
printf("\n");
111+
}
112+
printf("\n");
113+
}

Doubly_link_list_first_last_End.c

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<malloc.h>
4+
void ins_first(int);
5+
void ins_last(int);
6+
void Display();
7+
struct node
8+
{
9+
int info;
10+
struct node *lptr;
11+
struct node *rptr;
12+
}*l,*r;
13+
void main()
14+
{
15+
int choice,item;
16+
l=r=NULL;
17+
do
18+
{
19+
printf("\nDoubly Link List: \n(1)Insert at First \n(2)Insert at Last \n(3)Display \n(4)Exit \nEnter Your choice:");
20+
scanf("%d",&choice);
21+
switch(choice)
22+
{
23+
case 1:
24+
printf("\nEnter Item:");
25+
scanf("%d",&item);
26+
ins_first(item);
27+
break;
28+
29+
case 2:
30+
printf("\nEnter Item:");
31+
scanf("%d",&item);
32+
ins_last(item);
33+
break;
34+
35+
case 3:
36+
Display();
37+
break;
38+
}
39+
}while(choice < 4);
40+
}
41+
void ins_first(int item)
42+
{
43+
struct node *newnode;
44+
newnode=(struct node *)malloc(sizeof(struct node));
45+
if(newnode==NULL)
46+
{
47+
printf("\nMemory Not Allocated");
48+
getch();
49+
return;
50+
}
51+
newnode->info=item;
52+
if(l==NULL)
53+
{
54+
newnode->lptr=NULL;
55+
newnode->rptr=NULL;
56+
l=newnode;
57+
r=newnode;
58+
return;
59+
}
60+
else
61+
{
62+
newnode->lptr=NULL;
63+
newnode->rptr=l;
64+
l->lptr=newnode;
65+
l=newnode;
66+
return;
67+
}
68+
}
69+
void ins_last(int item)
70+
{
71+
struct node *newnode;
72+
newnode=(struct node *)malloc(sizeof(struct node));
73+
if(newnode==NULL)
74+
{
75+
printf("\nMemory Not Allocated" );
76+
getch();
77+
return;
78+
}
79+
newnode->info=item;
80+
if(l==NULL)
81+
{
82+
newnode->lptr=NULL;
83+
newnode->rptr=NULL;
84+
l=newnode;
85+
r=newnode;
86+
return;
87+
}
88+
else
89+
{
90+
newnode->lptr=r;
91+
newnode->rptr=NULL;
92+
r->rptr=newnode;
93+
r=newnode;
94+
return;
95+
}
96+
}
97+
void Display()
98+
{
99+
struct node *temp;
100+
if(l==NULL)
101+
{
102+
printf("\nEmpty Linked List...");
103+
getch();
104+
return;
105+
}
106+
printf("\nList is:");
107+
temp=l;
108+
while(temp!=NULL)
109+
{
110+
printf("\t%d",temp->info);
111+
temp=temp->rptr;
112+
}
113+
getch();
114+
}

In_Sorted_New_node_Linked List.c

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Implement algorithm of insert a newnode in sorted Link List //
2+
3+
//CODE:~
4+
5+
#include<stdio.h>
6+
#include<conio.h>
7+
#include<malloc.h>
8+
void insert_sorted(int);
9+
void display();
10+
struct node
11+
{
12+
int info;
13+
struct node *link;
14+
}*first;
15+
void main()
16+
{
17+
int item,choice,pos;
18+
do
19+
{
20+
printf("\n(1)Insert \(2)Display \n(3)Exit \n\nEnter Choice :");
21+
scanf("%d",&choice);
22+
switch(choice)
23+
{
24+
case 1:
25+
printf("\nEnter value for Insert :");
26+
scanf("%d",&item);
27+
insert_sorted(item);
28+
break;
29+
case 2:
30+
display();
31+
break;
32+
}
33+
}while(choice < 3);
34+
}
35+
void insert_sorted(int item)
36+
{
37+
struct node *newnode,*temp,*next;
38+
int flag = 0;
39+
newnode = (struct node*)malloc(sizeof(struct node));
40+
newnode->info = item;
41+
if(first==NULL)
42+
{
43+
newnode->link = NULL;
44+
first = newnode;
45+
}
46+
else
47+
{
48+
if(item<first->info)
49+
{
50+
newnode->link = first;
51+
first = newnode;
52+
}
53+
else
54+
{
55+
temp = first;
56+
while(temp->link!=NULL)
57+
{
58+
next = temp->link;
59+
if(item>temp->info && item<=next->info)
60+
{
61+
newnode->link=next;
62+
temp->link=newnode;
63+
flag=1;
64+
}
65+
temp = temp->link;
66+
}
67+
if(flag==0)
68+
{
69+
temp->link=newnode;
70+
newnode->link=NULL;
71+
}
72+
}
73+
}
74+
return;
75+
}
76+
void display()
77+
{
78+
struct node * temp;
79+
if(first==NULL)
80+
{
81+
printf("\nLink list is Empty");
82+
}
83+
else
84+
{
85+
temp = first;
86+
printf("\n\Link List :");
87+
while(temp!=NULL)
88+
{
89+
printf("\t%d",temp->info);
90+
temp=temp->link;
91+
}
92+
}
93+
return;
94+
}
95+
96+
//OUTPUT:~

Insertion_Sort.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//To Implement Insertion Sort
2+
//CODE:~
3+
4+
#include<stdio.h>
5+
#include<conio.h>
6+
void Insertion_sort(int[],int);
7+
void main()
8+
{
9+
int i,a[50],N;
10+
printf("\nEnter the Size of List =");
11+
scanf("%d",&N);
12+
printf("\nEnter Numbers:\n");
13+
for(i=0;i<N;i++)
14+
{
15+
scanf("%d",&a[i]);
16+
}
17+
Insertion_sort(a,N);
18+
printf("\nYour Sorted List:\n");
19+
for(i=0;i<N;i++)
20+
{
21+
printf("%d\n",a[i]);
22+
}
23+
getch();
24+
}
25+
void Insertion_sort(int array[],int N)
26+
{
27+
int i,j,key;
28+
for(i=1;i<=N-1;i++)
29+
{
30+
key=array[i];
31+
j=i;
32+
while(j>0 && array[j-1]>key)
33+
{
34+
array[j]=array[j-1];
35+
j--;
36+
}
37+
array[j]=key;
38+
}
39+
}

0 commit comments

Comments
 (0)