Space for google add

Line Editor program.

Assignment 1 :- Line Editor 


line.c 

 #include<stdio.h>

#include<stdlib.h>

#include<string.h>


struct node

{

struct node *prev;

char data[80];

struct node *next;

}*head=NULL,*newnode,*last=NULL,*temp,*ptr;

char line[80], fname[20];

int l1=0;


void display()

{

int i;

if(head==NULL)

printf("\n Linked list is empty.\n");

else

for(temp=head,i=1;temp!=NULL;i++,temp=temp->next)

{

printf("\n %d:%s",i,temp->data);

}

}


void create_link_list(char fname[])

{

FILE *fp;

fp=fopen(fname,"r");

if(fp!=NULL)

{

while(fgets(line,80,fp))

                {

                  newnode=(struct node *)malloc(sizeof(struct node));

strcpy(newnode->data,line);

newnode->prev=NULL;

newnode->next=NULL;


if(head==NULL)

head=last=newnode;

else

{

newnode->prev=last;

last->next=newnode;

last=last->next;

}

}

fflush(stdin);   

}

else

printf("\nFile is empty.");

fclose(fp);

}

int count()

{

int count=0;

for(temp=head;temp!=NULL;temp=temp->next)

{

count++;

}

return count;

}


void save()

{

FILE *fp1;

fp1=fopen(fname,"w");

if(!(fp1))

{

printf("\nFile not saved.\n");

exit(0);

}

else

{

for(temp=head;temp!=NULL;temp=temp->next)

fputs(temp->data,fp1);

                

}

printf("\nFile saved successfully.\n");

fclose(fp1);

}


void append()

{

printf("\nEnter text\n");

gets(line);

fflush(stdin);

strcat(line,"\n");

newnode=(struct node *)malloc(sizeof(struct node));

strcpy(newnode->data,line);

newnode->prev=NULL;

newnode->next=NULL;

last->next=newnode;

newnode->prev=last;

last=newnode;

count()+1;

}

void print(int n1,int n2)

{

int i;

if(head==NULL)

printf("File is empty.\n");

else

{

for(i=1,temp=head;i<n1 && temp!=NULL;i++)

temp=temp->next;

for(i=n1;i<=n2;i++)

{

printf("\nLine no.%d:%s",i,temp->data);

temp=temp->next;

}

}

}


void find(char *p)

{

int i=1;

temp=head;

while(temp)

{

if(strstr(temp->data,p))

{

printf("%i : %s",i,temp->data);

}

temp=temp->next;

i++;

}

}

void delete(int n1,int n2)

{

int i;

if(head==NULL)

printf("\nFile is empty.\n");

else

if(n1>count() || n2>count())

printf("\nInvalid Position\n");

else 

if (n1==1)

{

for(i=1;i<=n2;i++)

if(head==NULL)

head=last=NULL;

else

{

temp=head;

head=head->next;

free(temp);

}

}

else if(n2==count())

{

while(n2!=(n1-1))

{

temp=last;

last=last->prev;

free(temp);

--n2;

}

}

else

{

for(i=1,ptr=head;ptr!=NULL;i++,ptr=ptr->next)

if(i>=n1 && i<=n2)

{

temp=ptr;

ptr->next->prev=ptr->prev;

ptr->prev->next=ptr->next;

free(temp);

}

}

}

 void move(int n1,int n2,int n3)

{

int i,j;

struct node *p,*h=NULL,*l=NULL,*newnode;

if(n1>n2 || n3>count())

{

printf("\nINVALID POSITION.");

return;

}


for(ptr=head,j=1;ptr!=NULL && j<n3;ptr=ptr->next,++j);

for(p=head,i=1;p!=NULL;p=p->next,i++)

if(i>=n1 && i<=n2)

{

newnode=(struct node *)malloc(sizeof(struct node));

newnode->next=newnode->prev=NULL;

strcpy(newnode->data,p->data);

if(h==NULL)

h=l=newnode;

else

{

l->next=newnode;

newnode->prev=l;

l=newnode;

}

}

delete(n1,n2);

if(ptr->next==NULL)

{

ptr->next=h;

h->prev=ptr;

last=l;

}

else

{

h->prev=ptr;

l->next=ptr->next;

ptr->next=h;

}


}


void copy(int n1,int n2,int n3)

{

int i;

struct node *p,*l=NULL,*h=NULL,*newnode;

if(n1>n2 || n3>count())

{

printf("\nINVALID POSITION.");

return;

}

for(ptr=head,i=1;ptr!=NULL && i<n3;ptr=ptr->next,i++);

for(p=head,i=1;p!=NULL;p=p->next,i++)

if(i>=n1 && i<=n2)

{

newnode=(struct node *)malloc(sizeof(struct node));

newnode->next=newnode->prev=NULL;

strcpy(newnode->data,p->data);

if(h==NULL)

h=l=newnode;

else

{

l->next=newnode;

newnode->prev=l;

l=newnode;

}

}

if(ptr->next==NULL)

{

ptr->next=h;

h->prev=ptr;

last=l;

}

else

{

h->prev=ptr;

l->next=ptr->next;

ptr->next=h;

}

}

void insert(int n1)

{

int i;

for(i=1,temp=head;temp!=NULL&& i<n1;i++,temp=temp->next);

           

if(i==n1&& temp!=NULL)

{

  printf("\nEnter text\n");

fflush(stdin);

gets(line);

strcat(line,"\n");

newnode=(struct node *)malloc(sizeof(struct node));

strcpy(newnode->data,line);

newnode->prev=NULL;

newnode->next=NULL;

newnode->next=temp->next;

newnode->prev=temp;

                if(temp->next!=NULL)

temp->next->prev=newnode;

temp->next=newnode;

l1++;

}

else

{

if(temp==NULL)

printf("\nLine can't insert.\n");

}


void main(int ac,char *av[])

{

FILE *fp;

char s1,cmd[10],s5[20];

int s2, s3,s4;

strcpy(fname,av[1]);

        create_link_list(fname);

      while(1)

      {

        printf("\n $]");

        gets(cmd);

s2=0;s3=0;

s1=' ';

        sscanf(cmd,"%c%d%d%d",&s1,&s2,&s3,&s4); 

        switch(s1)

        {

  case 'd':

if(s3==0)

      delete(s2,s2);

    else if(s2==0 && s3==0)

delete(1,count());

else

    delete(s2,s3);

printf("\nData deleted.");

    break;

  case 'C':

if(s4==0)

copy(s2,s2,s3);

else

copy(s2,s3,s4);

printf("\nData Copied.");

break;

  case 'm':

if(s4==0)

move(s2,s2,s3);

else

move(s2,s3,s4);

printf("\nData Moved.");

break;

  case 'p':

if(s2<0||s3>count())

{

printf("\nInvalid number of lines.\n");

printf("\n File has %d lines\n",count());

}

else if(s2==0 && s3==0)

print(++s2,count());

else

print(s2,s3);

       break;  

  case 'a':

append();

break;

          case 's': 

save();

break;

  case 'c':

printf("Total Number of Nodes %d ",count());

break;

  case 'i': insert(s2);

break;

case 'f':

printf("\nEnter pattern :\n");

scanf("%s",s5);

find(s5); s1=' ';

break;

case 'e' :exit(0); break;

  default :printf("\nInvalid\n");

   

        }

}

}






Post a Comment

0 Comments