Ejemplo n.º 1
0
// 主函数-用于测试
int main() {
    // 初始化链表
    linkList list = initLinkList();
    // 插入三个元素
    inserts(&list, 333);
    inserts(&list, 222);
    inserts(&list, 2222);

    // 遍历链表
    lists(&list);

    // 求列表中元素个数
    printf("size = %d\n", list.size);


    // 获取列表中的元素
    printf("value = %d\n", getElement(&list, 0));
    printf("value = %d\n", getElement(&list, 1));
    printf("value = %d\n", getElement(&list, 2));

    // 删除列表中的元素
    deletes(&list, 2);

    printf("value = %d\n", getElement(&list, 0));
    printf("value = %d\n", getElement(&list, 1));
    // 查看最终列表中元素个数
    printf("size = %d\n", list.size);
}
Ejemplo n.º 2
0
/*-----------------------------------------------------------------------*/
void main()
{
int a;
clrscr();
inserts(a);
inserts(a);
inserts(a);
inserts(a);
inserts(a);
inserts(a);
display();
deletes();
display();
deletes();
display();
deletes();
display();
inserts(a);
inserts(a);
display();
getch();
}
Ejemplo n.º 3
0
main()
{
	struct quefr *que;
	int x,cord;
	void Outlin(struct quefr qq);
	void creat(struct quefr *qe);
	void insert(struct quefr *p,ElemType x);
	ElemType deletes(struct quefr *qe);
	do
	{
		printf("\n");
		printf("           主菜单           \n");
		printf("      1    建立链表队列     \n");
		printf("      2    入队一个元素     \n");
		printf("      3    出队一个元素     \n");
		printf("      4    结束程序运行     \n");
		printf("-------------------------------\n");
		printf("请输入您的选择(1, 2, 3, 4) ");
		scanf("%d",&cord);
		switch(cord)
		{
			case 1:
				{
					que=(struct quefr *)malloc(sizeof(struct quefr));
					creat(que);
					Outlin(*que);
				}break;
			case 2:
				{
					printf("x=?");
					scanf("%d",&x);
					insert(que,x);
					Outlin(*que);
				}break;
			case 3:
				{
					printf("x=%d\n",deletes(que));
					Outlin(*que);
				}break;
			case 4:
				{
					exit (0);
				}
		}
	}while (cord<=4);
}
main(void)
{
   int ch,e,pos,temp=0,ct=1,end=FALSE;
   list = NULL;
   LINK *x;
   LINK *p;
           // Creating a Header Node (Info part contains number of elements) //
           // List points to Header Node //
           p = getnode();
           p->info = 0;
           p->right = p;
           p->left = p;
           list = p;          
   
   while(1)
   {
          system("cls"); 
          printf("\n\n\nHeader Doubly Circular Linked List Implementation Using Dynamic Variables\n\n");
          printf("1. Insert Elements\n");
          printf("2. Delete Elements\n");
          printf("3. Display Elements\n");
          printf("4. Exit\n\n");
          printf("Enter Choice:");
          scanf("%d",&ch);
          switch(ch)
          {
              case 1:
                     printf("\nEnter Element:");
                     scanf("%d",&e);
                     if(emptys())
                                 pushs(e);
                     else
                     {
                                 printf("\nEnter Element Position:");
                                 scanf("%d",&pos);
                                 if(pos>count+1 || pos<=0)
                                                printf("\nIncorrect Position!");
                                 else
                                                inserts(e,pos); 
                                 fflush(stdin);
                                 getchar();                                
                     }           
                                  
                     break;
              case 2:
                     ct=1;
                     if(emptys())
                     {
                                printf("\nUnderflow!");
                                fflush(stdin);
                                getchar();  
                     }
                     else
                     {
                        printf("\nEnter Element to Delete:");
                        scanf("%d",&e);
                        x=list;               
                        while(end == FALSE)   // only 1 condition required since LL always contains Header Node 
                        {
                                  x = x->right;
                                  if(x->info==e)
                                  {
                                          if(x!=list->right)    // list.next is first node 
                                             temp = deletes(x); // Previous Node //
                                          else
                                             temp=pops();
                                          printf("\n%d Deleted at Position %d !",temp, ct);                                                                             
                                  }                              
                                  if(x->right==list)
                                  {                
                                     x=list;                                  
                                     end = TRUE;
                                  }
                                  ct++;
                        }  
                        end = FALSE;
                        if(x==list && temp==0)
                               printf("\nElement Does Not Exist");
                        fflush(stdin);
                        getchar();      
                        
                     }          
                     
                     break;
              case 3:
                     x = list->right;
                     if(!emptys())
                                       printf("\nLinked List(Number of Nodes:%d):",list->info);
                     while(end==FALSE && !emptys())
                     {
                            printf("%d->",x->info);                              
                            x = x->right;
                            if(x==list)
                                       end = TRUE;
                     }
                     end = FALSE;
                     if(emptys())
                            printf("\nLinked List Empty!");    
                            fflush(stdin);
                            getchar();                                           
                     
                     break;
              case 4:
                     exit(0);        // returning 0 means successful 
          }
          
   }                 
}
Ejemplo n.º 5
0
int main(int argc, char *argv[]){
    
    struct sockaddr_in server;
    int new_sock, server_sock = -1;
    int addrlen;
    char command[128], buf[128];
    
    if (argc != 2) {
        (void) fprintf(stderr,"usage: %s port# \n",argv[0]);
        exit(1);
    }
    
    portnum = atoi(argv[1]);
    
    // Create a Server Socket
    if((server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
        perror("socket");
        exit(1);
    }
    
    memset((void*)&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons((u_short)atoi(argv[1]));
    
    if(bind(server_sock, (struct sockaddr *)&server, sizeof server) < 0) {
        perror("bind");
        exit(1);
    }
    
    if(listen(server_sock, SOMAXCONN) < 0) {
        perror("listen");
        exit(1);
    }
    
    
    
    //
    //
    //
    while(1){
        // Input from the keyboard
        memset(command, 0x00, sizeof command);
        
        addrlen = sizeof server;
        new_sock = accept(server_sock,
                          (struct sockaddr *)&server,
                          (socklen_t *)&addrlen);
        
        if (new_sock < 0) {
            perror("accept");
            exit(1);
        }
        
        printf("connection : Host IP %s, Port %d, socket %d\n",
               inet_ntoa(server.sin_addr), ntohs(server.sin_port), new_sock);
        
        read(new_sock, command, sizeof command);	// read user command
        printf("%s", command);
        
        if(!strncmp(command, "@quit", 5)){			// quit
            memset(buf, 0x00, sizeof(buf));
            
            strcat(buf, "@delete ");
            strcat(buf, inet_ntoa(server.sin_addr));
            strcat(buf, " ");
            strcat(buf, toArray(ntohs(server.sin_port)));
            
            struct node* delNode = find(inet_ntoa(server.sin_addr), ntohs(server.sin_port));
            int roomNum = delNode->room;
            delNode->room = 0;
            
            current = head;
            while(current != NULL){
                if(current->room == roomNum)
                    connectToClient(current->IP, toArray(current->port), buf);
                current = current->next;
            }
        }
        else if(!strncmp(command, "@exit", 5)){		// exit
            memset(buf, 0x00, sizeof(buf));
            
            strcat(buf, "@delete ");
            strcat(buf, inet_ntoa(server.sin_addr));
            strcat(buf, " ");
            strcat(buf, toArray(ntohs(server.sin_port)));
            strcat(buf, "\n");
            
            int roomNum = find(inet_ntoa(server.sin_addr), ntohs(server.sin_port))->room;
            deletes(inet_ntoa(server.sin_addr), ntohs(server.sin_port));
            if(roomNum != 0){
                current = head;
                while(current != NULL){
                    if(current->room == roomNum)
                        connectToClient(current->IP, toArray(current->port), buf);
                    current = current->next;
                }
            }
        }
        else if(!strncmp(command, "@connect", 8)){	// connect
            insertFirst(inet_ntoa(server.sin_addr), ntohs(server.sin_port), &command[9], 0);
            printf("%s is connected\n", &command[9]);
            
            if (write(new_sock, "connection ok", strlen("connection ok")) < 0) {
                perror("write");
                exit(1);
            }
        }
        else if(!strncmp(command, "@getlist", 8)){		// user & room list
            memset(buf, 0x00, sizeof(buf));
            for(int i=0; i<room_size; i++){
                if(i == 0){
                    strcat(buf, "\n==========waiting room ");
                    strcat(buf, "==========\n");
                }
                else{
                    strcat(buf, "==========room# ");
                    strcat(buf, toArray(i));
                    strcat(buf, "==========\n");
                }
                
                current = head;
                while(current != NULL){
                    if(current->room == i){
                        strcat(buf, current->IP);
                        strcat(buf, " ");
                        strcat(buf, toArray(current->port));
                        strcat(buf, " ");
                        strcat(buf, current->ID);
                        strcat(buf, "\n");
                    }
                    current = current->next;
                }
            }
            
            if (write(new_sock, buf, strlen(buf)) < 0) {
                perror("write");
                exit(1);
            }
        }
        else if(!strncmp(command, "@mkroom", 7)){	// mkroom
            room_size++;
            memset(buf, 0x00, sizeof(buf));
            strcat(buf, "room ");
            strcat(buf, toArray(room_size));
            strcat(buf, " is created\n");
            
            if (write(new_sock, buf, strlen(buf)) < 0) {
                perror("write");
                exit(1);
            }
        }
        else if(!strncmp(command, "@join", 5)){		// join
            int roomNum = atoi(&command[6]);
            
            memset(buf, 0x00, sizeof(buf));
            strcat(buf, "@add ");
            strcat(buf, inet_ntoa(server.sin_addr));
            strcat(buf, " ");
            strcat(buf, toArray(ntohs(server.sin_port)));
            strcat(buf, "\n");
            
            char join[1024];
            memset(join, 0x00, sizeof(join));
            strcat(join, "@join\n");
            
            int i = 0;
            current = head;
            while(current != NULL){
                if(strcmp(toArray(ntohs(server.sin_port)), toArray(current->port)) && current->room == roomNum){
                    connectToClient(current->IP, toArray(current->port), buf);
                    strcat(join, current->IP);
                    strcat(join, " ");
                    strcat(join, toArray(current->port));
                    strcat(join, " ");
                    strcat(join, current->ID);
                    strcat(join, "\n");
                }
                current = current->next;
            }
            
            if (write(new_sock, join, strlen(join)) < 0) {
                perror("write");
                exit(1);
            }
            find(inet_ntoa(server.sin_addr), ntohs(server.sin_port))->room = atoi(&command[6]);
            printf("room to %d\n", atoi(&command[6]));
            printf("room to %d\n", atoi(&command[6]));
        }
    }
}// main
Ejemplo n.º 6
0
main()
{
	int x,y,cord;
	void outlin(struct node *h);
	void create();
	void insert(struct node *h,int x,int y);
	void deletes(struct node *h,int x);
	struct node *MaxCompare(struct node *h);
	struct node *MinCompare(struct node *h);
	int delIterance(struct node *h);
	void batchInsert(struct node *h,int x);
	void batchDelete(struct node *h,int x,int y);
	void Cz(struct node*  h);
	void Xg(struct node * h);
	printf("建立链表,输入-999完成链表: \n");
	create();
	i=j;
	outlin(head);
	do{
		printf("\n        主菜单        \n");
		printf("     1   插入一个元素   \n");
		printf("     2   删除一个元素   \n");
		printf("     3   升序排序       \n");
		printf("     4   降序排序       \n");
		printf("     5   查找元素       \n");
		printf("     6   修改元素       \n");
		printf("     7   删除重复元素   \n");
		printf("     8   批量加入元素   \n");
		printf("     9   批量删除元素   \n");
		printf("     0   结束程序运行   \n");
		printf("-----------------------------------------\n");
		printf(" 请输入您的选择(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) ");
		scanf("%d",&cord);
		switch(cord)
		{
			case 1:
			{
				printf("请输入插入的位置 i: ");
				scanf("%d",&x);
				printf("请输入插入的数据 y: ");
				scanf("%d",&y);
				insert(head,x,y);
				i=j;
				outlin(head);
			}break;
			case 2:
			{
				printf("x=?");
				scanf("%d",&x);
				deletes(head,x);
				i=j;
				outlin(head);
			}break;
			case 3:
			{
				printf("链表由大到小是");
				s=MaxCompare(head);
				j=i;
				outlin(s);
				//outlin(head);
			}break;
			case 4:
			{
				printf("链表由大到小是");
				s=MinCompare(head);
				j=i;
				outlin(s);
			}break;
			case 5:
			{
				Cz(head);
				outlin(head);
			}break;
			case 6:
			{
				Xg(head);
				outlin(head);
			}break;
			case 7:
			{
				k=delIterance(head);
				i=i-k;
				j=i;
				outlin(head);
			}break;
			case 8:
			{
				printf("请输入插入的位置 i: ");
				scanf("%d",&x);
				batchInsert(head,x);
				i=j;
				outlin(head);
			}break;
			case 9:
			{
				printf("请输入删除的起始位置 i: ");
				scanf("%d",&x);
				printf("请输入删除的结束位置 y: ");
				scanf("%d",&y);
				batchDelete(head,x,y);
				i=j;
				outlin(head);
			}break;
			case 0:
			{
				exit(0);
			}break;
		}
	}while(cord<=9&&cord>=0);
}
Ejemplo n.º 7
0
main(void)
{
   int ch,e,pos,temp=0,ct=1;
   LINK *x;
   LINK *y;
   list=NULL;
   while(1)
   {
          system("cls"); 
          printf("\n\n\nDoubly Linked List Implementation Using Dynamic Variables\n\n");
          printf("1. Insert Elements\n");
          printf("2. Delete Elements\n");
          printf("3. Display Elements\n");
          printf("4. Exit\n\n");
          printf("Enter Choice:");
          scanf("%d",&ch);
          switch(ch)
          {
              case 1:
                     printf("\nEnter Element:");
                     scanf("%d",&e);
                     if(emptys())
                                 pushs(e);
                     else
                     {
                                 printf("\nEnter Element Position:");
                                 scanf("%d",&pos);
                                 if(pos>count+1 || pos<=0)
                                                printf("\nIncorrect Position!");
                                 else
                                                inserts(e,pos); 
                                 fflush(stdin);
                                 getchar();                                
                     }           
                                  
                     break;
             case 2:
                     ct=1;
                     if(emptys())
                     {
                                printf("\nUnderflow!");
                                fflush(stdin);
                                getchar();  
                     }
                     else
                     {
                        printf("\nEnter Element to Delete:");
                        scanf("%d",&e);
                        x=list;
                        while(x!=NULL)
                        {
                                  if(x->info==e)
                                  {
                                          if(x!=list)
                                             temp = deletes(x); // Current Node //
                                          else
                                             temp = pops();
                                          printf("\n%d Deleted at Position %d !",temp, ct);                                                                            
                                  }
                                  x=x->right;
                                  ct++;
                        }  
                        if(x==NULL && temp==0)
                               printf("\nElement Does Not Exist");
                        fflush(stdin);
                        getchar();     
                        
                     }          
                     
                     break; 
              case 3:
                     x = list;
                     if(list!=NULL)
                                       printf("\nLinked List:");
                     while(x!=NULL)
                     {
                            printf("%d->",x->info);  
                            x = x->right;
                     }
                     if(list==NULL)
                            printf("\nLinked List Empty!");    
                     fflush(stdin);
                     getchar();                                           
                     
                     break;
              case 4:
                     exit(0);        // returning 0 means successful 
          }
          
   }                 
}
Ejemplo n.º 8
0
 int main()
 {
	 deletes();
	 return ;
 }
Ejemplo n.º 9
0
int main(int argc, char **argv) {
    // 매개변수 1:자기포트 2:서버IP 3:서버포트 4:자기ID
    int tcpServ_sock;
    
    struct sockaddr_in tcpServer_addr;
    struct sockaddr_in newTcp_addr;
    
    fd_set reads, temps;
    int fd_max;
    
    char command[1024];
    
    if(argc != 5){
        printf("Usage : %s <myport> <serverIP> <serverport> <myID>\n", argv[0]);
        exit(1);
    }
    
    display();
    
    if ((tcpServ_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }// creating socket descriptor for tcp
    
    memset((void *) &tcpServer_addr, 0, sizeof (tcpServer_addr));
    tcpServer_addr.sin_family = AF_INET;
    tcpServer_addr.sin_addr.s_addr = INADDR_ANY;
    tcpServer_addr.sin_port = htons((u_short)atoi(argv[1])); // portnum
    fd_max = 10;
    
    if (bind(tcpServ_sock, (struct sockaddr *)&tcpServer_addr, sizeof (tcpServer_addr)) < 0) {
        perror("bind error");
        exit(1);
    } // bind
    
    if (listen(tcpServ_sock, SOMAXCONN) < 0) {
        perror("listen");
        exit(1);
    } // wait connect
    
    // 여기까지 기본 소켓세팅
    hostIP = argv[2]; // ip
    hostport = argv[3]; // port numbere
    myID = argv[4];
    char buf[1024];
    strcat(buf, "@connect ");
    strcat(buf, myID);
    //peertcpSockets = connectToServer(buf);
    peertcpSockets = connectToServer(buf);
    
    FD_ZERO(&reads);
    FD_SET(fileno(stdin), &reads);
    FD_SET(tcpServ_sock, &reads);
    FD_SET(peertcpSockets, &reads);
    while(1){
        int nfound;
        temps = reads;
        nfound = select(fd_max+1, &temps, 0, 0, NULL);
        // nfound?
        if(FD_ISSET(fileno(stdin), &temps)) {
            // Input from the keyboard
            char sendhttp[1024];
            memset(sendhttp, NULL, sizeof(sendhttp));
            
            
            fgets(command, sizeof (command), stdin);
            
            if( command[0] == '@' ){
                // send to server
                if(command[1] == 'e'){ // when exit
                    head = NULL;
                    peertcpSockets = connectToServer(command);
                    close(peertcpSocket);
                    exit(1);
                }
                peertcpSockets = connectToServer(command);
                FD_SET(peertcpSockets, &reads);
                // exit했을때는 나에게 들어있는 유저목록 다 지워야한다.
            }else{
                node* cnode = head;
                char sendmsg[1024];
                strcat(sendmsg, myID);
                strcat(sendmsg, ") ");
                strcat(sendmsg, command);
                
                while(cnode != NULL){
                    peertcpSockets = connectToClient(head->IP, toArray(head->port), sendmsg);
                    close(peertcpSockets);
                    cnode = cnode->next;
                } // 유저에게 메시지 전송
                close(peertcpSocket);
            }
            fflush(stdin);
        }else if(FD_ISSET(tcpServ_sock, &temps)){
            int addrlen;
            addrlen = sizeof(newTcp_addr);
            peertcpSockets = accept(tcpServ_sock, (struct sockaddr *)&newTcp_addr, (unsigned int*)&addrlen);
            //printf("connection from host %s, port %d, socket %d\n\n",
            //       inet_ntoa(newTcp_addr.sin_addr), ntohs(newTcp_addr.sin_port), peertcpSocket);
            if (peertcpSockets < 0) {
                perror("accept");
                exit(1);
            }
            FD_SET(peertcpSockets, &reads);
            // make connection
        }else if(FD_ISSET(peertcpSockets, &temps)){ // 서버에서 응답받은 내용
            char buf[1024];
            memset(buf, NULL, sizeof(buf));
            read(peertcpSockets, buf, sizeof(buf));
            char tbuf[1024];
            strcpy(tbuf, buf);
            
            char* inif = strtok(tbuf, "\n");
            
            if(!strcmp(touppers(inif),"JOIN")){
                char* temp;
                temp = strtok(buf, "\n");
                printf("%s\n", temp); // 입력받은 첫줄 출력
                
                while((temp = strtok(NULL, "\n")) != NULL){
                    char* tempIP = strtok(temp, " ");
                    char* tempPort = strtok(NULL, " ");
                    int tport = atoi(tempPort);
                    char* tempID = strtok(NULL, " ");
                    insertFirst(tempIP, tport, tempID, 0);
                }
                close(peertcpSockets);
            }else if(!strcmp(touppers(inif),"INVITE OK")){
                printf("%s\n", inif);
                char* temp;
                temp = strtok(NULL, "\n");
                char* tempIP = strtok(temp, " ");
                char* tempPort = strtok(NULL, " ");
                char* tempID = strtok(NULL, " ");
                
                printf("invite %s\n", tempID);
                int tport = atoi(tempPort);
                insertFirst(tempIP, tport, tempID, 0);
                close(peertcpSockets);
                // invite에 성공했다면 그사람을 저장한다.
            }else if(!strcmp(touppers(inif),"@ADD")){
                printf("%s\n", inif);
                char* tempIP = strtok(NULL, " ");
                char* tempPort = strtok(NULL, " ");
                char* tempID = strtok(NULL, " ");
                
                int tport = atoi(tempPort);
                insertFirst(tempIP, tport, tempID, 0);
                close(peertcpSockets);
                // close 소켓 서버에서?유저에서?
            }else if(!strcmp(touppers(inif),"@DELETE")){
                printf("%s\n", inif);
                char* tempIP = strtok(NULL, " ");
                char* tempPort = strtok(NULL, " ");
                int tport = atoi(tempPort);
                deletes(tempIP, tport);
                close(peertcpSockets);
            }else{
                printf("%s\n",buf); // print receive msg
                close(peertcpSockets);
                //FD_CLR(peertcpSockets, &reads);
            }
            buf[0] = '\0';
            printf("> ");
        }// receive
        
        fflush(stdout);
    }
}//main End