/************************************************************************//*name:insertNode.c*//*func:向鏈表中插入結點*//*time:2013-04-01 *//************************************************************************/#include <stdio.h>#include <malloc.h>#include <stdlib.h>#include <string.h>#define NODESIZE 5typedef struct test{char cword;struct test *next;}LNode,*Linklist;//建立鏈表Linklist creat(char *str,int n){LNode *p1,*p2;LNode *head;int i;head = NULL;for(i=0; i<n; i++){p1=(LNode*)malloc(sizeof(LNode));p1->cword = str[i];if(head == NULL){head=p1;}else{p2->next=p1;}p2=p1;}p2->next=NULL;//最後結點指標域為空白return head;//返回頭指標}//插入結點//想象成小朋友手拉手,要加入一位新的小朋友,需要在插入位置使兩學生手脫開//前面同學拉新同學手,新同學拉後面同學手,這樣就完成了插入,成為新的隊列Linklist insert(Linklist head,char c,int n){Linklist p1=head,s;int j=0,i=n;//初始化while (p1 && j<i-1){p1=p1->next;j++;}if(!p1 || j>i-1)exit(0);s=(LNode*)malloc(sizeof(LNode));s->cword=c;s->next=p1->next;p1->next=s;return head;}int main(){Linklist head,p;int i;char str[NODESIZE];int len=NODESIZE;printf("輸入%d個鏈表結點(字串形式輸入):",NODESIZE);for (i=0; i<len; i++){scanf("%c",&str[i]);}head=creat(str,len);if(head == NULL)//建立鏈表失敗return -1;/////////////////////////////////插入前////////////////////////////////////printf("插入前\n");p = head;while (p!=NULL){printf("%c ",p->cword);p = p->next;}printf("\n");//////////////////////////////////////////////////////////////////////////char c;//需要插入的字元printf("輸入要插入結點的位置:");scanf("%d",&i);getchar();//擷取斷行符號字元printf("輸入要插入的結點內容:");scanf("%c",&c);head=insert(head,c,i);//////////////////////////////////插入後//////////////////////////////////printf("插入後\n");p = head;while (p!=NULL){printf("%c ",p->cword);p = p->next;}printf("\n");//////////////////////////////////////////////////////////////////////////return 0;}
/************************************************************************//*name:deleteNode.c*//*func:刪除鏈表中的結點*//*time:2013-04-02 *//************************************************************************/#include <stdio.h>#include <malloc.h>#include <stdlib.h>#include <string.h>#define NODESIZE 5typedef struct test{char cword;struct test *next;}LNode,*Linklist;//建立鏈表Linklist creat(char *str,int n){LNode *p1,*p2;LNode *head;int i;head = NULL;for(i=0; i<n; i++){p1=(LNode*)malloc(sizeof(LNode));p1->cword = str[i];if(head == NULL){head=p1;}else{p2->next=p1;}p2=p1;}p2->next=NULL;//最後結點指標域為空白return head;//返回頭指標}//刪除結點,記得釋放刪除的結點空間Linklist deleteNode(Linklist head,int n){Linklist p=head,q=head;int i=n;int j=0;while(p && j<i-1){p=p->next;j++;}while(j &&(--j))//使指標q指向p所指向的結點{q=q->next;}if(!p || j>i-1)exit(0);if(p->next==NULL)//刪除元素為鏈表末尾元素{q->next=NULL;free(p);return head;}if(i==1)//刪除元素為鏈表首部節點(無頭結點鏈表){head=head->next;free(p);return head;}q->next=p->next;free(p);return head;}int main(){Linklist head,p;int i;char str[NODESIZE];int len=NODESIZE;printf("輸入%d個鏈表結點(字串形式輸入):",NODESIZE);for (i=0; i<len; i++){scanf("%c",&str[i]);}head=creat(str,len);if(head == NULL)//建立鏈表失敗return -1;/////////////////////////////////刪除前////////////////////////////////////printf("刪除前\n");p = head;while (p!=NULL){printf("%c ",p->cword);p = p->next;}printf("\n");//////////////////////////////////刪除後//////////////////////////////////int pos;//要刪除元素位置printf("輸入要刪除元素的位置:");scanf("%d",&pos);p=deleteNode(head,pos);printf("刪除後\n");while (p!=NULL){printf("%c ",p->cword);p = p->next;}printf("\n");return 0;}
/************************************************************************//*name:updateNode.c*//*func:修改鏈表結點內容*//*time:2013-04-02 *//************************************************************************/#include <stdio.h>#include <malloc.h>#include <stdlib.h>#include <string.h>#define NODESIZE 5typedef struct test{char cword;struct test *next;}LNode,*Linklist;//建立鏈表Linklist creat(char *str,int n){LNode *p1,*p2;LNode *head;int i;head = NULL;for(i=0; i<n; i++){p1=(LNode*)malloc(sizeof(LNode));p1->cword = str[i];if(head == NULL){head=p1;}else{p2->next=p1;}p2=p1;}p2->next=NULL;//最後結點指標域為空白return head;//返回頭指標}//修改鏈表結點內容Linklist updateNode(Linklist head,char c,int n){Linklist p=head,q=head;int i=n;int j=0;while(p && j<i-1){p=p->next;j++;}if(!p || j>i-1)exit(0);p->cword=c;//修改元素的值return head;}int main(){Linklist head,p;int i;char str[NODESIZE];int len=NODESIZE;printf("輸入%d個鏈表結點(字串形式輸入):",NODESIZE);for (i=0; i<len; i++){scanf("%c",&str[i]);}head=creat(str,len);if(head == NULL)//建立鏈表失敗return -1;/////////////////////////////////修改前////////////////////////////////////printf("修改前\n");p = head;while (p!=NULL){printf("%c ",p->cword);p = p->next;}printf("\n");//////////////////////////////////修改後//////////////////////////////////int pos;//要修改元素位置char newc;//修改元素的值printf("輸入要修改元素的位置:");scanf("%d",&pos);getchar();//擷取斷行符號字元printf("輸入要修改元素的值:");scanf("%c",&newc);p=updateNode(head,newc,pos);printf("修改後\n");while (p!=NULL){printf("%c ",p->cword);p = p->next;}printf("\n");return 0;}
/************************************************************************//*name:searchNode.c*//*func:尋找鏈表中的結點*//*time:2013-04-01 *//************************************************************************/#include <stdio.h>#include <malloc.h>#include <stdlib.h>#include <string.h>#define NODESIZE 5typedef struct test{char cword;struct test *next;}LNode,*Linklist;//建立鏈表Linklist creat(char *str,int n){LNode *p1,*p2;LNode *head;int i;head = NULL;for(i=0; i<n; i++){p1=(LNode*)malloc(sizeof(LNode));p1->cword = str[i];if(head == NULL){head=p1;}else{p2->next=p1;}p2=p1;}p2->next=NULL;//最後結點指標域為空白return head;//返回頭指標}//尋找結點Linklist search(Linklist head,char &c,int n){Linklist p1=head;int j=0,i=n;//初始化while (p1 && j<i-1){p1=p1->next;j++;}if(!p1 || j>i-1)exit(0);c = p1->cword;return head;}int main(){Linklist head,p;int i;char str[NODESIZE];int len=NODESIZE;printf("輸入%d個鏈表結點(字串形式輸入):",NODESIZE);for (i=0; i<len; i++){scanf("%c",&str[i]);}head=creat(str,len);if(head == NULL)//建立鏈表失敗return -1;/////////////////////////////////尋找前////////////////////////////////////printf("尋找前\n");p = head;while (p!=NULL){printf("%c ",p->cword);p = p->next;}printf("\n");//////////////////////////////////////////////////////////////////////////char c;printf("輸入要尋找結點的位置:");scanf("%d",&i);head=search(head,c,i);printf("尋找位置的元素為:%c\n",c);return 0;}