Then, the user is asked what he would like to do:
void deleteLine();
void deleteRange();
void insertLine();
void replaceRange();
void swapLines();
so far, the deleteLine() is the only one working, together with its subfunctions getline(), which gets the deleted line and removeLine() which removes the chosen line.
:chomp: I've been coding for 8 ours already. I can't figure out how to code the other functions which are:
deleteRange
(user enters a two numbers, "from" and "to" and deletes that range)
replaceRange
(user enters numbers "from" and "to" and program asks user to replace each line in the range)
insertLine
(user enters the line number after which a new line is to be inserted. Program asks the user to enter the new line to be inserted.)
swapLines
(user enters two lines numbers whose contents are to be swapped)
PHP Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct line
{
char* line;
struct line *prev, *next;
} LINE;
LINE *p;
LINE *list = NULL; // this will serve as the head of a list.
LINE *endList = NULL; // this points to the last element of a list.
LINE *newNode = NULL;
const int LINE_MAX = 80;
int c;
char *getline(LINE*head, unsigned line_no);
void removeLine(LINE*p, int line_no);
void removeRange(LINE*p, int start, int end, int rng);
void insertNode(char newline, int line_no);
void deleteLine();
void deleteRange();
void insertLine();
void replaceRange();
void swapLines();
int main()
{
printf("Pls enter any number of lines. End with a ^D/^Z:\n");
printf("\n");
char line[LINE_MAX];
char *pline = NULL;
while (fgets(line, LINE_MAX, stdin))
{
int len = strlen(line);
line[len-1] = '\0';
pline = (char *)malloc(sizeof(char) * (len + 1));
strncpy(pline, line, len);
pline[len] = '\0';
LINE *temp;
// test first if your list is non empty
if (list != NULL)
{
for (temp = list; temp->next != NULL; temp = temp->next)
;
// at this point, you're at the last element
temp->next = (LINE*)malloc(sizeof(LINE));
temp->next->line = pline;
temp->next->prev = temp;
temp->next->next = NULL;
endList = temp->next;
}
else // our list is empty
{
// temp at the last element of the list.
temp = (LINE *)malloc(sizeof(LINE));
temp->line = pline;
temp->prev = temp->next = NULL;
list = temp;
endList = temp;
}
}
printf("\nWhat would you like to do?\n");
printf("[1] Delete Line\n");
printf("[2] Delete Range\n");
printf("[3] Insert Line\n");
printf("[4] Replace Range\n");
printf("[5] Swap Lines\n");
int choice;
scanf("%d", &choice);
switch(choice){
case 1: deleteLine(); break;
case 2: deleteRange(); break;
case 3: insertLine(); break;
case 5: swapLines(); break;
default: printf("1-5"); break;
}
}
void deleteLine(){
p =list;
for(c=0; p!=NULL; p=p->next){
c++;
}
int num;
printf("\nEnter the line number to be removed\n (line number starts from 0)\n");
scanf("%d", &num);
if((num ==0) && (c == 1)){
printf("\nList is empty.\n");
abort();
}
else if ( num >= c){
printf("\nNot a valid line number.\n");
abort();
}
else{
printf("\n");
printf("\nThe line [%s] was removed", getline(list,num));
printf("\n");
printf("\nModified List:\n");
printf("\n");
}
removeLine(list,num);
for (p = list; p!= NULL; p=p->next){
printf("%s\n", p->line);
}
system("PAUSE");
}
void deleteRange(){
p =list;
for(c=0; p!=NULL; p=p->next){
c++;
}
int from,to, range;
printf("\nPlease enter the range of lines you want to remove\n\t");
printf("from:");
scanf("%d", &from);
printf("\n\tto:");
scanf("%d", &to);
range = (to-from)+1;
printf("\n");
if( (from==0) && (to==0) && (c==1) ){
printf("\nThe list is empty");
abort();
}
else if(from > c || to > c || from > to ){
printf("\nNot a valid range.");
abort();
}
else{
if(from==0){
while(from<range){
printf("Line [%s] was removed\n\t", getline(list,from));
from++;
}
}
else{
while(from<=range){
printf("Line [%s] was removed\n\t", getline(list,from));
from++;
}
}
}
removeRange(list,from,to,range);
printf("\n");
printf("\nModified List:\n");
printf("\n");
for (p = list; p!= NULL; p=p->next){
printf("%s\n", p->line);
}
system("PAUSE");
}
void insertLine(){
int num;
char newline;
char bago;
printf("Enter the number of the line after which you would like to insert an new line: ");
scanf("%d", &num);
printf("Enter the line: ");
scanf("%s", &newline);
p=list;
insertNode(newline,num);
for (p = list; p!= NULL; p=p->next){
printf("%s\n", p->line);
}
system("PAUSE");
}
void swapLines(){
int num1,num2,j,k;
LINE*p2;
printf("Enter the first line number: ");
scanf("%d", &num1);
printf("Enter the second line number: ");
scanf("%d", &num2);
for(j=0; j<num1;j++){
p=p->next;
}
for(k=0; k<num2; k++){
p2=p2->next;
}
p->prev->next=p2->next->prev;
p2=p->prev->next;
for (p = list; p!= NULL; p=p->next){
printf("%s\n", p->line);
}
system("PAUSE");
}
/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
char *getline(LINE*head, unsigned line_no){
int i=0;
for(p=head; i<line_no; i++){
p=p->next;
}
return p->line;
}
void removeLine(LINE *p, int line_no){
int j;
for (j = 0; j < line_no; j++){
p = p->next;
}
if (p->prev == NULL){
p->next->prev=NULL;
*p = *p->next;
}
else if (p->next == NULL){
p->prev->next=NULL;
}
else{
p->next->prev = p->prev;
p->prev->next = p->next;
}
}
void removeRange(LINE *p, int start, int end, int rng){
int j;
int k=0;
LINE*head;
for (j = 0; j < start; j++){
p = p->next;
}
if (p->prev == NULL){
p->next->prev=NULL;
*p = *p->next;
}
else if (p->next == NULL){
p->prev->next=NULL;
}
else{
p->next->prev = p->prev;
p->prev->next = p->next;
}
}
void insertNode(char newline, int line_no){
int i;
LINE *tempo,*t,*r;
p =list;
for(c=0; p!=NULL; p=p->next){
c++;
}
p =list;
r=p;
// here r stores the first location
if(line_no >= c || line_no <= 0){
printf("Invalid line number");
return;
}
if (line_no == 0){
tempo=(LINE *)malloc(sizeof(LINE));
//tempo->line=newline;
// IF LIST IS NULL ADD AT BEGINNING
if ( p== NULL){
p=tempo;
p->next=NULL;
}
else{
tempo->next=p;
p=tempo;
}
}
else{
for(i=0;i<line_no;i++){
t=r; //t will be holding previous value
r=r->next;
}
tempo=(LINE*)malloc(sizeof(LINE));
//tempo->line=newline;
t->next=tempo;
t=tempo;
t->next=r;
return;
}
}

