单链表的各种操作

001/*单链表的各种操作*/
002 
003# define null 0
004 
005typedef char ElemType; /* 字符型数据*/
006 
007typedef struct LNode
008{
009    ElemType data;
010    structLNode *next;
011};
012         
013setnull(structLNode **p);
014int length (struct LNode **p);
015ElemType get(structLNode **p,inti);
016void insert(struct LNode **p,ElemType x,int i);
017int delete(structLNode **p,inti);
018void display(struct LNode **p);
019 
020main()
021{
022    structLNode *head,*q;  /*定义静态变量*/
023    intselect,x1,x2,x3,x4;
024    inti,n;
025    intm,g;
026    chare,y;
027     
028    head=setnull(&head); /*建议链表并设置为空表*/
029    printf("请输入数据长度: ");
030    scanf("%d",&n);
031    for(i=1;i<n;i++);
032    {
033        printf("将数据插入到单链表中: ");
034        scanf("%d",&y);
035        insert(&head,y,i);} /*插入数据到链表*/
036        display(&head); /*显示链表所有数据*/
037         
038        printf("select 1 求长度 length()\n");
039        printf("select 2 取结点 get()\n");
040        printf("select 3 求值查找 locate()\n");
041        printf("select 4 删除结点 delete()\n");
042        printf("input your select: ");
043        scanf("%d",&select);   
044        switch(select)
045        {
046            case1:
047            {
048                x1=length(&head);
049                printf("输出单链表的长度%d ",x1);
050                display(&head);
051            }break;
052             
053            case2:
054            {
055                printf("请输入要取得结点: ");
056                    scanf("%d",&m);
057                    x2=get(&head,m);
058                    printf(x2);
059                display(&head);
060            }break;
061         
062            case3:
063            {
064                printf("请输入要查找的数据: ");
065                    scanf("%d",&e);
066                    x3=locate(&head,e);
067                    printf(x3);
068                display(&head);
069            }break;
070             
071            case4:
072            {
073                printf("请输入要删除的结点: ");
074                    scanf("%d",&g);
075                    x4=delete(&head,g);
076                    printf(x4);
077                display(&head);
078            }break;
079        }
080    }
081}
082 
083 
084setnull(structLNode **p)
085{
086    *p=null;
087}
088 
089int length (struct LNode **p)
090{
091    intn=0;
092    structLNode *q=*p;
093    while(q!=null)
094    {
095        n++;
096        q=q->next;
097    }
098    return(n);
099}
100 
101ElemType get(structLNode **p,inti)
102{
103    intj=1;
104    structLNode *q=*p;
105    while(j<i&&q!=null)
106    {
107        q=q->next;
108        j++;
109    }
110    if(q!=null)
111        return(q->data);
112    else
113        printf("位置参数不正确!\n");
114}
115 
116int locate(struct LNode **p,ElemType x)
117{
118    intn=0;
119    structLNode *q=*p;
120    while(q!=null&&q->data!=x)
121    {
122        q=q->next;
123        n++;
124    }
125    if(q==null)
126        return(-1);
127    else
128        return(n+1);
129}
130 
131void insert(struct LNode **p,ElemType x,int i)
132{
133    intj=1;
134    structLNode *s,*q;
135    s=(structLNode *)malloc(sizeof(structLNode));
136    s->data=x;
137    q=*p;
138    if(i==1)
139    {
140        s->next=q;
141        p=s;
142    }
143    else
144    {
145        while(j<i-1&&q->next!=null)
146        {
147            q=q->next;
148                j++;
149        }
150        if(j==i-1)
151            {
152                s->next=q->next;
153                q->next=s;
154            }
155        else
156            printf("位置参数不正确!\n");
157    }  
158}
159 
160int delete(structLNode **p,inti)
161{
162    intj=1;
163    structLNode *q=*p,*t;
164    if(i==1)
165    {
166        t=q;
167        *p=q->next;
168    }
169    else
170    {
171        while(j<i-1&&q->next!=null)
172        {
173            q=q->next;
174                j++;
175        }
176            if(q->next!=null&&j==i-1)
177            {
178                t=q->next;
179                q->next=t->next;
180            }
181            else
182                printf("位置参数不正确!\n");
183    }
184    if(t=null) 
185        free(t);
186}
187 
188void display(struct LNode **p)
189{  
190    structLNode *q;
191    q=*p;
192    printf("单链表显示: ");
193    if(q==null)
194        printf("链表为空!");
195    elseif (q->next==null)
196            printf("%c\n",q->data);
197        else
198        {
199            while(q->next!=null)
200            {
201                printf("%c->",q->data);
202                q=q->next;
203            }
204        printf("%c",q->data);
205    }
206    printf("\n");
207}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值