1.1 ¶¯Ì¬Á´±íµÄʵÏÖ
ÒÔÎÞÍ·Á´±íµÄÐÎʽʵÏÖ
struct Node
{
char name[15];
int num;
char sex[3];
struct Node*next;
};
1.1.1 ´´½¨Á´±í
//µÚÒ»¸ö½Úµã
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
scanf(p);
p->next = NULL
//µÚ¶þ¸ö½Úµã
struct Node *pf = (struct Node*)malloc(sizeof(struct Node));
scanf(pf);
pf->next = NULL
p->next = pf;
//µÚÈý¸ö½Úµã
struct Node *pt = (struct Node*)malloc(sizeof(struct Node));
scanf(pt);
pt->next = NULL
дһ¸ö×Óº¯Êý£¬×Óº¯ÊýµÄ·µ»Ø±íÍ·Ö¸Õë
struct Node * creatLink()
{
struct Node *head //Á´±íµÄ±íÍ·Ö¸Õë
struct Node *pf; //ÔÀ´Á´±íµÄ×îºóÒ»¸ö½ÚµãµÄÖ¸Õë
struct Node *pNew; //ÿ´ÎÐÂÉêÇëµÄ½ÚµãµÄÖ¸Õë
int n; //½ÚµãµÄ¸öÊý£¬Óû§Ñ¡ÔñÊäÈëµÄÊý¾ÝµÄ¸öÊý
int i;
for(i = 1; i<= n; i++
{
//ÐÂÉêÇëÒ»¿éÄÚ´æ¿Õ¼ä
pNew = (struct Node*)malloc(sizeof(struct Node));
//ÐÂÉêÇëµÄ½ÚµãµÄÖ¸ÕëÓòÖÿÕ
//´æ´¢Êý¾Ý£¬ÔÙÐÂÉêÇëµÄ¿Õ¼äÖÐ
scanf
Èç¹ûÐÂÉêÇëµÄ½ÚµãÊǵÚÒ»¸ö½Úµã
Á´±íµÄ±íÍ·Ö¸ÕëÖ¸ÏòµÚÒ»¸ö½Úµã
Á´±íµÄ±íβָÕëÖ¸ÏòµÚÒ»¸ö½Úµã £¨µÚÒ»¸ö½Úµã¼ÈÊDZíÍ·Ò²ÊDZí⣩
Èç¹ûÉêÇëµÄ²»ÊǵÚÒ»¸ö½Úµã
ÔÀ´Á´±íµÄ×îºóÒ»¸ö½ÚµãµÄÖ¸ÕëÓò´æ´¢ÐÂÉêÇëµÄ½ÚµãµÄµØÖ·
ÐÂÉêÇëµÄ½Úµã³ÉΪÁ´±íµÄ×îºóÒ»¸ö½Úµã£¨±í⣩
}
·µ»Ø±íÍ·Ö¸Õë
}
1.1.2 Á©Á´±íµÄ±éÀú
ÖªµÀÁ´±íµÄ±íÍ·£¬´ÓÁ´±íµÄ±íÍ·¿ªÊ¼ÒÀ´ÎÍùϱéÀú£¬ÐèÒªÖªµÀ±éÀúµ½Ê²Ã´Ê±ºòÍ£Ö¹
1.1.3 Á´±í½ÚµãµÄ²éÕÒ
²éÕÒ£º¾²Ì¬²éÕÒ------>²éÕÒ²éÕÒ±íÖеÄÊý¾Ý£¬²»Ð޸IJéÕÒ
¶¯Ì¬²éÕÒ---->ÐèÒª¶Ô²éÕÒ±í½øÐÐд²Ù×÷£¬ÐÞ¸ÄÔÀ´µÄÊý¾Ý
¹Ø¼ü×Ö£º²éÕÒµÄÌõ¼þ
Ö÷¹Ø¼ü×Ö£ºÑ§ºÅ£¬ÒªÃ´ÕÒµ½Ò»¸öÈË£¬ÒªÃ´Ã»ÓÐ
´Î¹Ø¼ü×Ö£º·ûºÏ¸ÃÌõ¼þ¿ÉÒÔÓжà¸öÊý¾Ý
1.1.4 Á´±í½ÚµãµÄɾ³ý
Ê×ÏÈÕÒµ½½áµã------------->ɾ³ý²Ù×÷---->ɾ³ýÖ®ºóÁ´±íÈÔÈ»ÓÐÐò
ûÓÐÕÒµ½½áµã------------->ɾ³ýʧ°Ü
pf; //±íʾҪɾ³ýµÄ½áµã
ÅжÏpfµÄλÖãº
±íÍ· pf == head
Öмä
±íβ pf->next == NULL
1) ÔÚ±íβ²åÈë
2) °´ÕÕijÖÖ(ijһ¸öÊý¾ÝÏî)˳Ðò²åÈë
°´ÕÕ±àºÅ´ÓСµ½´óµÄÐÎʽ½øÐвåÈë
pNew; //Òª²åÈëµÄнáµã
pf //нڵãÒª²åÈëµÄλÖà нڵã²åÈëÔÚpfµÄÇ°Ãæ
//²éÕÒÒª²åÈëµÄλÖÃ
Âú×ãÌõ¼þ£º pNew->num < pf->num
нڵãpNew²åÈëÔÚpfµÄÇ°Ãæ
//²åÈëµÄλÖõÄÅжÏ
ÅжÏpfµÄλÖÃ
1£© pf == head; //нڵã³ÉΪеıíÍ·
2£© pfÔÚÖм䡣 //нڵãpNew²åÈëÔÚpfµÄÇ°Ãæ
3£© Á´±íÖÐûÓкÏÊʵÄÒª²åÈ룬²åÈëµ½Á´±íµÄ×îºó£¬ pf == NULL
#include <stdio.h> #include <stdlib.h> typedef struct Node { char name[15]; int num; char sex[3]; struct Node*next; }LinkNode,*pLinkNode;
LinkNode *creatLink(int n); void printLink(const LinkNode *phead); void findLinkNode(LinkNode *phead); LinkNode *findLinkNodeName(LinkNode *phead,char *name,LinkNode **pt); LinkNode *findLinkNodeNum(LinkNode *phead,int num,LinkNode **pt); LinkNode *insertLinkNode(LinkNode *phead); LinkNode *deleteLinkNode(LinkNode *phead);
int main() { LinkNode *head; int n;
printf("ÇëÊäÈë½ÚµãµÄ¸öÊý£º"); scanf("%d",&n);
head = creatLink(n);
printLink(head);
while(1) { //findLinkNode(head); head = insertLinkNode(head); printLink(head);
head = deleteLinkNode(head); printLink(head); }
return 0; }
/*********************************************************** *º¯ÊýÔÐÍ£ºLinkNode *creatLink(int n) *º¯Êý¹¦ÄÜ£º´´½¨Á´±í *º¯Êý²ÎÊý£ºint n; //±íʾÁ´±í½ÚµãµÄ¸öÊý *º¯Êý·µ»ØÖµ£º·µ»ØÁ´±íµÄ±íÍ·£¬Èç¹ûΪNULL£¬´´½¨Ê§°Ü ************************************************************/
LinkNode *creatLink(int n) { int i; LinkNode *head = NULL; //Á´±íµÄ±íÍ·Ö¸Õë LinkNode *pf = NULL; //Á´±íµÄ±íβָÕë(Ö¸ÕëÁ´±íµÄ×îºóÒ»¸ö½Úµã) LinkNode *pNew = NULL; //ÐÂÉêÇëµÄ½ÚµãµÄÖ¸Õë
for(i = 1; i <= n; i++) { //ÐÂÉêÇëÒ»¸ö½Úµã pNew = (LinkNode *)malloc(sizeof(LinkNode)); //ÐÂÉêÇëµÄ½ÚµãµÄÖ¸ÕëÓòΪ¿Õ pNew->next = NULL; //ÍùÐÂÉêÇëµÄ½Úµã´æ´¢Êý¾Ý printf("ÇëÊäÈë%d¸ö½ÚµãµÄÐÕÃû ±àºÅ ÐÔ±ð£º",i); scanf("%s %d %s",pNew->name,&pNew->num,pNew->sex); if(i == 1) //ÅжÏÊÇ·ñΪÉêÇëµÄµÚÒ»¸ö½Úµã { head = pNew;//±íÍ·Ö¸ÕëÖ¸ÏòÐÂÉêÇëµÄ½Úµã pf = pNew; //±íβָÕëÖ¸ÏòÐÂÉêÇëµÄ½Úµã } else { pf->next = pNew; //ÐÂÉêÇëµÄ½ÚµãµÄµØÖ·´æ´¢ÔÚÔÀ´Á´±íµÄ×îºóÒ»¸ö½áµãµÄÖ¸ÕëÓò pf = pNew; //ÐÂÉêÇëµÄ½áµã³ÆΪÁ´±íеıíβ } } return head; }
/*********************************************************** *º¯ÊýÔÐÍ£ºvoid printLink(const LinkNode *phead) *º¯Êý¹¦ÄÜ£ºÁ´±íµÄ±éÀú *º¯Êý²ÎÊý£ºconst LinkNode *phead Á´±íµÄ±íÍ·Ö¸Õë *º¯Êý·µ»ØÖµ£ºÎÞ *˵Ã÷£º Á´±íµÄ×îºóÒ»¸ö½áµãµÄÖ¸ÕëÓòΪNULL ************************************************************/ void printLink(const LinkNode *phead) { if(phead == NULL) { return; } printf("ÐÕÃû\t ±àºÅ\t ÐÔ±ð\n"); while(phead != NULL) { printf("%s\t %d\t %s\n",phead->name,phead->num,phead->sex); phead = phead->next; } }
/*********************************************************** *º¯ÊýÔÐÍ£ºLinkNode *findLinkNodeNum(LinkNode *phead, int num LinkNode **pt) *º¯Êý¹¦ÄÜ£º²éÕÒÁ´±íÖеÄijһ¸ö½áµã *º¯Êý²ÎÊý£ºLinkNode *phead Á´±íµÄ±íÍ· int num ²éÕÒµÄÌõ¼þ LinkNode **pt ½«²éÕҵĽڵãµÄÒ»¸ö½ÚµãµÄµØÖ·±£´æÔÚ*pt *º¯Êý·µ»ØÖµ£º²éÕÒµ½µÄ½ÚµãµÄµØÖ· Èç¹ûûÓÐÕÒµ½Ôò·µ»ØNULL ************************************************************/ LinkNode *findLinkNodeNum(LinkNode *phead,int num,LinkNode **pt) {
while(phead != NULL ) { if(num < phead->num) { return phead; } *pt = phead; phead = phead->next; }
return NULL; }
/*********************************************************** *º¯ÊýÔÐÍ£ºLinkNode *findLinkNodeName(LinkNode *phead, char *name, LinkNode **pt) *º¯Êý¹¦ÄÜ£º²éÕÒÁ´±íÖеÄijһ¸ö½áµã *º¯Êý²ÎÊý£ºLinkNode *phead Á´±íµÄ±íÍ· char *name ²éÕÒµÄÌõ¼þ LinkNode **pt ½«²éÕҵĽڵãµÄÒ»¸ö½ÚµãµÄµØÖ·±£´æÔÚ*pt *º¯Êý·µ»ØÖµ£º²éÕÒµ½µÄ½ÚµãµÄµØÖ· Èç¹ûûÓÐÕÒµ½Ôò·µ»ØNULL ************************************************************/ LinkNode *findLinkNodeName(LinkNode *phead,char *name,LinkNode **pt) {
while(phead != NULL ) { if(str_cmp(phead->name,name) == 1) { return phead; } *pt = phead; phead = phead->next; }
return NULL; }
int str_cmp(const char *str,const char *buf) { while(*str != '\0' || *buf != '\0') { if(*str != *buf) { return 0; } str++; buf++; } return 1; }
/*********************************************************** *º¯ÊýÔÐÍ£ºvoid findLinkNode(LinkNode *phead) *º¯Êý¹¦ÄÜ£º²éÕÒÁ´±íÖеÄijһ¸ö½áµã *º¯Êý²ÎÊý£ºLinkNode *phead Á´±íµÄ±íÍ· *º¯Êý·µ»ØÖµ£ºÎÞ ************************************************************/
void findLinkNode(LinkNode *phead) { int num;
printf("ÇëÊäÈëÒª²éÕҵıàºÅ£º"); scanf("%d",&num);
while(phead != NULL) { if(phead->num == num) { printf("============²éÕҳɹ¦==============\n"); printf("ÐÕÃû\t ±àºÅ\t ÐÔ±ð\n"); printf("%s\t %d\t %s\n",phead->name,phead->num,phead->sex); break; //return pehad; } phead = phead->next; } if(phead == NULL) { printf("===============²éÕÒʧ°Ü============\n"); } }
//void findLinkNode(LinkNode *phead) //{ // //²éÕÒµÄÌõ¼þ // int flag; // printf("1: °´ÕÕÐÕÃû²éÕÒ"); // printf("2: °´ÕÕ±àºÅ²éÕÒ"); // printf("2: °´Õճɼ¨·¶Î§²éÕÒ"); // // switch(flag) // { // case 1: // case 2: // case 3: // case 4: // } // //}
/*********************************************************** *º¯ÊýÔÐÍ£ºLinkNode *deleteLinkNode(LinkNode *phead) *º¯Êý¹¦ÄÜ£ºÉ¾³ýÁ´±íÖеÄÒ»¸ö½áµã *º¯Êý²ÎÊý£ºLinkNode *phead Á´±íµÄ±íÍ· *º¯Êý·µ»ØÖµ£º·µ»ØÁ´±íµÄ±íÍ·£¬ ************************************************************/ LinkNode *deleteLinkNode(LinkNode *phead) { LinkNode *pf = NULL; //Ҫɾ³ýµÄ½áµã LinkNode *pt = NULL; //Ҫɾ³ýµÄ½áµãµÄÇ°Ò»¸ö½áµã char buf[15];
printf("ÇëÊäÈëҪɾ³ýµÄÐÕÃû£º"); scanf("%s",buf);
pf = findLinkNodeName(phead,buf,&pt); if(pf == NULL) { printf("ɾ³ýʧ°Ü\n"); return phead; } if(pf == phead) //ɾ³ý±íÍ· { phead = phead->next; //±íÍ·µÄÏÂÒ»¸ö½áµã³ÆΪеıíÍ· } else //ɾ³ýÖÐ¼ä »òÕß É¾³ýµÄÊDZíβ { pt->next = pf->next;//Ҫɾ³ýµÄ½áµãµÄÇ°Ò»¸ö½áµãµÄÖ¸ÕëÓò´æ´¢ //Ҫɾ³ýµÄ½áµãµÄÒ»¸ö½áµãµÄµØÖ· } free(pf); //ɾ³ýpfÖ¸ÕëÖ¸ÏòµÄ½áµã pf = NULL;
return phead; } /*********************************************************** *º¯ÊýÔÐÍ£ºLinkNode *deleteLinkNode(LinkNode *phead) *º¯Êý¹¦ÄÜ£ºÔÚÁ´±íÖеÄijһ¸ö½áµã *º¯Êý²ÎÊý£ºLinkNode *phead Á´±íµÄ±íÍ· *º¯Êý·µ»ØÖµ£º·µ»ØÁ´±íµÄ±íÍ·£¬ ************************************************************/ LinkNode *insertLinkNode(LinkNode *phead) { LinkNode *pf = NULL; //Òª²åÈëµÄ½áµãλÖà LinkNode *pt = NULL; //Òª²åÈëµÄ½áµãµÄÇ°Ò»¸ö½áµã LinkNode *pNew = NULL; //вåÈëµÄ½áµã
printf("ÇëÊäÈëÒª²åÈëµÄÐÕÃû ±àºÅ ÐÔ±ð£º"); pNew = (LinkNode *)malloc(sizeof(LinkNode)); scanf("%s %d %s",pNew->name,&pNew->num,pNew->sex);
pf = findLinkNodeNum(phead,pNew->num,&pt); if(pf == NULL) { //нڵãÖ»ÄܲåÈëÔÚ±íβ pt->next = pNew; pNew->next = NULL; //нڵã³ÉΪеıíβ } if(pf == phead) //²åÔÚ±íÍ·Ç°Ãæ³ÉΪеıíÍ· { pNew->next = phead; phead = pNew; } else //²åÈëÖмä { pt->next = pNew; //Òª²åÈëµÄ½áµãµÄÇ°Ò»¸ö½áµãµÄÖ¸ÕëÓò´æ´¢ÐÂÉêÇëµÄ½áµãµÄµØÖ· pNew->next = pf; //ÐÂÉêÇëµÄ½áµãµÄÖ¸ÕëÓò´æ´¢Òª²åÈëµÄ½áµãµÄµØÖ· } return phead; } |