C-Linked-list

//Linked-list#include#includetypedef struct _node{

int value;

struct _node *next;? //point to the next node;

} Node;

typedef struct List{

Node *head;

// Node *tail;

}List;

void add(List *plist, int number);

void print(List *plist);

void foundnum(List *plist, int number);

void delfoundednum(List *plist, int number);

int main(int arge, char const *argv[])

{

// Node * head = NULL;

List list;

list.head = NULL;

int number;

do{

scanf("%d",&number);

if(number != -1 ){

add(&list, number);

}

} while ( number != -1 );

print(&list);

scanf("%d",&number);

foundnum(&list, number);

delfoundednum(&list, number);

print(&list);

return 0;

}

// another choice:

// Node *add(Node **phead, int number)

// int main(){...head = add(&head, number);...}

void add(List *plist, int number)

{

// add to linked-list;

Node *p=(Node*)malloc(sizeof(Node));

p->value = number;

p->next = NULL;

//Finde the last;

Node *last = plist->head;

if( last ){

while( last->next ){

last = last->next;? // plist->head = plist->head->next;

} // attach;

last->next = p;? // plist->head->next = p;

}else {

plist->head=p;

}

// printf("o");

}

void print(List *plist)

{

Node *p;

for (p = plist->head; p; p = p->next){

printf("%d\t",p->value);

}

printf("\n");

}

void foundnum(List *plist, int number)

{

Node *p;

int isfound = 0;

for(p = plist->head; p; p = p->next){

if(p->value == number){

printf("isfound\n");

isfound = 1;

break;

}

}

if(!isfound)printf("isnotfound\n");

}

void delfoundednum(List *plist, int number)

{

Node *p,*q;

int isfound = 0;

for(q = NULL, p = plist->head; p; q = p, p = p->next){

if(p->value == number){

if( q ){

q->next = p->next;

}else{

plist->head=p->next;

}? ? // q = p; p = p->next; q->next = p->next; free(p);

free(p);

break;

}

// Any pointer at the left of ->must be check whether it is NULL;

}

// delete list;

// for(p=plist->head; p; p=q){

// q=p->next;

// free(p);

// }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容