Welcome, guest! Login / Register - Why register?
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 C 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 &#40;"plik.txt","r"&#41;;
 rewind(baza);
 while(fgetc(baza)!=EOF){
  if(fgetc(baza)==' ')
   a++;
 }
 return a;
}

void save(int num)
{
 baza=fopen &#40;"plik.txt","a"&#41;;
 fprintf(baza,"%d ",num);
 fclose(baza);
}

void read()
{
 if ((baza=fopen&#40;"plik.txt", "a"&#41;)!=NULL) 
 {
  fclose(baza);
  int read,i,a;
  a=check();
  baza=fopen &#40;"plik.txt","r"&#41;;
  for(i=0;i<a;i++)
  {
   if (feof(baza)) break;
   fscanf(baza,"%d",&read;);
   add(read, &head;);
  }
 }
}

int main(){
 int num,i;
 read();
 printf("How many elements would you like to add? ");
 scanf("%d",&num;);
 for(i=1;i<=num;i++){
  add(i, &head;);
  save(i);
 }
 printlist(head);
 free(head);
 system&#40;"pause"&#41;;
}

 

Revise this Paste

Your Name: Code Language: