this is an old stuff. anywhoo, i got this question for my final project:
Menggunakan konsep baris gilir, bina program untuk operasi insert, delete, search nod dalam baris gilir tersebut. Gunakan kaedah pointer.
This program was written using a C language. So, with the help of my friend, here is the answer…
#include
#include
struct node {
int item;
struct node *next;
};
typedef struct node Listnode;
typedef Listnode *ListNodePtr;
int isEmpty(ListNodePtr sptr)
{
return sptr==NULL;
}
void insert(ListNodePtr *x, int value) {
ListNodePtr newptr;
newptr=malloc(sizeof(Listnode));
newptr->item=value;
newptr->next=*x;
*x=newptr;
}
void printlist(ListNodePtr currptr)
{
if(currptr==NULL)
printf(“List is emptyn”);
else {
printf(“The list is: “);
while(currptr!=NULL) {
printf(“%d –> “, currptr->item);
currptr=currptr->next;
}
printf(“NULLn”);
}
}
int del(ListNodePtr *x, int value) {
ListNodePtr prevptr, currptr, tempptr;
if(value==(*x)->item) {
tempptr=*x;
*x=(*x)->next;
free(tempptr);
return value;
}
else {
prevptr=*x;
currptr=(*x)->next;
while(currptr!=NULL && currptr->item !=value) {
prevptr=currptr;
currptr=currptr=currptr->next;
}
if(currptr!=NULL) {
tempptr=currptr;
prevptr->next=currptr->next;
free(tempptr);
return value;
}
}
return 0;
}
int find(ListNodePtr *x, int value) {
int m=0;
ListNodePtr prevptr, currptr, tempptr;
if(value==(*x)->item) {
tempptr=*x;
*x=(*x)->next;
return value;
}
else {
prevptr=*x;
currptr=(*x)->next;
while(currptr!=NULL && currptr->item !=value) {
prevptr=currptr;
currptr=currptr=currptr->next;
m++;
}
if(currptr!=NULL) {
return value;
}
}
return 0;
}
int main() {
int choice, I, index;
ListNodePtr head=NULL;
do {
printf(“nMenun—-n1. Insert datan2. Delete datan3. Search datan4. Exitn”);
do {
printf(“Input: “);
scanf(“%d”, &choice);
}
while(choice<1||choice>4);
switch(choice) {
case 1:
printf(“Enter item: “);
scanf(“%d”, &I);
insert(&head, I);
printlist(head);
break;
case 2:
if(!isEmpty(head)) {
printf(“Enter item number to be deleted: “);
scanf(“%d”, &I);
if(del(&head, I)) {
printf(“%d deletedn”, I);
printlist(head);
}
else
printf(“%d not foundn”, I);
}
else
printf(“List is emptyn”);
break;
case 3:
if(!isEmpty(head)) {
printf(“Enter item number as index: “);
scanf(“%d”, &I);
index=find(&head, I);
if(index!=0)
printf(“%d is in listn”, I);
else
printf(“No record foundn”);
}
else
printf(“List is emptyn”);
break;
case 4:
printf(“n*** Thank you ***n”);
getch();
break;
default:
printf(“n— Error —n”);
}
}
while(choice!=4);
}
or…download the program here. for those who has a question or assignment just like this, feel happy to use this.