Subjects
PRICING
TEST PREP
SIGN IN
Start Free Trial
Mithun P.
Undergraduate student at IIT Madras, Tutor for 4 years.
Tutor Satisfaction Guarantee
Python Programming
TutorMe
Question:

Create a doubly linked list with python, insert, delete and display options

Mithun P.
Answer:

import random as rd from time import time import matplotlib.pyplot as plot class Node: def __init__(self, data, next, prev): self.next = next self.data = data self.prev = prev def print_data(self): print self.data def append(start,end, element, position): if start == None: start = element end = start else: i=end end=element element.prev=i i.next = element return start,end def insert(start, end, N, element, position): if start == None: start = element end = start elif position ==1: element.next = start start.prev = element start = element else: if position<=N/2: i = start for j in range(position - 2): i = i.next else: i = end for j in range(N-position + 1): i = i.prev element.next = i.next element.prev = i i.next = element try: element.next.prev=element except AttributeError: element.next=None end=element return start,end def display(start): i = start while i.next != None: i.print_data() i = i.next i.print_data() """ def display_rev(end): i = end while i.prev != None: i.print_data() i = i.prev i.print_data() """ def delete(start, end, N, position): if N == 1: p=start end=start start.next=None del p else: if position == 1: p = start start = start.next start.prev = None del p elif position == N: p = end end = end.prev end.next = None del p else: if position <= N/2: i = start for j in range(position - 2): i = i.next else: i=end for j in range(N - position + 1): i = i.prev x = i.next.next del i.next i.next = x x.prev = i return start,end x=[] y1=[] y2=[] N=0 for M in range(1000,50000,1000): print M start = None end = None #print '*' for i in range(0,M): obj = Node(rd.randint(1,M), None, None) start,end = append(start,end, obj, "end") #print '**' N=M t11=0 t22=0 for i in range(0,100): obj1 = Node(rd.randint(1,10000), None, None) t1=time() start,end = insert(start, end, N, obj1, rd.randint(1,N)) N+=1 t2=time() t11=t11+t2-t1 t1=time() start,end = delete(start, end, N, rd.randint(1,N)) N-=1 t2=time() t22=t22+t2-t1 x.append(M) y1.append(t11) y2.append(t22) print '***' while N!=0: start,end = delete(start, end, N, 1) N-=1 #print '****' A,=plot.plot(x,y1,'-b') B,=plot.plot(x,y2,'-r') plot.legend([A,B],['Insertion','Deletion']) plot.xlabel('N') plot.ylabel('Execution Time') plot.title('Time Complexity Analysis for 100 Operations') plot.show()

Computer Networking
TutorMe
Question:

To create a simple file transfer protocol (FTP) program called SimpleFTP to transfer files from a Client machine/process to a Server machine/process.

Mithun P.
Answer:

Client #include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> #include <string.h> #include <math.h> #include<unistd.h> #include<stdlib.h> #define BUFFERSIZE 1024 #define SERVERADDR "127.0.0.1" #define CPORT 7000 #define SPORT1 8000 #define SPORT2 8001 int main() { int i,p,j; struct sockaddr_in s_server,s_client; int sock,conn_sock,bytes_received,si_len=sizeof(s_server),bytes_sent,conn_status,size,n; char fsize[BUFFERSIZE],message[BUFFERSIZE],send_buf[BUFFERSIZE],filename[BUFFERSIZE]; FILE *fp; // Creating the client UDP socket sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); if (sock<0) { printf("Socket creation failed.\n "); return 1; } else { printf("Socket creation successful at %d\n ",sock); } // Assigning server IP and port for sending s_server.sin_family =AF_INET; s_server.sin_port=htons(SPORT1); if(!inet_aton(SERVERADDR,&s_server.sin_addr)) { printf("IP network format conversion failed.\n"); return 0; } else { printf("IP network format conversion successful.\n"); } printf("\nType file name :"); scanf("%s",filename); fp=fopen(filename,"r"); fseek(fp,0L,SEEK_END); size=ftell(fp); sprintf(fsize,"%d",size); fseek(fp,0L,SEEK_SET); strcpy(send_buf,"NAME"); strcat(send_buf,filename); bytes_sent = sendto(sock,send_buf,sizeof(send_buf),0,\ (struct sockaddr*)&s_server,si_len); printf("\n%d bytes sent : %s \n",bytes_sent,send_buf); bzero(send_buf,BUFFERSIZE); strcpy(send_buf,"SIZE"); strcat(send_buf,fsize); bytes_sent = sendto(sock,send_buf,sizeof(send_buf),0,\ (struct sockaddr*)&s_server,si_len); printf("\n%d bytes sent : %s \n",bytes_sent,send_buf); bzero(send_buf,BUFFERSIZE); strcpy(send_buf,"CLOSECTRL"); bytes_sent = sendto(sock,send_buf,sizeof(send_buf),0,\ (struct sockaddr*)&s_server,si_len); printf("\n%d bytes sent : %s \n",bytes_sent,send_buf); bzero(send_buf,BUFFERSIZE); close(sock); // Create a TCP connection socket for client conn_sock=socket(AF_INET,SOCK_STREAM,0); if(conn_sock==-1) { printf("Socket creation failed.\n"); close(conn_sock); return 1; } else { printf("Socket creation successful with descriptor %d .\n",conn_sock); } // Requesting for a TCP connection to the server memset(&s_server,0,sizeof(s_server)); s_server.sin_family=AF_INET; inet_aton(SERVERADDR,&s_server.sin_addr); s_server.sin_port=htons(SPORT2); while(connect(conn_sock,(struct sockaddr*)&s_server,si_len)!=0) { } printf("Connected to the server.\n"); j=0; while(j<size) { bzero(send_buf,BUFFERSIZE); n=fread(send_buf,1,10,fp); i=0; p=n; while(p>0) { bytes_sent = send (conn_sock, &send_buf[i], p, 0); printf ("%d bytes sent:%s\n", bytes_sent, send_buf); p=p-bytes_sent; i=i+bytes_sent; j=j+bytes_sent; } } fclose(fp); close(conn_sock); return 0; } Server #include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> #include <string.h> #include <math.h> #include<unistd.h> #include<stdlib.h> #define BUFFERSIZE 1024 #define SERVERADDR "127.0.0.1" #define CPORT 7000 #define SPORT1 8000 #define SPORT2 8001 int main() { struct sockaddr_in s1,s2,s_client; int sock,req_sock,conn_sock,bytes_received,si_len=sizeof(s_client),bytes_sent,conn_status,bind_status,size,siz,n; char fsize[BUFFERSIZE],message[BUFFERSIZE],recv_buf[BUFFERSIZE],filename[BUFFERSIZE]; FILE *fp; // Creating the socket sock=socket (AF_INET,SOCK_DGRAM,IPPROTO_UDP); if(sock<0) { printf(" Socket creation failed .\n "); } else { printf("Socket created with descriptor %d \n",sock); } // Initializing and binding the socket s1.sin_family=AF_INET; s1.sin_port=htons(SPORT1); s1.sin_addr.s_addr=htonl(INADDR_ANY); bind_status=bind(sock,(struct sockaddr*)&s1,\ sizeof(s1)); if(bind_status<0) { printf("Binding socket failed .\n"); } else { printf(" Binding socket successful.\n"); } // Creating the socket req_sock=socket (AF_INET,SOCK_STREAM,0); if(req_sock<0) { printf(" Socket creation failed .\n "); } else { printf("Socket created with descriptor %d \n",sock); } // Initializing and binding the socket s2.sin_family=AF_INET; s2.sin_port=htons(SPORT2); s2.sin_addr.s_addr=htonl(INADDR_ANY); bind_status=bind(req_sock,(struct sockaddr*)&s2,\ sizeof(s2)); if(bind_status<0) { printf("Binding socket failed .\n"); } else { printf(" Binding socket successful.\n"); } int j,len; bzero(filename,BUFFERSIZE); while(1) { // Server wait for a packet bzero(recv_buf,BUFFERSIZE); recv_buf[0]='\0'; bytes_received=recvfrom(sock,recv_buf,sizeof(recv_buf),\ 0,(struct sockaddr*)&s_client,&si_len); printf("Data received from %s :%d \n",inet_ntoa(s_client.sin_addr),\ s_client.sin_port); printf("%d bytes of data received : %s \n",bytes_received,recv_buf); len=strlen(recv_buf); if(strstr(recv_buf,"NAME")) { for(j=4;j<len;j++) recv_buf[j-4]=recv_buf[j]; recv_buf[len-4]='\0'; printf("\n %s \n",recv_buf); strcpy(filename,recv_buf); } else if(strstr(recv_buf,"SIZE")) { for(j=4;j<len;j++) recv_buf[j-4]=recv_buf[j]; recv_buf[len-4]='\0'; printf("\n %s \n",recv_buf); strcpy(fsize,recv_buf); size=atoi(fsize); } else if(strstr(recv_buf,"CLOSE")) break; } bzero(recv_buf,BUFFERSIZE); close(sock); // Listen for TCP connection requests printf("Listening for connection requests.\n"); listen(req_sock,5); conn_sock=accept(req_sock,(struct sockaddr*)&s_client,&si_len); if(conn_sock==-1) { printf("Socket creation for client failed.\n"); close(conn_sock); close(req_sock); return 1; } else { printf("Socket created for client with descriptor %d.\n",conn_sock); } printf("IIST\n"); printf("%s\n",filename); fp=fopen(filename,"w"); siz=0; while(siz<size) { bzero(recv_buf,BUFFERSIZE); // Receiving data from client through the connection socket bytes_received=recv(conn_sock,recv_buf,BUFFERSIZE,0); //recv_buf[bytes_received]='\0'; printf("%d bytes received : %s \n",bytes_received,recv_buf); fwrite(recv_buf,bytes_received,1,fp); siz=siz+bytes_received; } fclose(fp); close(req_sock); close(conn_sock); return 0; }

C Programming
TutorMe
Question:

Write a C program to create a doubly linked list of size 15 with position number as data, in which user can insert new elements at any desired position.

Mithun P.
Answer:

#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 15 struct NodeType { int data; struct NodeType* next; struct NodeType* prev; }; struct NodeType new; struct NodeType *newp,*after,*add,*newpp,*head=NULL,*tail=NULL; void Append() { int i; for(i=1;i<=N;i++) { newp=malloc(sizeof(new)); if(head==NULL) { tail=head=newp; newp->prev=NULL; } else { tail->next=newp; newp->prev=tail; tail=newp; } newp->data=i; newp->next=NULL; } // Printing the elements in the double linked list newp=head; while(1) { printf("%d\n",newp->data); if(newp->next == NULL) break; newp=newp->next; } } void Insert(int pos, int value) { if(pos>1) { after=head; int k; for(k=1; k<pos-1;k++) { if(after->next==NULL) { break; } else { after=after->next; } } add=malloc(sizeof(new)); add->data=value; add->next=NULL; add->prev=NULL; if(after==NULL || add==NULL) { exit(0); printf("\n NULL \n"); } if(after->next==NULL) tail=add; else { add->next=after->next; after->next->prev=add; } add->prev=after; after->next=add; } else if(pos==1) { add=malloc(sizeof(new)); add->data=value; add->next=head; add->prev=NULL; head->prev=add; head=add; } // Printing the elements in the double linked list printf("\n"); newp=head; while(1) { printf("%d\n",newp->data); if(newp->next == NULL) break; newp=newp->next; } } int main() { int pos,data; Append(); while(1) { printf("\n Enter position\n"); scanf("%d",&pos); printf("\n Enter data\n"); scanf("%d",&data); Insert(pos,data); } return 0; }

Send a message explaining your
needs and Mithun will reply soon.
Contact Mithun
Ready now? Request a lesson.
Start Session
FAQs
What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Session" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.