2014年10月8日 星期三

[C] Linked List 實作

#include <stdio.h>
#include <stdlib.h>

typedef struct IDlist
{
     int userId;
     struct IDlist* next;
}tIDlist;


typedef struct IDhead
{
     int count;
     tIDlist *rear;
     tIDlist *front;
     
}tIDhead;


void printAll();
void addToHead(tIDhead*,int);
void addToTail(tIDhead*,int);
void removeFirst(tIDhead*);
void removeLast(tIDhead*);
void removeNth(tIDhead*,int);
void printAll();


tIDhead *reg;
tIDlist *previous;
int main (void)
{
     
     
     reg=(tIDhead *)malloc(sizeof(tIDhead));
     
     previous=(tIDlist *)malloc(sizeof(tIDlist));
     previous=NULL;
     
     int menu;
     int IDnumber;
     int nth;
     reg->count=0;
     reg->front=NULL;
     reg->rear=NULL;
     
     while (1)
     {
          printf("Which operation you want to operate? \n");
          
          
          printf("  1. insert one user ID to the head\n");
          printf("  2. insert one user ID to the tail \n");
          printf("  3. remove the first user ID\n");
          printf("  4. remove the last user ID\n");
          printf("  5. remove the n-th user ID\n");
          
          scanf("%d",&menu);
          //if 1.
          if (menu == 1)
          {
               
               printf("Please enter the user ID number:");
               scanf("%d",&IDnumber);
               addToHead(reg,IDnumber);
               
          }
          
          //if 2.
          else if (menu == 2)
          {
               printf("Please enter the user ID number:");
               scanf("%d",&IDnumber);
               addToTail(reg,IDnumber);
             //  printf("%d",reg->front->userId);
          }
          //if 3.
          else if (menu == 3)
          {
                  removeFirst(reg);
          }
          //if 4.
          else if (menu == 4)
          {
               previous=reg->front;
               removeLast(reg);
          }
          //if 5.
          else if (menu == 5)
          {
               printf("what number u want remove?\n");
               scanf("%d",&nth);
               removeNth(reg,nth);
          }
          else
          {
               
          }
          
          
          printAll();
          
     }
     system("pause");
     return 0;
     
}
void addToHead(tIDhead* reg,int Idnumber){
     tIDlist *tmp;
     
     tmp=(tIDlist *)malloc(sizeof(tIDlist));
     tmp->userId=Idnumber;
     
     if(reg->count==0){
          reg->front=tmp;
          
     }
     else{
          tmp->next=reg->front;
          reg->front=tmp;
     }
     
     reg->count++;
}
void addToTail(tIDhead* reg,int Idnumber){
     tIDlist *tmp;
     
     tmp=(tIDlist *)malloc(sizeof(tIDlist));
     tmp->userId=Idnumber;
     
     
     if(reg->count==0){
          reg->front=tmp;
          
     }
     else{
          reg->rear->next=tmp;
     }
     
     reg->rear=tmp;
     
     tmp->next=NULL;
     
     reg->count++;
     
}
void printAll(){
     
     tIDlist *tmp;
     
     tmp=(tIDlist *)malloc(sizeof(tIDlist));
     tmp->next=NULL;

     tmp=reg->front;
     int i=0;
     while (i<reg->count) {
          if (tmp==NULL) {
               break;
          }
          printf("%d.%d\n",i+1,tmp->userId);
          
          tmp=tmp->next;
          
          i++;
          
     }
     
}
void removeFirst(tIDhead* reg){
     reg->front=reg->front->next;
     reg->count--;
}
void removeLast(tIDhead* reg){
     
     if (previous->next->next==NULL) {
          //previous finded

          reg->rear=previous;
          reg->count--;
     }
     else{
          previous=previous->next;
          removeLast(reg);
     }
     
}
void removeNth(tIDhead* reg,int nth){
     int count=1;
     previous=reg->front;
     if (nth==1) {
          removeFirst(reg);
     }
     else if(nth==reg->count){
          previous=reg->front;
          removeLast(reg);
     }
     else{
          while (count<nth) {
               if (count+1==nth) {
                    previous->next=previous->next->next;
                    reg->count--;
                    break;
               }
               previous=previous->next;
               count++;
          }
     }
}


有時間再來打註解,打得蠻草的
有些是作業的要求(禁止使用迴圈,防呆功能)
才寫這麼亂

2014年10月2日 星期四

[ C ] memset ,初始化陣列

如要初始化陣列,比方說元素全部設為0




char  IamArray[10][10];

memset( IamArray, 0 ,sizeof(IamArray));



#note : 大小給程式自己抓就好