{"id":209,"date":"2005-02-06T02:44:19","date_gmt":"2005-02-05T18:44:19","guid":{"rendered":"https:\/\/www.azfar.my\/2005\/02\/node\/209"},"modified":"2005-02-06T02:46:47","modified_gmt":"2005-02-05T18:46:47","slug":"data-struct-program","status":"publish","type":"post","link":"https:\/\/azfar.my\/home\/2005\/02\/data-struct-program.php","title":{"rendered":"data struct program"},"content":{"rendered":"<p>this is an old stuff. anywhoo, i got this question for my final project:<\/p>\n<blockquote><p>Menggunakan konsep baris gilir, bina program untuk operasi insert, delete, search nod dalam baris gilir tersebut. Gunakan kaedah pointer.<\/p><\/blockquote>\n<p>This program was written using a C language. So, with the help of my friend, here is the answer&#8230;<\/p>\n<p><!--more--><\/p>\n<div class=\"example\">\n#include<stdio.h><br \/>\n#include<conio.h><br \/>\n#include<stdlib.h><\/p>\n<p>struct node {<br \/>\n\tint item;<br \/>\n\tstruct node *next;<br \/>\n};<\/p>\n<p>typedef struct node Listnode;<br \/>\ntypedef Listnode *ListNodePtr;<\/p>\n<p>int isEmpty(ListNodePtr sptr)<br \/>\n{<br \/>\n\treturn sptr==NULL;<br \/>\n}<\/p>\n<p>void insert(ListNodePtr *x, int value) {<br \/>\n\tListNodePtr newptr;<br \/>\n\tnewptr=malloc(sizeof(Listnode));<br \/>\n\tnewptr->item=value;<br \/>\n\tnewptr->next=*x;<br \/>\n\t*x=newptr;<br \/>\n}<\/p>\n<p>void printlist(ListNodePtr currptr)<br \/>\n{<br \/>\n\tif(currptr==NULL)<br \/>\n\t\tprintf(&#8220;List is emptyn&#8221;);<br \/>\n\telse {<br \/>\n\t\tprintf(&#8220;The list is: &#8220;);<\/p>\n<p>\t\twhile(currptr!=NULL) {<br \/>\n\t\t\tprintf(&#8220;%d &#8211;> &#8220;, currptr->item);<br \/>\n\t\t\tcurrptr=currptr->next;<br \/>\n\t\t}<br \/>\n\t\tprintf(&#8220;NULLn&#8221;);<br \/>\n\t}<br \/>\n}<\/p>\n<p>int del(ListNodePtr *x, int value) {<br \/>\n\tListNodePtr prevptr, currptr, tempptr;<br \/>\n\tif(value==(*x)->item) {<br \/>\n\t\ttempptr=*x;<br \/>\n\t\t*x=(*x)->next;<br \/>\n\t\tfree(tempptr);<br \/>\n\t\treturn value;<br \/>\n\t}<br \/>\n\telse {<br \/>\n\t\tprevptr=*x;<br \/>\n\t\tcurrptr=(*x)->next;<\/p>\n<p>\t\twhile(currptr!=NULL &#038;&#038; currptr->item !=value) {<br \/>\n\t\t\tprevptr=currptr;<br \/>\n\t\t\tcurrptr=currptr=currptr->next;<br \/>\n\t\t}<br \/>\n\t\tif(currptr!=NULL) {<br \/>\n\t\t\ttempptr=currptr;<br \/>\n\t\t\tprevptr->next=currptr->next;<br \/>\n\t\t\tfree(tempptr);<br \/>\n\t\t\treturn value;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\treturn 0;<br \/>\n}<\/p>\n<p>int find(ListNodePtr *x, int value) {<br \/>\n\tint m=0;<br \/>\n\tListNodePtr prevptr, currptr, tempptr;<\/p>\n<p>\tif(value==(*x)->item) {<br \/>\n\t\ttempptr=*x;<br \/>\n\t\t*x=(*x)->next;<br \/>\n\t\treturn value;<br \/>\n\t}<br \/>\n\telse {<br \/>\n\t\tprevptr=*x;<br \/>\n\t\tcurrptr=(*x)->next;<\/p>\n<p>\t\twhile(currptr!=NULL &#038;&#038; currptr->item !=value) {<br \/>\n\t\t\tprevptr=currptr;<br \/>\n\t\t\tcurrptr=currptr=currptr->next;<br \/>\n\t\t\tm++;<br \/>\n\t\t}<br \/>\n\t\tif(currptr!=NULL) {<br \/>\n\t\t\treturn value;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\treturn 0;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n\tint choice, I, index;<br \/>\n\tListNodePtr head=NULL;<\/p>\n<p>\tdo {<br \/>\n\tprintf(&#8220;nMenun&#8212;-n1. Insert datan2. Delete datan3. Search datan4. Exitn&#8221;);<br \/>\n\tdo {<br \/>\n\t\tprintf(&#8220;Input: &#8220;);<br \/>\n\t\tscanf(&#8220;%d&#8221;, &#038;choice);<br \/>\n\t}<br \/>\n    while(choice<1||choice>4);<\/p>\n<p>\tswitch(choice) {<br \/>\n\tcase 1:<br \/>\n\t\tprintf(&#8220;Enter item: &#8220;);<br \/>\n\t\tscanf(&#8220;%d&#8221;, &#038;I);<br \/>\n\t\tinsert(&#038;head, I);<br \/>\n\t\tprintlist(head);<br \/>\n\t\tbreak;<\/p>\n<p>\tcase 2:<br \/>\n\t\tif(!isEmpty(head)) {<br \/>\n\t\t\tprintf(&#8220;Enter item number to be deleted: &#8220;);<br \/>\n\t\t\tscanf(&#8220;%d&#8221;, &#038;I);<\/p>\n<p>\t\t\tif(del(&#038;head, I)) {<br \/>\n\t\t\t\tprintf(&#8220;%d deletedn&#8221;, I);<br \/>\n\t\t\t\tprintlist(head);<br \/>\n\t\t\t}<br \/>\n\t\t\telse<br \/>\n\t\t\t\tprintf(&#8220;%d not foundn&#8221;, I);<br \/>\n\t\t}<br \/>\n\t\telse<br \/>\n\t\t\tprintf(&#8220;List is emptyn&#8221;);<br \/>\n\t\tbreak;<\/p>\n<p>\tcase 3:<br \/>\n\t\tif(!isEmpty(head)) {<br \/>\n\t\t\tprintf(&#8220;Enter item number as index: &#8220;);<br \/>\n\t\t\tscanf(&#8220;%d&#8221;, &#038;I);<br \/>\n\t\t\tindex=find(&#038;head, I);<br \/>\n\t\t\tif(index!=0)<br \/>\n\t\t\tprintf(&#8220;%d is in listn&#8221;, I);<br \/>\n\t\t\telse<br \/>\n\t\t\t\tprintf(&#8220;No record foundn&#8221;);<br \/>\n\t\t}<br \/>\n\t\telse<br \/>\n\t\t\tprintf(&#8220;List is emptyn&#8221;);<br \/>\n\t\tbreak;<\/p>\n<p>\tcase 4:<br \/>\n\t\tprintf(&#8220;n*** Thank you ***n&#8221;);<br \/>\n\t\tgetch();<br \/>\n\t\tbreak;<\/p>\n<p>\tdefault:<br \/>\n\t\tprintf(&#8220;n&#8212; Error &#8212;n&#8221;);<br \/>\n\t}<br \/>\n\t}<br \/>\n    while(choice!=4);<br \/>\n}\n<\/p><\/div>\n<p>or&#8230;download the program <a href=\"http:\/\/tutor.azfar.my\/cee\/datastruct\/node.c\" target=\"_blank\">here<\/a>. for those who has a question or assignment just like this, feel happy to use this. <img src=\"https:\/\/azfar.my\/home\/wp-includes\/images\/smilies\/icon_mrgreen.gif\" alt=\":mrgreen:\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/azfar.my\/home\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/azfar.my\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azfar.my\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azfar.my\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/azfar.my\/home\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":0,"href":"https:\/\/azfar.my\/home\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"wp:attachment":[{"href":"https:\/\/azfar.my\/home\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azfar.my\/home\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azfar.my\/home\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}