单链表(C程序设计教程)

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 struct listNode{
  5     char data;
  6     struct listNode *nextPtr;
  7     };
  8 typedef struct listNode LISTNODE;
  9 typedef LISTNODE *LISTNODEPTR;
 10 
 11 void insertList(LISTNODEPTR *,char);
 12 char deleteList(LISTNODEPTR *,char);
 13 int isEmpty(LISTNODEPTR);
 14 void printList(LISTNODEPTR);
 15 void instruction(void);
 16 
 17 int main()
 18 {
 19     LISTNODEPTR startPtr=NULL;
 20     int choice;
 21     char item;
 22     instruction();/*显示菜单*/
 23     printf("\n?\n");
 24     scanf("%d",&choice);
 25     while(choice!=3)
 26     {
 27             switch(choice)
 28             {
 29                 case 1:
 30                     printf("Enter a character:");
 31                     scanf("\n%c",&item);
 32                     insertList(&startPtr,item);
 33                     printList(startPtr);
 34                     break;
 35                 case 2:
 36                     if(!isEmpty(startPtr))
 37                     {
 38                         printf("Enter character to be deleted:");
 39                         scanf("\n%c",&item);
 40                         if(deleteList(&startPtr,item))
 41 
 42                             {
 43                                 printf("%c deleted.\n",item);
 44                                 printList(startPtr);
 45                             }
 46                         else
 47                             printf("%c not found.\n\n",item);
 48 
 49                     }
 50                     else
 51                         printf("List is empty.\n\n");
 52                     break;
 53                 default:
 54                     printf("Invalid choice.\n\n");
 55                     instruction();
 56                     break;
 57             }
 58         printf("?");
 59         scanf("%d",&choice);
 60     }
 61     printf("End of run.\n");
 62 
 63     return 0;
 64 }
 65 
 66 
 67 void instruction(void)
 68 {
 69     printf("Enter your choice:\n"
 70            "1 to insert an element to the List.\n"
 71            "2 to delete an element from the List.\n"
 72            "3 to end.");
 73 }
 74 
 75 void insertList(LISTNODEPTR *sPtr,char value)
 76 {
 77     LISTNODEPTR newPtr,previousPtr,currentPtr;
 78     newPtr=malloc(sizeof(LISTNODEPTR));
 79     if(newPtr!=NULL)
 80     {
 81         newPtr->data=value;
 82         newPtr->nextPtr=NULL;
 83         previousPtr=NULL;
 84         currentPtr=*sPtr;
 85         while(currentPtr!=NULL&&value>currentPtr->data)
 86         {
 87             previousPtr=currentPtr;
 88             currentPtr=currentPtr->nextPtr;
 89         }
 90         if(previousPtr==NULL)
 91         {
 92             newPtr->nextPtr=*sPtr;
 93             *sPtr=newPtr;
 94         }
 95         else
 96         {
 97             previousPtr->nextPtr=newPtr;
 98             newPtr->nextPtr=currentPtr;
 99         }
100 
101     }
102     else
103         printf("%c not inserted.No memory avaliable.\n",value);
104 }
105 
106 char deleteList(LISTNODEPTR *sPtr,char value)
107 {
108     LISTNODEPTR previousPtr,currentPtr,tempPtr;
109     if(value==((*sPtr)->data))
110     {
111         tempPtr=*sPtr;
112         *sPtr=(*sPtr)->nextPtr;
113         free(tempPtr);
114         return value;
115     }
116     else
117     {
118         previousPtr=*sPtr;
119         currentPtr=(*sPtr)->nextPtr;
120         while(currentPtr!=NULL&&currentPtr->data!=value)
121         {
122             previousPtr=currentPtr;
123             currentPtr=currentPtr->nextPtr;
124         }
125         if(currentPtr!=NULL)
126         {
127             tempPtr=currentPtr;
128             previousPtr->nextPtr=currentPtr->nextPtr;
129             free(tempPtr);
130             return value;
131         }
132     }
133     return '\0';
134 }
135 
136 
137 int isEmpty(LISTNODEPTR sPtr)
138 {
139     return sPtr==NULL;
140 }
141 
142 void printList(LISTNODEPTR currentPtr)
143 {
144     if(currentPtr==NULL)
145         printf("List is empty.\n\n");
146     else
147     {
148         printf("The List is:\n");
149         while(currentPtr!=NULL)
150         {
151             printf("%c-->",currentPtr->data);
152             currentPtr=currentPtr->nextPtr;
153 
154         }
155         printf("NULL \n\n");
156     }
157 }

 

转载于:https://www.cnblogs.com/SkyFire/archive/2013/04/13/3019162.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值