Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- }*head;
- int count()
- {
- struct node *temp;
- temp = head;
- int c=0;
- while(temp != NULL)
- {
- c++;
- temp = temp->next;
- }
- return c;
- }
- void display()
- {
- struct node *temp;
- temp = head;
- if(temp == NULL)
- printf("List is empty\n");
- else
- {
- while(temp != NULL)
- {
- printf("%d ", temp->data);
- temp = temp->next;
- }
- printf("\n");
- }
- }
- void push(int num)
- {
- struct node *temp;
- temp=(struct node*)malloc(sizeof(struct node));
- temp->data=num;
- if(head==NULL){
- head=temp;
- temp->next=NULL;
- }
- else{
- temp->next=head;
- head=temp;
- }
- }
- void delete()
- {
- struct node *temp;
- temp = head;
- head = temp->next;
- free(temp);
- printf("Delete successful\n");
- }
- int main()
- {
- int n, op, x;
- struct node *temp;
- while(1)
- {
- printf("\n1.Insert\n");
- printf("2.Display\n");
- printf("3.Size\n");
- printf("4.Delete\n");
- printf("5.Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &op);
- if(op == 1)
- {
- printf("Enter number to insert: ");
- scanf("%d", &n);
- push(n);
- }
- else if(op == 2)
- {
- printf("Element(s) in the list are: ");
- display();
- }
- else if(op == 3)
- {
- printf("Size of the list is: %d", count());
- }
- else if(op == 4)
- {
- if(head == NULL)
- printf("List is empty!\n");
- else
- {
- delete();
- }
- }
- else if(op == 5)
- break;
- else
- printf("Invalid option\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement