데이터 구조 - 선형 표 (단일 체인 표 C 언어 구현)

15982 단어 데이터 구조
//      

#include "stdlib.h"
#include "stdio.h"




void printLinkList(struct node *h);

//       
typedef struct node {
    int data;       //   
    struct node *next;   //   
}Lnode,*LinkList;



//         
LinkList createLinkList(int n)
{
    struct node *h, *p, *q;
    int i;
    p=(struct node *)malloc(sizeof(struct node));
    h=p;
    q=p;
    for(i=0;istruct node *)malloc(sizeof(struct node));

        printf("    %d     :", i+1);
        scanf("%d", &(p->data));

        q->next=p;
        q=p;
    }
    q->next=NULL;
    return h;
}

//         
//                 ,            ,    
LinkList FindElem(LinkList h){
    LinkList p = h->next;
    int data;
    printf("         :
"
); scanf("%d",&data); int i = 1; int flag = 0; while(p!= NULL){ if (data == p->data){ printf(" %d
"
,data); flag = 1; break; } p = p->next; i++; } if (flag == 1){ printf(" %d
"
,i); } else{ printf("
"
); } return h; } // LinkList InsertList(LinkList h){ LinkList p = h->next; LinkList q; q = (LinkList)malloc(sizeof(Lnode));// int data,n; printf("
"
); scanf("%d",&data); printf("
"
); scanf("%d",&n); int j = 1; if (n == 1){ q->next = p->next; q->data = data; p->next = q; return p; } while(p!= NULL && n > j+1){ p = p->next; j++; } printf("%d
"
,p->data); if (p ==NULL){ printf(" "); return NULL; } q->next = p->next; q->data = data; p->next = q; return h; } LinkList Insert_Linklist(LinkList h) // , { LinkList p, q, s; int x; printf(" :
"
); scanf("%d",&x); q = h; p = h->next; while(p) { if(p->data <= x) { q = p; p = p->next; } else break; } s = (LinkList)malloc(sizeof(Lnode)); if(!s) return 0; s->data = x; q->next = s; s->next = p; return h; } // 2 LinkList InsertList2(LinkList h){ LinkList p = h->next; LinkList q; printf(" :
"
); int data,n; scanf("%d",&data); printf(" :
"
); scanf("%d",&n); if(n == 1){// q = (LinkList)malloc(sizeof(Lnode));// q->data = data; q->next = p; h->next = q; return h;// } else{ // n-1 int i = 1; int flag = 0; while(p!= NULL){ if (n == i+1){ flag = 1; break; } p = p->next; i++; } printf("%d
"
,p->data); if (p == NULL){ printf("
"
); return NULL; } else { q = (struct node *)malloc(sizeof(struct node)); q->data = data; q->next = p->next; p->next = q; return h; } return h; } return h; } // X LinkList DeleateList(LinkList h){ LinkList p = h->next; LinkList q = h; printf("
"
); int data; scanf("%d",&data); while(p != NULL){ if(p->data == data){ q->next = p->next; free(p);// p p = q->next;//p }else{ q = p; p = p->next; } } return h; } // LinkList DelEven_LinkList(LinkList h){ LinkList p, q; q = h; p = h->next; while(p) { if(p->data % 2 == 0) { q->next = p->next; free(p); p = q->next; } else { q = p; p = p->next; } } return h; } // LinkList ClearList(LinkList h){ LinkList p = h->next; LinkList q; while(p != NULL){ q = p; p = p->next; free(p); } h->next = NULL; return h; } // LinkList Reversed(LinkList h){ LinkList q=h->next; LinkList p=NULL; LinkList pNext; while (NULL != q) { pNext=q->next; q->next = p; p=q; q=pNext; } h->next = p; return h;// } // , void printLinkList(struct node *h) { LinkList p=h->next; int i = 0; while(p!=NULL) { printf("%d ", p->data); p=p->next; i++; } printf("
"
); printf(" :%d
"
,i); } void menu(){ printf("1:
"
); printf("2:
"
); printf("3:
"
); printf("4:
"
); printf("5:
"
); printf("6:
"
); printf("7:
"
); printf("0:
"
); } int main() { LinkList head; int m; while(1){ menu(); printf(" :
"
); scanf("%d",&m); switch (m) { case 1: int n; //struct node *head; printf(" :"); scanf("%d", &n); head=createLinkList(n);// printf("
。。。
"
); printf("

"
); printLinkList(head); break; case 2: // //head = InsertList(head); head = Insert_Linklist(head);// printf(" :"); printLinkList(head); break; case 3: // head = DeleateList(head); printLinkList(head); break; case 4: // FindElem(head); break; case 5: // Reversed(head); printf(" :
"
); printLinkList(head); break; case 6: // ClearList(head); printf(" :
"
); printLinkList(head); break; case 7: DelEven_LinkList(head); printLinkList(head); break; case 0: return 0; break; default: break; } } }

좋은 웹페이지 즐겨찾기