On-line: гостей 0. Всего: 0 [подробнее..]
Программисты всех стран, объединяйтесь!

АвторСообщение



ссылка на сообщение  Отправлено: 22.11.14 07:15. Заголовок: structure variables c programming


I want to modify this program to use a linked list o PayRecord structure variables instead of an array of those structures:

#include <stdio.h>

#define NUMRECS 5

struct PayRecord /* construct a global structure type */
{
int id;
char name[20];
double rate;
};

int main()
{
int i;
struct PayRecord employee[NUMRECS] = {{32479, "Abrams, B.", 6.72},
{33623, "Bohm, P.", 7.54},
{34145, "Donaldson, S.", 5.56},
{35987, "Ernst, T.", 5.43},
{36203, "Gwodz, K.", 8.72}
};
struct PayRecord *recptr = employee; /* Pointer to array elements */
int idnum; /* User entered employee id value */
double rate; /* User entered pay rate */
int found = 0; /* Flag when employee record found */

for (i = 0; i < NUMRECS; i++, recptr++)
printf("%d %-20s %4.2f\n",
recptr->id,recptr->name,recptr->rate);

/* Get employee id and new rate */
printf("\nEnter employee number followed by new pay rate: ");
scanf("%d %lf", &idnum, &rate);

/* Search for employee id in array */
for (i = 0, recptr = employee; i < NUMRECS && !found; i++)
if (recptr->id == idnum)
found = 1; /* Set flag when found */
else recptr++; /* Otherwise increment pointer */

if (!found) /* No record found */
printf("\nNo such employee found!\n\n");
else /* Found the employee */
recptr->rate = rate; /* Update the rate */

/* Output the array again */
for (i = 0, recptr = employee; i < NUMRECS; i++, recptr++)
printf("%d %-20s %4.2f\n",
recptr->id,recptr->name,recptr->rate);
return 0;
}


Kind of like this:

#include <stdio.h>

int main()
{
struct TeleType /* Structure with link pointer */
{
char name[30];
char phoneNum[15];
struct TeleType *nextaddr;
};

/* A series of structure variables, or records, are declared and */
/* initialized, except for the nextaddr field: */
struct TeleType t1 = {"Acme, Sam","(555) 898 2392"};
struct TeleType t2 = {"Dolan, Edith","(555) 682 3104"};
struct TeleType t3 = {"Lanfrank, John","(555) 718 4581"};

/* Now the nexaddr field in each record is updated so that each record */
/* has a pointer to the next record. The last record has NULL in the */
/* nextaddr field to close off the list. */
t1.nextaddr = &t2; /* store t2's address in t1.nextaddr */
t2.nextaddr = &t3; /* store t3's address in t2.nextaddr */
t3.nextaddr = NULL; /* store the NULL address in t3.nextaddr */

struct TeleType *outptr = &t1; /* Point into list */

while (outptr != NULL) /* display till end of linked list */
{
printf("%-30s %-20s\n", outptr->name, outptr->phoneNum);
outptr = outptr->nextaddr; /* get next address */
}

return 0;
}


Any help would be great :)

Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 1 [только новые]





ссылка на сообщение  Отправлено: 22.11.14 08:59. Заголовок: If you need the same..


If you need the same functionality as with struct TeleType then simply add one more member to your original structure that will be a pointer to a next element in the list.

Or you could define one more structure like
 
struct Node
{
struct Node *next;
struct PayRecord record;
};


As for me then it is a bad idea to combine local variables in a list because if you need to add a new element in the list you need to declare a new local variable with unique name.

I think that you need a list where each element of which is allocated dynamically.

Спасибо: 0 
ПрофильЦитата Ответить
Ответ:
1 2 3 4 5 6 7 8 9
большой шрифт малый шрифт надстрочный подстрочный заголовок большой заголовок видео с youtube.com картинка из интернета картинка с компьютера ссылка файл с компьютера русская клавиатура транслитератор  цитата  кавычки моноширинный шрифт моноширинный шрифт горизонтальная линия отступ точка LI бегущая строка оффтопик свернутый текст

показывать это сообщение только модераторам
не делать ссылки активными
Имя, пароль:      зарегистрироваться    
Тему читают:
- участник сейчас на форуме
- участник вне форума
Все даты в формате GMT  3 час. Хитов сегодня: 10
Права: смайлы да, картинки да, шрифты да, голосования нет
аватары да, автозамена ссылок вкл, премодерация откл, правка нет