Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- /*
- For a linked list of integers, create a user-friendly, men-driven program that performs the following
- operations.
- (a) Create a list of integers. //checked
- (b) Insert an integer at the end of a list.
- (c) Search for an integer. //checked
- (d) Delete an integer.
- (e) Print out a list. //checked
- */
- typedef struct list
- {
- int Data ;
- struct list *link ;
- }Node ;
- Node *Create_list(Node *N,int num)
- {
- Node *New_Node ;
- New_Node = malloc( sizeof( Node) );
- New_Node->Data = num ;
- New_Node->link = N ;
- return New_Node ;
- }
- void printList(Node *first)
- {
- printf("The list contains: ");
- for (; first; first = first->link)
- printf("%4d", first->Data);
- printf("\n");
- }
- int search(Node *first,int n)
- {
- Node *t;
- t = malloc( sizeof( Node) );
- t = first ;
- while(t->link != NULL)
- {
- if(t->Data == n)
- {
- //printf("integer %d is found\n",n);
- return 1;
- break;
- }
- t = t->link ;
- }
- return -1;
- }
- void End_insert(Node **first,int num)
- {
- Node *New_Node ;
- New_Node = malloc( sizeof( Node) );
- New_Node->Data = num ;
- New_Node->link = NULL ;
- for(; *first != NULL ; *first = *first->link)
- {
- if(*first->link == NULL)
- *first->link = New_Node ;
- }
- }
- /*
- void delete(Node **first,int d)
- {
- Node* trail , *x ;
- trail = first ;
- x = trail->link ;
- for(; trail != NULL ; trail = trail->link)
- {
- if(trail->Data == d)
- }
- if(x->Data == d)
- {
- trail->link = x->link ;
- free(x);
- }
- else if(first->Data == d)
- {
- }
- }
- */
- int main(int argc, char *argv[])
- {
- Node *head , *ptr , *end;
- int DataArray[5] = { 1, 42 , 37 ,98 ,65};
- int i,j;
- head = NULL ;
- end = Create_list(end,5);
- // head = malloc( sizeof(Node) );
- for(i=4 ; i>=0 ; i--)
- head = Create_list(head,DataArray[i]);
- scanf("%d",&j);
- if( search(head,j) == 1)
- printf("integer %d is found\n",j);
- else
- printf("integer is not found\n");
- End_insert(&head,9);
- ptr = head ;
- printList(head);
- while(ptr != NULL)
- {
- printf("data = %d \n",ptr->Data);
- ptr = ptr->link ;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment