Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Plain Text by wojczer ( 14 years ago )
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct node
{
int v;
struct node* next;
};
struct node* head=NULL;
void printlist(struct node* head)
{
while (head != NULL)
{
printf("%d\n",head->v);
head=head->next;
}
}
int add(int new_value,struct node** pHead)
{
struct node* tmp,*tmp2;
tmp=(struct node*)malloc(sizeof(struct node));
if(tmp==NULL) return 1;
tmp->v=new_value;
tmp->next=NULL;
if((*pHead)==NULL)
*pHead=tmp;
else
{
tmp2=*pHead;
while(tmp2->next != NULL)
tmp2=tmp2->next;
tmp2->next=tmp;
}
return 0;
}
FILE * baza;
int check()
{
int a=0;
baza=fopen ("plik.txt","r");
rewind(baza);
while(fgetc(baza)!=EOF){
if(fgetc(baza)==' ')
a++;
}
return a;
}
void save(int num)
{
baza=fopen ("plik.txt","a");
fprintf(baza,"%d ",num);
fclose(baza);
}
void read()
{
int read,i,a;
a=check();
baza=fopen ("plik.txt","r");
for(i=0;i<a;i++){
if (feof(baza)) break;
fscanf(baza,"%d",&read;);
add(read, &head;);
}
}
int main(){
int num,i;
printf("%d",check());
read();
printf("How many elements would you like to add? ");
scanf("%d",#);
for(i=1;i<=num;i++){
add(i, &head;);
save(i);
}
printlist(head);
free(head);
system("pause");
}
Revise this Paste