#include <stdio.h>
#include <stdlib.h>
typedef struct node node;
struct node {
int value;
node* next;
};
node *getnode();
void addfirst(node**,int);
void print(node *);
void insert(node **, int, int);
#include "list.h"
int main()
{
node *list;
insert(&list, 0, 5);
insert(&list, 1, 10);
insert(&list, 2, 15);
insert(&list, 3, 20);
print(list);
return 0;
}
node *getnode()
{
node* temp;
temp = (node*) malloc( sizeof(node) );
if( temp != NULL )
return temp;
abort();
}
void addfirst( node **ptr, int val )
{
node *temp = getnode();
temp -> value = val;
temp -> next = *ptr;
(*ptr) = temp;
}
void print( node *ptr )
{
while( ptr )
{
printf("%d\n", ptr -> value );
ptr = ptr -> next;
}
}
void insert(node **ptr, int index, int val)
{
if(index == 0)
{
addfirst(ptr, val);
}
else
{
node *temp = getnode();
temp -> value = val;
int j;
for(j=1; j < index; j++)
{
(*ptr) = (*ptr) -> next;
}
temp -> next = (*ptr) -> next;
(*ptr) -> next = temp;
}
}
/*//////////////////////////////
Structure In C
*///////////////////////////////
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define TRUE 1
#define NIL (struct Employee *)NULL
struct Employee {
char name[30] ;
int ID ;
char JobTittle[30] ;
char Addres[30];
struct Employee *next ;
} ;
struct Employee *first , *last , *node ;
void enter ();
void list ();
void del ();
void FindByID();
int main ()
{
char s[10] ;
first = NIL ;
while (TRUE) {
printf("E:\tEnter New Employee\n");
printf("R:\tRemove a Employee\n");
printf("L:\tList All Of The Employee\n");
printf("F:\tFind Employee By ID\n");
printf("Q:\tQuit The Program\n") ;
printf("Make Your Choice : ");
printf("( E R L F Q) : ") ;
gets(s) ;
*s = toupper(*s) ;
switch (*s) {
case 'E' :
enter() ;
break ;
case 'L' :
list() ;
break ;
case 'R' :
del() ;
break ;
case 'F':
FindByID();
break;
case 'Q' :
exit(0) ;
}
}
return 0;
}
//********************
void enter ()
{
char numstr[30] ;
node = (struct Employee *)malloc(sizeof(struct Employee));
node -> next = NIL ;
if (first == NIL)
first = last = node ;
else {
last -> next = node ;
last = node ;
}
printf("\n Enter name of Employee:");
gets(last -> name) ;
printf("\n Enter Employee ID :");
gets(numstr) ;
last -> ID = atoi(numstr) ;
printf("\n Enter Employee JobTittle :");
gets(last -> JobTittle) ;
printf("\n Enter Addres :");
gets(last -> Addres) ;
}
//********************
void list ( )
{
printf("\n\n\n\n");
int i ;
if (first == NIL) {
printf("\n<< the list is empty .>>") ;
getch() ;
return ;
}
last = first ;
printf("\t\tName\t\tNumber\t\tJobTittle\tAddres\n");
printf("\t\t----\t\t------\t\t--------\t---------") ;
i = 6 ;
printf("\n\n");
do {
printf("\t\t");
printf("%s", last -> name) ;
printf("\t\t");
printf("%d", last -> ID) ;
printf("\t\t");
printf("%s", last -> JobTittle) ;
printf("\t\t");
printf("%s",last -> Addres);
i ++ ;
last=last -> next ;
printf("\n\n");
} while (last != NIL) ;
printf("\n\n\n\n");
printf("\t\t********************************************") ;
printf("\npress a key to continue...");
getch() ;
printf("\n\n");
}
//*******************
void del ()
{
int stnumber ;
printf("Enter Employee ID for delete:");
scanf("%d", &stnumber) ;
last = node = first ;
while (last != NIL)
{
if (last -> ID!=stnumber) {
node = last ;
last = last -> next ;
continue ;
}
else
{
if (last == first)
{
first=last -> next ;
free(last) ;
free(node) ;
break ;
}
else
{
node -> next=last -> next;
free(last) ;
break ;
}
}
}
}
//*******************
void FindByID()
{
int stnumber ;
if (first == NIL) {
printf("\n<< the list is empty .>>") ;
getch() ;
return ;
}
last = first ;
printf("\n\nEnter Employee ID To Search It : ");
scanf("%d", &stnumber) ;
printf("\n\n\t\tName\t\tNumber\t\tJobTittle\tAddres\n");
printf("\t\t----\t\t------\t\t--------\t---------") ;
printf("\n\n");
do
{
if (last -> ID==stnumber)
{
printf("\t\t");
printf("%s", last -> name) ;
printf("\t\t");
printf("%d", last -> ID) ;
printf("\t\t");
printf("%s", last -> JobTittle) ;
printf("\t\t");
printf("%s",last -> Addres);
printf("\n\n");
}
last=last -> next ;
} while (last != NIL) ;
printf("\n\n\n\n");
printf("\t\t********************************************") ;
printf("\npress a key to continue...");
getch() ;
printf("\n\n");
}
void insert(node **head, int index, int val)
{
node *ptr = (*head);
if(index == 0)
{
addfirst(head, val);
}
else
{
node *temp = getnode();
temp -> value = val;
int j;
for(j=1; j < index; j++)
{
ptr = ptr -> next;
}
temp -> next = ptr -> next;
ptr -> next = temp;
}
}
خیلی ممنون به خاطر اون برنامه. توی ++c با کلاسها میشه خیلی راحت لیست رو پیاده سازی کرد. حتی یه پیاده سازی از لیست دو طرفه هم توی کتابخانه stl وجود داره.
اون برنامه به نظر من چند تا اشکال داره:
اول اینکه اون برنامه از متغیرهای سراسری استفاده میکنه که این خوب نیست. بهتره اطلاعات رو از طریق پارامترها به توابع بفرستیم که کد پرتابل تر بشه و اشکالزدایی هم راحتتر بشه (تا جایی که ممکنه نباید از متغیر سراسری استفاده کرد)
دوم اینکه اون برنامه طوری نوشته شده که بتونه با کاربر به صورت تعاملی رفتار کنه. اما معمولا لیست پیوندی باید توسط برنامه نویس استفاده بشه.
من تابع ()insert رو بدین صورت بازنویسی کردم و همه چیز درست شد:کد: [انتخاب]void insert(node **head, int index, int val)
{
node *ptr = (*head);
if(index == 0)
{
addfirst(head, val);
}
else
{
node *temp = getnode();
temp -> value = val;
int j;
for(j=1; j < index; j++)
{
ptr = ptr -> next;
}
temp -> next = ptr -> next;
ptr -> next = temp;
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node node;
struct node {
int value;
node* next;
};
node *getnode();
void addfirst(node**, int);
int getfirst(node**);
void print(node *);
int get(node*, int);
void deletelist(node**);
void insert(node **, int, int);
void del(node **, int);
int search(node *, int val);
node *
getnode()
{
node* temp;
temp = (node*) malloc( sizeof(node) );
if( temp != NULL )
{
temp -> next = NULL;
return temp;
}
exit(EXIT_FAILURE);
}
void
addfirst( node **ptr,
int val )
{
node *temp = getnode();
temp -> value = val;
temp -> next = *ptr;
(*ptr) = temp;
}
int
getfirst(node **ptr)
{
node *temp = *ptr;
*ptr = (*ptr) -> next;
free(*ptr);
return temp -> value;
}
void
print( node *ptr )
{
while( ptr )
{
printf("%d\n", ptr -> value );
ptr = ptr -> next;
}
}
int
get(node *head,
int i)
{
node *ptr = head;
while( --i )
{
ptr = ptr -> next;
}
return (ptr -> value);
}
void
deletelist( node **head )
{
node *ptr = *head;
node *next;
while( ptr )
{
next = ptr -> next;
free(ptr);
ptr = next;
}
}
void
insert(node **head,
int index,
int val)
{
node *ptr = (*head);
if(index == 0)
{
addfirst(head, val);
}
else
{
node *temp = getnode();
temp -> value = val;
int j;
for(j=1; j < index; j++)
{
ptr = ptr -> next;
}
temp -> next = ptr -> next;
ptr -> next = temp;
}
}
void
del( node **head,
int index )
{
if( index == 0 )
{
node *ptr = (*head);
(*head) = ptr -> next;
free(ptr);
}
else
{
node *preptr = (*head);
node *ptr = preptr -> next;
int j;
for(j=1; j < index; j++)
{
preptr = preptr -> next;
ptr = preptr -> next;
}
preptr -> next = ptr -> next;
free(ptr);
}
}
int
search(node *head,
int val)
{
int i = 0;
node *ptr = head;
while( ptr )
{
if( (ptr -> value) == val )
return i;
i++;
ptr = ptr -> next;
}
return (-1);
}
node *list1 = getnode()
void addfirst(node **head, int value);
int getfirst(node **head);
void print(node *head);
int get(node *head, int index);
void deletelist(node **head);
void insert(node **head, int index, int val);
void del(node **head, int index);
int search(node *head, int val);