Справочник функций

Ваш аккаунт

Войти через: 
Забыли пароль?
Регистрация
Информацию о новых материалах можно получать и без регистрации:

Почтовая рассылка

Подписчиков: -1
Последний выпуск: 19.06.2015

список-строка C

87K
26 апреля 2013 года
dima0294
2 / / 26.04.2013
Есть строка-список. Удалить из списка все слова длина которых != длине первого слова. Не понимаю как зациклить этот участок кода для всего списка(для удаление следующего слова после первого работает)

if(LenNext == LenFirst)
{
Next = Point(Next, LenFirst);
}
else
{
for(int i=0; i<LenNext; i++)
{
tmp = Next->next;
Next->next = tmp->next;
free(tmp);
}
}

сам код вот :

# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
# include <string.h>

struct Item
{
char ptr;
Item *next;
};

struct Item *GetHead(char *str)
{
struct Item *head = NULL, *now, *last;
for(; *str; str++)
{
if(head == NULL)
{
head = now = (struct Item*)malloc(sizeof(struct Item));
head -> ptr = *str;
}
else
{
now = (struct Item*)malloc(sizeof(struct Item));
now -> ptr = *str;
last -> next = now;
}
last = now;
}
last -> next = NULL;
return head;
}

void OutPutList(struct Item *head)
{
for(; head!=NULL; head = head->next)
{
printf("%c", head->ptr);
}
}

void DeleteItem(struct Item *head)
{
while(head!=NULL)
{
head = head -> next;
free(head);
}
}

int CountWord(struct Item *head)
{
int counter = 0;
while(head!=NULL)
{
if(head->ptr == 32)
{
break;
}
counter++;
head = head->next;
}
return counter;
}

struct Item *Point(struct Item *head, int length)
{
struct Item *last;
last = head;
while(length > 0 && last->next)
{
last = last -> next;
length--;
}
return last;
}

void DeleteElements(struct Item *Point)
{
struct Item *cur;
cur = Point->next;
Point->next = cur->next;
Point->ptr = cur->ptr;
free(cur);
}

int main()
{
struct Item *first;
char *str;
char ch;
int i = 0;
do
{
int counter=0;
int size=1;
str = (char*)malloc(sizeof(char)*size);
printf("\nEnter String : ");
do
{
ch = getch();
if(ch == 13)
{
break;
}
else
{
printf("%c", ch);
str[counter] = ch;
counter++;
size++;
str = (char*)realloc(str, sizeof(char)*size);
}
}
while(ch!=13);
str[counter] = 32;
counter++;
str[counter] = NULL;

int len = strlen(str);
printf("\nLen : %d", len);
printf("\nEntered String : ");
first = GetHead(str); // получить указатель на 1-ый элемент списка
OutPutList(first); // вывести список

struct Item *last;
last = Point(first, counter); // получить указатель на последний элемент списка
printf("\nThe End Is : %c", last->ptr);

int LenFirst = CountWord(first);// подсчет длины первого слова в списке
printf("\nLenFirstWord : %d", LenFirst);
struct Item *Next;
Next = Point(first, LenFirst); // получение указателя на пробел перед вторым словом
printf("\nRes : %c", Next->ptr);

int LenNext = CountWord(Next->next); // подсчет длины второго слова
printf("\nLenNext : %d", LenNext);

/* вот в этом цикле ошибка.*/

struct Item *tmp = NULL;


if(LenNext == LenFirst)
{
Next = Point(Next, LenFirst);
}
else
{
for(int i=0; i<LenNext; i++)
{
tmp = Next->next;
Next->next = tmp->next;
free(tmp);
}
}


printf("\nThe Result Is : ");
OutPutList(first);

}
while(ch != 121);

getch();
return 0;
}
87K
26 апреля 2013 года
dima0294
2 / / 26.04.2013
помогите пожалуйста!)
Реклама на сайте | Обмен ссылками | Ссылки | Экспорт (RSS) | Контакты
Добавить статью | Добавить исходник | Добавить хостинг-провайдера | Добавить сайт в каталог