Удаление элемента из односвязного списка - C (СИ) (77497)
Формулировка задачи:
Почему не работает функция удаления идентификатора? Как ее можно переделать?
#include"stdafx.h"
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void print(struct spisok *p);
struct spisok
{
char id[8];
struct spisok *next;
};
struct spisok* vvod(struct spisok *p, char i[8]);
struct spisok* udalenie(struct spisok *p);
int main()
{
int i;
char j[8];
struct spisok *p,*f;
p=NULL;
for(i=0;i<3;i++)
{
printf("vvedite identificatori\n");
gets(j);
p= vvod(p,j);
}
f=udalenie(p);
print(f);
getch();
return 0;
}
struct spisok* vvod(struct spisok *p, char j[8] )
{
struct spisok *pt,*k,*f;
pt = (struct spisok *) malloc(sizeof( struct spisok));
strcpy(pt->id, j);
if(p==NULL || strcmp(pt->id,p->id)<0)
{
pt->next=p;
p=pt;
}
else
{
k=p;
while(k!=NULL && strcmp(pt->id,k->id)>=0)
{
f=k;
k=k->next;
}
f->next=pt; pt->next=k;
}
return p;
}
void print(struct spisok *p)
{
struct spisok *pt;
for(pt=p;pt!=NULL;pt=pt->next)
puts(pt->id);
}
struct spisok* udalenie(struct spisok *p)
{
char j[8];
struct spisok *pt,*f,*k;
printf("vvedite identificator kotorii nugno udalit\n");
gets(j);
pt=p;
f=p;
while(pt->next!=NULL)
{
k=pt->next;
if(!strcmp(j,pt->id))
f->next=k;
if(pt!=p)
f->next=pt;
pt=pt->next;
}
return p;
}Решение задачи: «Удаление элемента из односвязного списка»
textual
Листинг программы
if(prev) prev->next = pt;