Coding Seekho
Ir al canal en Telegram
Live Programming Videos : C, C++, Data Structure, Advanced Data Structure, Core Java, Advanced Java, Python
Mostrar másEl país no está especificadoTecnologías y Aplicaciones15 765
6 842
Suscriptores
+224 horas
-77 días
+530 días
Archivo de publicaciones
6 855
import turtle
import random
import time
delay=0.1
sc=0
hs=0
bodies=[]
#creating a screen
s1=turtle.Screen()
s1.title("Snake Game")
s1.bgcolor("light blue")
s1.setup(height=600,width=600)
#creating a head
h1=turtle.Turtle()
h1.shape("circle")
h1.color("red")
h1.fillcolor("blue")
h1.speed(0)
h1.penup()
h1.ht()
h1.goto(280,200)
h1.st()
h1.direction="stop"
#creating a food
f1=turtle.Turtle()
f1.shape("square")
f1.color("green")
f1.speed(0)
f1.penup()
f1.ht()
f1.goto(-280,200)
f1.st()
f1.direction="stop"
#creating a scoreboard
sb=turtle.Turtle()
sb.ht()
sb.penup()
sb.goto(-260,280)
sb.write("Score:0 | Highest Score:0")
def moveup():
if h1.direction!="down":
h1.direction="up"
def movedown():
if h1.direction!="up":
h1.direction="down"
def moveright():
if h1.direction!="left":
h1.direction="right"
def moveleft():
if h1.direction!="right":
h1.direction="left"
def movestop():
h1.direction="stop"
def move():
if h1.direction=="up":
y=h1.ycor()
h1.sety(y+20)
if h1.direction=="down":
y=h1.ycor()
h1.sety(y-20)
if h1.direction=="left":
x=h1.xcor()
h1.setx(x-20)
if h1.direction=="right":
x=h1.xcor()
h1.setx(x+20)
#event handling
s1.listen()
s1.onkey(moveup,"Up")
s1.onkey(movedown,"Down")
s1.onkey(moveleft,"Left")
s1.onkey(moveright,"Right")
s1.onkey(movestop,"space")
while True:
s1.update()
#check collision with border
if h1.xcor()>290:
h1.setx(-300)
if h1.xcor()<-290:
h1.setx(300)
if h1.ycor()>290:
h1.sety(-300)
if h1.ycor()<-290:
h1.sety(300)
#check collision with food
if h1.distance(f1)<20:
x=random.randint(-300,300)
y=random.randint(-300,300)
f1.goto(x,y)
#increase the length of snake
body=turtle.Turtle()
body.speed(0)
body.shape("square")
body.penup()
body.color("yellow")
bodies.append(body)
sc=sc+100
if sc>hs:
hs=sc
sb.clear()
sb.write("Score:{} | Highest Score:{}".format(sc,hs))
delay=delay-0.001
#move snake body
for i in range(len(bodies)-1,0,-1):
x=bodies[i-1].xcor()
y=bodies[i-1].ycor()
bodies[i].goto(x,y)
if len(bodies)>0:
x=h1.xcor()
y=h1.ycor()
bodies[0].goto(x,y)
move()
#check collision with body
for b in bodies:
if b.distance(h1)<20:
time.sleep(1)
h1.goto(0,0)
h1.direction="stop"
for b in bodies:
b.ht()
bodies.clear()
sc=0
delay=0.1
sb.clear()
sb.write("Score:{} | Highest Score:{}".format(sc,hs))
time.sleep(delay)
s1.mainloop()
6 855
char stack[100];
int top=-1;
char pop()
{
char c1=stack[top];
top--;
return c1;
}
void push(char ch)
{
top++;
stack[top]=ch;
}
int isOperator(char ch)
{
if(ch=='+' ch=='-' ch=='*' ch=='/' ch=='^')
{
return 1;
}
else
{
return 0;
}
}
char peek()
{
return stack[top];
}
precedence(char ch)
{
if(ch=='^')
{
return 3;
}
else if(ch=='/' ch=='*')
{
return 2;
}
else if(ch=='+' ch=='-')
{
return 1;
}
else
{
return 0;
}
}
int isOperand(char ch)
{
if(isalnum(ch))
{
return 1;
}
else
{
return 0;
}
}
void infixtopostfix(char infix[],char postfix[])
{
int i=0,j=0;
char ch;
while(infix[i]!='\0')
{
ch=infix[i];
if(isOperand(ch))
{
postfix[j]=ch;
j++;
}
else if(ch=='(')
{
push(ch);
}
else if(ch==')')
{
while(top!=-1 && peek()!='(')
{
postfix[j]=pop();
j++;
}
if(top!=-1)
{
pop();
}
}
else if(isOperator(ch))
{
while(top!=-1 && peek()!='(' &&
(precedence(peek()))>=(precedence(ch)))
{
postfix[j]=pop();
j++;
}
push(ch);
}
i++;
}
while(top!=-1)
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
}
void main()
{
char infix[100],postfix[100];
printf("Enter infix expression:");
scanf("%99s",infix);
infixtopostfix(infix,postfix);
printf("\nPstfix expression is: %s",postfix);
getch();
}
6 855
#include<stdio.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *start=NULL;
node *cn()
{
node *g1;
g1=(node *)malloc(sizeof(node));
printf("Enter a data:");
scanf("%d",&g1->data);
g1->next=g1;
return g1;
}
void is()
{
node *t1,*t2;
t1=cn();
if(start==NULL)
{
start=t1;
}
else
{
t1->next=start;
t2=start;
while(t2->next!=start)
{
t2=t2->next;
}
t2->next=t1;
start=t1;
}
}
void ie()
{
node *b1,*y1;
b1=cn();
if(start==NULL)
{
start=b1;
}
else
{
y1=start;
while(y1->next!=start)
{
y1=y1->next;
}
y1->next=b1;
b1->next=start;
}
}
void im()
{
int d,i;
node *j1,*j2,*j3;
j1=cn();
if(start==NULL)
{
start=j1;
}
else
{
j2=start;
printf("Enter where you want to insert:");
scanf("%d",&d);
for(i=0;i<d-2;i++)
{
j2=j2->next;
}
j3=j2->next;
j2->next=j1;
j1->next=j3;
}
}
ds()
{
node *x1;
if(start==NULL)
{
printf("There is no any node to delete");
}
else
{
x1=start;
while(x1->next!=start)
{
x1=x1->next;
}
x1->next=start->next;
x1=start;
start=start->next;
x1->next=NULL;
free(x1);
}
}
void de()
{
node *y1,*y2;
if(start==NULL)
{
printf("There is no any node to delete");
}
else
{
y1=start;
while(y1->next->next!=start)
{
y1=y1->next;
}
y2=y1->next;
y1->next=start;
y2->next=NULL;
free(y2);
}
}
dm()
{
}
display()
{
node *u1;
if(start==NULL)
{
printf("There is no any node to display");
}
else
{
u1=start;
while(u1->next!=start)
{
printf("%d ",u1->data);
u1=u1->next;
}
printf("%d ",u1->data);
}
}
void main()
{
int ch;
printf("\n1. Insert start");
printf("\n2. Insert End");
printf("\n3. Insert Middle");
printf("\n4. Delete start");
printf("\n5. Delete end");
printf("\n6. Delete middle");
printf("\n7. Display");
printf("\n8. Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
is();
break;
}
case 2:
{
ie();
break;
}
case 3:
{
im();
break;
}
case 4:
{
ds();
break;
}
case 5:
{
de();
break;
}
case 6:
{
dm();
break;
}
case 7:
{
display();
break;
}
case 8:
{
exit(0);
}
default:
{
printf("Invalid Input");
}
}
}
getch();
}
6 855
#include<stdio.h>
struct node
{
struct node *prev;
int info;
struct node *next;
};
struct node *head=NULL;
cn()
{
struct node *g1;
g1=(struct node *)malloc(sizeof(struct node));
printf("Enter a data:");
scanf("%d",&g1->info);
g1->prev=NULL;
g1->next=NULL;
return g1;
}
is()
{
struct node *s1;
s1=cn();
if(head==NULL)
{
head=s1;
}
else
{
s1->next=head;
head->prev=s1;
head=s1;
}
}
ie()
{
struct node *j1,*h1;
j1=cn();
if(head==NULL)
{
head=j1;
}
else
{
h1=head;
while(h1->next!=NULL)
h1=h1->next;
j1->prev=h1;
h1->next=j1;
}
}
im()
{
}
ds()
{
struct node *v1;
if(head==NULL)
{
printf("There is no any node to delete");
}
else
{
v1=head;
head=head->next;
head->prev=NULL;
v1->next=NULL;
free(v1);
}
}
de()
{
struct node *u1;
if(head==NULL)
{
printf("There is no any node to delete");
}
else
{
u1=head;
while(u1->next!=NULL)
{
u1=u1->next;
}
u1->prev->next=NULL;
u1->prev=NULL;
free(u1);
}
}
dm()
{
int d;
struct node *u1;
if(head==NULL)
{
printf("There is no any node to delete");
}
else
{
u1=head;
printf("Enter a data that you want to delete:");
scanf("%d",&d);
while(u1->info!=d)
{
u1=u1->next;
}
u1->prev->next=u1->next;
u1->next->prev=u1->prev;
u1->prev=NULL;
u1->next=NULL;
free(u1);
}
}
display()
{
struct node *b1;
if(head==NULL)
{
printf("There is no any node to display");
}
else
{
b1=head;
while(b1!=NULL)
{
printf("%d ",b1->info);
b1=b1->next;
}
}
}
void main()
{
int ch;
printf("\n1. Insert start");
printf("\n2. Insert End");
printf("\n3. Insert Middle");
printf("\n4. Delete start");
printf("\n5. Delete end");
printf("\n6. Delete middle");
printf("\n7. Display");
printf("\n8. Exit");
while(1)
{
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
is();
break;
}
case 2:
{
ie();
break;
}
case 3:
{
im();
break;
}
case 4:
{
ds();
break;
}
case 5:
{
de();
break;
}
case 6:
{
dm();
break;
}
case 7:
{
display();
break;
}
case 8:
{
exit(0);
}
default:
{
printf("Invalid Input");
}
}
}
getch();
}
6 855
struct node
{
int data;
struct node *next;
};
struct node *start=0;
struct node *createNode()
{
struct node *k1;
k1=(struct node *)malloc(sizeof(struct node));
printf("Enter a data:");
scanf("%d",&k1->data);
k1->next=0;
return k1;
}
void insertStart()
{
struct node *b1;
b1=createNode();
if(start==0)
{
start=b1;
}
else
{
b1->next=start;
start=b1;
}
}
void insertEnd()
{
struct node *x1,*v1;
x1=createNode();
if(start==0)
{
start=x1;
}
else
{
v1=start;
while(v1->next!=0)
{
v1=v1->next;
}
v1->next=x1;
}
}
void insertMiddle()
{
struct node *b1,*b2;
int d;
b1=createNode();
if(start==0)
{
start=b1;
}
printf("Enter a data where you want to store:");
scanf("%d",&d);
b2=start;
while(b2->data!=d)
{
b2=b2->next;
}
b1->next=b2->next;
b2->next=b1;
}
void deleteStart()
{
struct node *g1;
if(start==0)
{
printf("There is no any node to delete");
}
else
{
g1=start;
start=start->next;
g1->next=0;
free(g1);
printf("Node Deleted Successfully");
}
}
void deleteEnd()
{
struct node *p1,*p2;
if(start==0)
{
printf("There is no any node to delete");
}
else if(start->next==0)
{
free(start);
start=0;
}
else
{
p1=start;
while(p1->next->next!=0)
{
p1=p1->next;
}
p2=p1->next;
p1->next=0;
free(p2);
}
}
void deleteMiddle()
{
}
void display()
{
struct node *d1;
if(start==0)
{
printf("There is no any node to display");
}
else
{
d1=start;
while(d1!=0)
{
printf("%d ",d1->data);
d1=d1->next;
}
}
}
void main()
{
int ch;
printf("\n1. Insert from start.");
printf("\n2. Insert from end.");
printf("\n3. Insert from middle.");
printf("\n4. Delete from start.");
printf("\n5. Delete from end.");
printf("\n6. Delete from middle.");
printf("\n7. Display");
printf("\n8. Exit");
while(1)
{
printf("\nEnter you choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
insertStart();
break;
}
case 2:
{
insertEnd();
break;
}
case 3:
{
insertMiddle();
break;
}
case 4:
{
deleteStart();
break;
}
case 5:
{
deleteEnd();
break;
}
case 6:
{
deleteMiddle();
break;
}
case 7:
{
display();
break;
}
case 8:
{
exit(0);
}
default:
{
printf("Wrong Input");
}
}
}
getch();
}
6 855
void quick_sort(int a[],int N)
{
int top=-1;
int beg;
int end;
int pivot;
int lower[20];
int upper[20];
if(N>1)
{
top++;
lower[top]=0;
upper[top]=N-1;
}
while(top!=-1)
{
beg=lower[top];
end=upper[top];
top--;
pivot=quick(a,beg,end,pivot);
if(bega[r])
{
t=a[pivot];
a[pivot]=a[r];
a[r]=t;
pivot=r;
}
while(a[pivot]>=a[l] && pivot!=l)
{
l++;
}
if(pivot==l)
{
return pivot;
}
if(a[pivot]
6 855
#include <stdio.h>
#include <conio.h>
void merge_sort(int a[], int N)
{
int top = -1;
int beg;
int end;
int mid;
int lower[20];
int upper[20];
if (N > 1)
{
top++;
lower[top] = 0;
upper[top] = N - 1;
}
while (top != -1)
{
beg = lower[top];
end = upper[top];
top--;
if (beg < end)
{
mid = (beg + end) / 2;
top++;
lower[top] = mid + 1;
upper[top] = end;
top++;
lower[top] = beg;
upper[top] = mid;
}
}
/* Actual merging */
for (int i = 1; i < N; i = i * 2)
{
for (int beg = 0; beg < N; beg = beg + 2 * i)
{
int mid = beg + i - 1;
int end = beg + 2 * i - 1;
if (mid < N - 1)
{
if (end >= N)
end = N - 1;
merge(a, beg, mid, end);
}
}
}
}
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int b[20];
i = beg;
j = mid + 1;
k = beg;
while (i <= mid && j <= end)
{
if (a[i] <= a[j])
{
b[k] = a[i];
i++;
}
else
{
b[k] = a[j];
j++;
}
k++;
}
while (i <= mid)
{
b[k] = a[i];
i++;
k++;
}
while (j <= end)
{
b[k] = a[j];
j++;
k++;
}
for (i = beg; i <= end; i++)
{
a[i] = b[i];
}
}
void main()
{
int a[] = {77, 44, 11, 78, 2, 91, 74, 20, 50, 7};
int N = 10;
int i;
merge_sort(a, N);
for (i = 0; i < N; i++)
{
printf("%d ", a[i]);
}
getch();
}
Output:
2 7 11 20 44 50 74 77 78 91
6 855
struct node
{
int data;
struct node*next;
};
struct node *start=0;
struct node *cn()
{
struct node *k1;
k1=(struct node *)malloc(sizeof(struct node));
printf("Enter a data:");
scanf("%d",&k1->data);
k1->next=k1;
return k1;
}
void is()
{
struct node *h1,*t1;
h1=cn();
if(start==0)
{
start=h1;
}
else
{
t1=start;
while(t1->next!=start)
{
t1=t1->next;
}
h1->next=start;
start=h1;
t1->next=h1;
}
}
void ie()
{
struct node *b1,*k1;
b1=cn();
if(start==0)
{
start=b1;
}
else
{
k1=start;
while(k1->next!=start)
k1=k1->next;
k1->next=b1;
b1->next=start;
}
}
im()
{
}
void ds()
{
struct node *g1;
if(start==0)
{
printf("There is no any node to delete");
}
else
{
g1=start;
while(g1->next!=start)
{
g1=g1->next;
}
g1->next=start->next;
g1=start;
start=start->next;
g1->next=0;
free(g1);
}
}
void de()
{
struct node *a1,*p1;
if(start==0)
{
printf("There is no any node to delete");
}
else
{
a1=start;
while(a1->next->next!=start)
{
a1=a1->next;
}
p1=a1->next;
a1->next=start;
p1->next=0;
free(p1);
}
}
dm()
{
}
void display()
{
struct node *g1;
if(start==0)
{
printf("There is no any node to display.");
}
else
{
g1=start;
while(g1->next!=start)
{
printf("%d ",g1->data);
g1=g1->next;
}
printf("%d",g1->data);
}
}
void main()
{
int ch;
printf("\n1. Insert from start");
printf("\n2. Insert from end");
printf("\n3. Insert from middle");
printf("\n4. Delete from start");
printf("\n5. Delete from end");
printf("\n6. Delete from middle");
printf("\n7. Display");
printf("\n8. Exit");
while(1)
{
printf("\nEnter Your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
is();
break;
}
case 2:
{
ie();
break;
}
case 3:
{
im();
break;
}
case 4:
{
ds();
break;
}
case 5:
{
de();
break;
}
case 6:
{
dm();
break;
}
case 7:
{
display();
break;
}
case 8:
{
exit(0);
}
default:
{
printf("Wrong Input");
}
}
}
getch();
}
6 855
struct node
{
int data;
struct node*next;
};
struct node *start=0;
cn()
{
}
is()
{
}
ie()
{
}
im()
{
}
ds()
{
}
de()
{
}
dm()
{
}
display()
{
}
void main()
{
printf("\n1. Insert from start");
printf("\n2. Insert from end");
printf("\n3. Insert from middle");
printf("\n4. Delete from start");
printf("\n5. Delete from end");
printf("\n6. Delete from middle");
printf("\n7. Display");
printf("\n8. Exit");
while(1)
{
printf("\nEnter Your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
is();
break;
}
case 2:
{
ie();
break;
}
case 3:
{
im();
break;
}
case 4:
{
ds();
break;
}
case 5:
{
de();
break;
}
case 6:
{
dm();
break;
}
case 7:
{
display();
break;
}
case 8:
{
exit(0);
}
default:
{
printf("Wrong Input");
}
}
}
getch();
}
6 855
struct node
{
int data;
struct node *next;
};
struct node *start=0;
struct node *cn()
{
struct node *d1;
d1=(struct node *)malloc(sizeof(struct node));
printf("Enter a data:");
scanf("%d",&d1->data);
d1->next=0;
return d1;
}
void is()
{
struct node *h1;
h1=cn();
if(start==0)
{
start=h1;
}
else
{
h1->next=start;
start=h1;
}
}
void ie()
{
struct node *t1,*h1;
t1=cn();
if(start==0)
{
start=t1;
}
else
{
h1=start;
while(h1->next!=0)
{
h1=h1->next;
}
h1->next=t1;
}
}
im()
{
}
void ds()
{
struct node *s1;
if(start==0)
{
printf("There is no any node to delete");
}
else
{
s1=start;
start=start->next;
s1->next=0;
free(s1);
}
}
void de()
{
struct node *ptr1,*ptr8;
if(start==0)
{
printf("There is no any node to delete");
}
else
{
ptr1=start;
while(ptr1->next->next!=0)
{
ptr1=ptr1->next;
}
ptr8=ptr1->next;
ptr1->next=0;
free(ptr8);
printf("Node deleted successfully");
}
}
dm()
{
}
void display()
{
struct node *p1;
if(start==0)
{
printf("There is no any node to display");
}
else
{
p1=start;
while(p1!=0)
{
printf("%d ",p1->data);
p1=p1->next;
}
}
}
void main()
{
int ch;
printf("\n1. Insert from start.");
printf("\n2. Insert from end");
printf("\n3. Insert from middle");
printf("\n4. Delete from start");
printf("\n5. Delete from end");
printf("\n6. Delete from middle");
printf("\n7. Display");
printf("\n8. Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
is();
break;
}
case 2:
{
ie();
break;
}
case 3:
{
im();
break;
}
case 4:
{
ds();
break;
}
case 5:
{
de();
break;
}
case 6:
{
dm();
break;
}
case 7:
{
display();
break;
}
case 8:
{
exit(0);
}
default:
{
printf("Wrong Input.");
}
}
}
getch();
}
6 855
struct node
{
int data;
struct node *next;
};
struct node *start=0;
struct node *createNode()
{
struct node *p1;
p1=(struct node *)malloc(sizeof(struct node));
printf("Enter a data:");
scanf("%d",&p1->data);
p1->next=0;
return p1;
}
insertStart()
{
struct node *k1;
k1=createNode();
if(start==0)
{
start=k1;
}
else
{
k1->next=start;
start=k1;
}
}
insertEnd()
{
}
insertMiddle()
{
}
deleteStart()
{
struct node *i1;
if(start==0)
{
printf("There is no any node to delete");
}
else
{
i1=start;
start=start->next;
i1->next=0;
free(i1);
printf("Node deleted successfully");
}
}
deleteEnd()
{
}
deleteMiddle()
{
}
display()
{
struct node *h1;
if(start==0)
{
printf("There is no any node to display");
}
else
{
h1=start;
while(h1!=0)
{
printf("%d ",h1->data);
h1=h1->next;
}
}
}
void main()
{
int ch;
printf("\n1. Insert from start");
printf("\n2. Insert from End");
printf("\n3. Insert from Middle");
printf("\n4. Delete from start");
printf("\n5. Delete from end");
printf("\n6. Delete from middle");
printf("\n7. Display");
printf("\n8. Exit");
while(1)
{
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
insertStart();
break;
}
case 2:
{
insertEnd();
break;
}
case 3:
{
insertMiddle();
break;
}
case 4:
{
deleteStart();
break;
}
case 5:
{
deleteEnd();
break;
}
case 6:
{
deleteMiddle();
break;
}
case 7:
{
display();
break;
}
case 8:
{
exit(0);
}
default:
{
printf("Wrong Input");
}
}
}
getch();
}
