ขอความช่วยเหลือครับ เกี่ยวกับภาษาซีครับ ต้องส่งพรุ่งนี้แล้ว ทำไม่เป็นจิงๆครับ ไม่รู้จะพึงใครจริงๆครับ
โจทย์ครับ ใช้ array 10ช่อง เก็บข้อมูลแบบ List ครับ เป็นเมนูดังนี้ครับ
1.insert 2.delete 3.print 4.exit input :
รับค่าเป็น int ครับ และข้อมูลที่รับต้องเรียงค่าจากน้อยไปมากด้วยครับ ถ้าข้อมูลเต็มแล้วจะ insert ไม่ได้อีกให้พิมพ์ว่า fullครับ
ภาพนี้ครับ insert 6 |arr1||arr2||arr3||arr3||arr4||arr5||arr6||arr7||arr8||arr9| | 6 | | | | | | | | | |
insert 3 |arr1||arr2||arr3||arr3||arr4||arr5||arr6||arr7||arr8||arr9| | 3 | 5 | | | | | | | | |
insert 5 |arr1||arr2||arr3||arr3||arr4||arr5||arr6||arr7||arr8||arr9| | 3 | 5 | 6 | | | | | | | |
ผมไปเจอโค้ดในเน็ต
#include <stdio.h> #include <stdlib.h> #include <ctype.h>
typedef struct node { int data;
struct node *ptr; } mynode;
/*---- Global var. and function ----*/ mynode *head=NULL; /* Header node */ mynode *new_node(void); mynode *find_for_insert(x,pos); mynode *find_for_delete(int); void insert(void); void delete(void); void search(void); void list(void); int max_node=0;
/*---- Main Program ----*/ void main() { char sc[20];
while (1) { /* Infinite loop */ printf("\n* Command: I)insert, S)search, L)list, D)delete, Q)quit\n"); scanf("%s",sc);
switch (toupper(sc[0])) { case 'I': insert(); break; case 'S': search(); break; case 'L': list(); break; case 'D': delete(); break; case 'Q': exit(0); } } }
/*--- Insert node ----*/ void insert() { mynode *new, *tmp; int x,pos;
while (1) { printf("Enter data for insert :\n"); scanf("%d",&x); /* input data x */ printf("Enter position for insert (0 break) :\n"); scanf("%d",&pos); /* input position for insert */ if (pos==0) break;
if ((new=new_node())==NULL) { printf("can't get new node\n"); /* Can't get new node */ return; } new->data=x; /* put x to node->data */ new->ptr=NULL; /* put NULL to node->ptr */
if (head==NULL) /* If a first node */ head=new; /* Link head to first node */ else { tmp=find_for_insert(x,pos); /* Find pos node to insert*/ if (tmp==head && tmp->data >x) { /* Insert at a head */ new->ptr=head; head=new; } else { new->ptr=tmp->ptr; /* Link to next node */ tmp->ptr=new; /* Link tail node to new */ } } } }
/*--- Delete node ----*/ void delete() { mynode *tmp, *tmp2; int x;
if (max_node==0) { printf("Is empty Linked List, can't delete\n"); return; } printf("Enter data for delete:\n"); scanf("%d",&x); /* input data x */
if ((tmp=find_for_delete(x))==NULL) printf("data %d not found, can't delete:\n"); else { if (tmp==head && x==tmp->data) { tmp2=tmp; head=tmp->ptr; /* Delete at a first node */ } else { tmp2=tmp->ptr; tmp->ptr=tmp->ptr->ptr; /* Delete other node */ } printf("deleted data=%d ptr=%9ld\n",x,(long)tmp2); free(tmp2); max_node--; /* decrease max_node */ } }
/*--- New node ----*/ mynode *new_node(void) { max_node++; /* Increase maximum node in List */
return (mynode *)malloc(sizeof(mynode)); /* get new node */ }
/*--- Find a node for insert ----*/ mynode *find_for_insert(x,pos) int x,pos; { mynode *t, *p; int k; t=p=head; while (t->ptr!=NULL) { if (t->data >x) /* Exit from loop */ break; p=t; /* Save pre-node */ t=t->ptr; /* Move to next node */
} if (t->ptr==NULL && t->data <x) /* Insert at tail */ p=t;
return p; }
/*--- Print out Linked List ----*/ void list() { mynode *t,*Average; int n=0,sum=0;
if (max_node==0) printf("Is empty Linked List\n");
t=head; /* Start at Header node */ while (t!=NULL) { printf("node#%2d data=%3d ads=%9ld\n",++n,t->data,(long)t); t=t->ptr; }
}
/*--- Find a node for delete ----*/ mynode *find_for_delete(x) int x; { mynode *t,*p;
t=p=head; while (t->ptr!=NULL) { if (t->data==x) /* found, exit from loop */ break; p=t; /* Save a pre-node */ t=t->ptr; /* Move to next node */ } if (t->data!=x) /* not found, can't delete */ p=NULL;
return p; }
/*--- Search a node ----*/ void search() { mynode *t; int x, i=0;
if (max_node==0) { printf("Is empty Linked List, can't search\n"); return; } printf("Enter data for search:\n"); scanf("%d",&x); /* input data x */
t=head; while (t->ptr!=NULL) { if (t->data==x) /* found, exit from loop */ break; t=t->ptr; /* Move to next node */ i++; /* count node# */ } if (t->data==x) /* found */ printf("Found %d in node#%d\n",t->data,i+1); else printf("Not found %d\n",x); }
มันมีส่วนคล้ายๆครับ ช่วยแก้ให้ผมหน่อยนะครับ ผมต้องส่งแล้วครับ วันศุกร์พรุ่งนี้ครับ ขอบพระคุณมากๆนะครับ
|