LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2003, 04:57 AM   #1
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Rep: Reputation: 15
SaTaN please 3 programs to check


i have finished 3 programs singly- doubly-, circular linked lists. if u know any better wy to implement pls send them to me
 
Old 10-20-2003, 04:57 AM   #2
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
/*--------------------------------------------------main-----------------------------------------*/

main()
{
int num,pos,i,j,size,choice, check;
char option;
List *new;
ListElmt *elm,*buffer;
list_size(new) = 0;

printf("\nIMPLEMENTATION OF A SINGLY - LINKED LIST\n\n");

start: printf("\n****************MENU**************\n");
printf("\n1. Initialize and create a new list");
printf("\n2. Insert an element in the list");
printf("\n3. Delete an element from the list");
printf("\n4. Display the list");
printf("\n5. Destroy the list");
printf("\n6. Exit");
printf("\n\n Enter your choice : ");
scanf("%d", &choice);
printf("\n");

if(choice>=6)
return -1;

if(choice!=1 && list_size(new)==0)
{
printf("Unless you create a list you cannot perform this operation.\n");
goto start;
}

switch(choice)
{

case 1:

if(list_size(new)>0)
{
printf("A list is currently under operation. You have to destroy it before creating a new list. To destroy a list select Destroy the list option in the menu.\n");
goto start;
}

new =malloc(sizeof(List));
list_init(new,data_destroy);
printf("Enter the head element : ");
scanf("%d",&num);
list_ins_next(new, NULL, num);
printf("The elements added next will be included at the tail of list until you enter -1.");
while(1)
{
printf("Enter the element : ");
scanf("%d",&num);
if(num==-1)
break;
list_ins_next(new,list_tail(new),num);
}
goto start;

case 2:

printf("Enter the new element : ");
scanf("%d",&num);

printf("Insert as head? (enter yes/no) : ");
scanf("%s", &option);
if(strcmp(&option, "yes")==0)
{
list_ins_next(new, NULL, num);
printf("The element is inserted as head.\n");
goto start;
}

printf("Insert the new element after : ");
scanf("%d", &pos);

check = 0;
elm=new->head;
size=list_size(new);
for(i=0;i<size;i++)
{
if(list_data(elm)==pos)
{
check = 1;
buffer=list_next(elm);
list_ins_next(new,elm,num);
break;
}
elm=list_next(elm);
}
if(check == 0)
printf("The specified element is not found in the list.\n");
else
printf("The element is inserted as specified.");
goto start;

case 3:

printf("Delete the head element? (enter yes/no) : ");
scanf("%s", &option);
if(strcmp(&option, "yes")==0)
{
list_rem_next(new, NULL, &num);
printf("The head element is deleted.\n");
goto start;
}

printf("Delete the element present after: ");
scanf("%d", &pos);
num=0; check=0;
elm=new->head;
size=list_size(new);
for(i=0;i<size;i++)
{
if(list_data(elm)==pos)
{
check=1;
buffer=list_next(elm);
list_rem_next(new,elm,&num);
break;
}
elm=list_next(elm);
}

if(check == 0)
printf("The specified element is not found in the list.\n");
else
printf("The element is deleted as specified.\n");
goto start;

case 4:

printf("The current list is :\n");
display(new);
printf("\n");
goto start;

case 5:

list_destroy(new);
printf("The list is destroyed\n");
goto start;

}

}
 
Old 10-20-2003, 04:58 AM   #3
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
/*dlist.c*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dlist.h"

void dlist_init(DList *list, void (*destroy)(void *data)) {

list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;

return;

}

void dlist_destroy(DList *list) {

void *data;

while (dlist_size(list) > 0) {

if(dlist_remove(list, dlist_tail(list), (void **)&data) == 0 && list->destroy != NULL) {

list->destroy(data);

}
}

memset(list, 0, sizeof(DList));

return;

}

int dlist_ins_next(DList *list, DListElmt *element, const void *data) {

DListElmt *new_element;

if (element == NULL && dlist_size(list) != 0)
return -1;

if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL)
return -1;

new_element->data = (void *)data;

if (dlist_size(list) == 0) {

list->head = new_element;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_element;

}

else {

new_element->next = element->next;
new_element->prev = element;

if (element->next == NULL)
list->tail = new_element;
else
element->next->prev = new_element;

element->next = new_element;

}

list->size++;

return 0;

}

int dlist_ins_prev(DList *list, DListElmt *element, const void *data) {

DListElmt *new_element;

if (element == NULL && dlist_size(list) != 0)
return -1;

if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL)
return -1;

new_element->data = (void *)data;

if (dlist_size(list) == 0) {

list->head = new_element;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_element;

}

else {

new_element->next = element;
new_element->prev = element->prev;

if (element->prev == NULL)
list->head = new_element;
else
element->prev->next = new_element;

element->prev = new_element;

}

list->size++;

return 0;

}

int dlist_remove(DList *list, DListElmt *element, void **data) {

if (element == NULL || dlist_size(list) == 0)
return -1;

*data = element->data;

if (element == list->head) {

list->head = element->next;

if (list->head == NULL)
list->tail = NULL;
else
element->next->prev = NULL;

}

else {

element->prev->next = element->next;

if (element->next == NULL)
list->tail = element->prev;
else
element->next->prev = element->prev;

}

free(element);

list->size--;

return 0;

}


void display(DList *list)

{

DListElmt *element;

printf("%d", list->head->data);
for(element = dlist_next(list->head); element != NULL; element = dlist_next(element))
printf(" <-> %d",element->data);
}

void data_destroy(void *data)
{

free(&data);

}
 
Old 10-20-2003, 04:59 AM   #4
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
/*dlist.c main*/
/*--------------------------------------------------main-----------------------------------------*/

main()
{
int num,pos,i,j,size,choice, check;
char option;
DList *new;
DListElmt *elm,*buffer;
dlist_size(new) = 0;

printf("\nIMPLEMENTATION OF A DOUBLY - LINKED LIST\n\n");

start: printf("\n****************MENU**************\n");
printf("\n1. Initialize and create a new list");
printf("\n2. Insert an element in the list");
printf("\n3. Delete an element from the list");
printf("\n4. Display the list");
printf("\n5. Destroy the list");
printf("\n6. Exit");
printf("\n\n Enter your choice : ");
scanf("%d", &choice);
printf("\n");

if(choice>=6)
return -1;

if(choice!=1 && dlist_size(new)==0)
{
printf("Unless you create a list you cannot perform this operation.\n");
goto start;
}

switch(choice)
{

case 1:

if(dlist_size(new)>0)
{
printf("A list is currently under operation. You have to destroy it before creating a new list. To destroy a list select Destroy the list option in the menu.\n");
goto start;
}

new =malloc(sizeof(DList));
dlist_init(new,data_destroy);

printf("Enter the head element : ");
scanf("%d",&num);
dlist_ins_prev(new, NULL, num);
printf("The elements added next will be included at the tail of list until you enter -1.");
while(1)
{
printf("Enter the element : ");
scanf("%d",&num);
if(num==-1)
break;
dlist_ins_next(new,dlist_tail(new),num);
}
elm=new->head;
goto start;

case 2:

check = 0;
printf("Enter the new element : ");
scanf("%d",&num);

printf("1. Insert the new element 1/2 (1=after 2=before)\n");
printf(" Enter your choice : ");
inse: scanf("%d", &choice);

if(choice == 1)
printf("Insert %d after : ",num);
else
if(choice == 2)
printf("Insert %d before : ",num);
else
{
printf("No such choice. Enter choice again : ");
goto inse;

}
scanf("%d", &pos);


elm=new->head;
size=dlist_size(new);
for(i=0;i<size;i++)
{
if(dlist_data(elm)==pos)
{
check = 1;
buffer=dlist_next(elm);
if(choice == 1)
dlist_ins_next(new,elm,num);
else
dlist_ins_prev(new,elm,num);
break;
}
elm=dlist_next(elm);
}

if(check == 0)
printf("The specified element is not found in the list.\n");
else
printf("The element is inserted as specified.\n");
goto start;

case 3:

printf("Delete the element : ");
scanf("%d", &num);
check=0;
elm=new->head;
size=dlist_size(new);
for(i=0;i<size;i++)
{
if(dlist_data(elm)==num)
{
check=1;
buffer=dlist_next(elm);
dlist_remove(new,elm,&num);
break;
}
elm=dlist_next(elm);
}
if(check==0)
printf("The specified element is not found in the list.\n");
else
printf("The element is deleted as specified.\n");
goto start;

case 4:

printf("The current list is :\n");
display(new);
printf("\n");
goto start;

case 5:

dlist_destroy(new);
printf("The list is destroyed");
goto start;

}

}
 
Old 10-20-2003, 05:00 AM   #5
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
/*clist.c*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "clist.h"

void clist_init(CList *list, void (*destroy)(void *data)) {

list->size = 0;
list->destroy = destroy;
list->head = NULL;

return;

}

void clist_destroy(CList *list) {

void *data;

while (clist_size(list) > 0) {

if(clist_rem_next(list, list->head, (void **)&data) == 0 && list->destroy != NULL) {

list->destroy(&data);

}
}

memset(list, 0, sizeof(CList));

return;

}

int clist_ins_next(CList *list, CListElmt *element, const void *data) {

CListElmt *new_element;

if ((new_element = (CListElmt *)malloc(sizeof(CListElmt))) == NULL)
return -1;

new_element->data = (void *)data;

if (clist_size(list) == 0) {

new_element->next = new_element;
list->head = new_element;

}
else {

new_element->next = element->next;
element->next = new_element;

}

list->size++;

return 0;

}

int clist_rem_next(CList *list, CListElmt *element, void **data) {

CListElmt *old_element;

if (clist_size(list) == 0)
return -1;

*data = element->next->data;

if (element->next == element) {

old_element = element->next;
list->head = NULL;

}

else {

old_element = element->next;
element->next = element->next->next;

if (old_element == list->head) //set the starting point(head) only for display
list->head = element->next;

}

free(old_element);

list->size--;

return 0;

}


void display(CList *list)

{

CListElmt *element;

printf("-> %d ->", list->head->data);
for(element=clist_next(list->head); element!=list->head; element=clist_next(element))
printf(" %d ->",element->data);
}

void data_destroy(void *data)
{

free(&data);

}
 
Old 10-20-2003, 05:00 AM   #6
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
/*clist.c main*/
/*--------------------------------------------------main-----------------------------------------*/

main()
{
int num,pos,i,j,size,choice, check;
char option;
CList *new;
CListElmt *elm,*buffer;
clist_size(new) = 0;

printf("\nIMPLEMENTATION OF A CIRCULAR - LINKED LIST\n\n");

start: printf("\n****************MENU**************\n");
printf("\n1. Initialize and create a new list");
printf("\n2. Insert an element in the list");
printf("\n3. Delete an element from the list");
printf("\n4. Display the list");
printf("\n5. Destroy the list");
printf("\n6. Exit");
printf("\n\n Enter your choice : ");
scanf("%d", &choice);
printf("\n");

if(choice>=6)
return -1;

if(choice!=1 && clist_size(new)==0)
{
printf("Unless you create a list you cannot perform this operation.\n");
goto start;
}

switch(choice)
{

case 1:

if(clist_size(new)>0)
{
printf("A list is currently under operation. You have to destroy it before creating a new list. To destroy a list select Destroy the list option in the menu.\n");
goto start;
}

new =malloc(sizeof(CList));
clist_init(new,data_destroy);
printf("Enter the head element : ");
scanf("%d",&num);
clist_ins_next(new, NULL, num);
printf("The elements added next will be included next to the previous element until you enter -1.\n");
elm=new->head;
while(1)
{
printf("Enter the element : ");
scanf("%d",&num);
if(num==-1)
break;
clist_ins_next(new,elm,num);
elm = elm->next;
}
goto start;

case 2:

printf("Enter the new element : ");
scanf("%d",&num);

printf("Insert the new element after : ");
scanf("%d", &pos);

check = 0;
elm=new->head;
size=clist_size(new);
for(i=0;i<size;i++)
{
if(clist_data(elm)==pos)
{
check = 1;
buffer=clist_next(elm);
clist_ins_next(new,elm,num);
break;
}
elm=clist_next(elm);
}
if(check == 0)
printf("The specified element is not found in the list.\n");
else
printf("The element is inserted as specified.");
goto start;

case 3:

printf("Delete the element present after: ");
scanf("%d", &pos);
num=0; check=0;
elm=new->head;
size=clist_size(new);
for(i=0;i<size;i++)
{
if(clist_data(elm)==pos)
{
check=1;
buffer=clist_next(elm);
clist_rem_next(new,elm,&num);
break;
}
elm=clist_next(elm);
}

if(check == 0)
printf("The specified element is not found in the list.\n");
else
printf("The element is deleted as specified.\n");
goto start;

case 4:

printf("The current list is :\n");
display(new);
printf("\n");
goto start;

case 5:

clist_destroy(new);
printf("The list is destroyed\n");
goto start;

}

}
 
Old 10-20-2003, 05:01 AM   #7
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
/*dlist.h*/

#ifndef DLIST_H
#define DLIST_H

#include<stdlib.h>

typedef struct DListElmt_ {

void *data;
struct DListElmt_ *prev;
struct DListElmt_ *next;

} DListElmt;

typedef struct DList_ {

int size;

int (*match) (const void *key1, const void key2);
void (*destroy) (void *data);

DListElmt *head;
DListElmt *tail;

}DList;

void dlist_init(DList *list, void(*destroy) (void *data));

void dlist_destroy(DList *list);

int dlist_ins_next(DList *list, DListElmt *element, const void *data);

int dlist_ins_prev(DList *list, DListElmt *element, const void *data);

int dlist_remove(DList *list, DListElmt *element, void **data);

#define dlist_size(list) ((list)->size)

#define dlist_head(list) ((list)->head)

#define dlist_tail(list) ((list)->tail)

#define dlist_is_head(element) ((element)->prev == NULL ? 1 : 0)

#define dlist_is_tail(element) ((element)->next == NULL ? 1 : 0)

#define dlist_data(element) ((element)->data)

#define dlist_next(element) ((element)->next)

#define dlist_prev(element) ((element)->prev)

#endif



/*clist.h*/

#ifndef CLIST_H
#define CLIST_H

#include<stdlib.h>

typedef struct CListElmt_ {

void *data;
struct CListElmt_ *next;

} CListElmt;

typedef struct CList_ {

int size;

int (*match) (const void *key1, const void key2);
void (*destroy) (void *data);

CListElmt *head;

} CList;

void clist_init(CList *list, void(*destroy) (void *data));

void clist_destroy(CList *list);

int clist_ins_next(CList *list, CListElmt *element, const void *data);

int clist_rem_next(CList *list, CListElmt *element, void **data);

#define clist_size(list) ((list)->size)

#define clist_head(list) ((list)->head)

#define clist_data(element) ((element)->data)

#define clist_next(element) ((element)->next)

#endif
 
Old 10-20-2003, 07:44 AM   #8
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Why did you post these programs on this forum ???You could have sent them as a mail.

I don't think I have time to check your programs.

Moreover since you have done it successfully .... it won't be necessary.

Plz don't flood my mail box unless it is necessary.
 
Old 10-20-2003, 10:42 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
When posting code, please use code-tags, to preserve indention and spacing.
 
Old 10-20-2003, 11:41 AM   #10
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
or if dealing with much more than say 50 lines, then either mail your code to a person or host it somewhere for us to look at
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wanna Hear Satan? masonm General 4 09-24-2005 11:45 PM
is there a way to check which programs are installing in linux? mickeyren Linux - Software 7 02-24-2005 10:49 AM
Program like Satan Drunkalot Linux - Networking 1 08-31-2004 08:20 AM
I was a Tool of Satan macewan General 14 01-15-2004 03:00 PM
CD Burner possessed by the Dark Spawn of Satan Brane Ded Linux - Hardware 5 12-03-2003 12:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration