Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- struct node {
- int data;
- struct node *next;
- }*start;
- void create(int d) {
- struct node *q,*temp;
- temp= (struct node* )malloc(sizeof(struct node));
- temp->data=d;
- temp->next=NULL;
- if(start==NULL) /*If list is empty */
- start=temp;
- else { /*Element inserted at the end */
- q=start;
- while(q->next!=NULL)
- q=q->next;
- q->next=temp;
- }
- } //End of create()
- void add_beg(int d) {
- struct node *temp;
- temp=(struct node* )malloc(sizeof(struct node));
- temp->data=d;
- temp->next=start;
- start=temp;
- }/*End of add_beg()*/
- void add_after(int d,int pos) {
- struct node *temp,*q;
- int i;
- q=start;
- for(i=0;i<pos-1;i++) {
- q=q->next;
- if(q==NULL) {
- printf("There are less than %d elements",pos);
- return;
- }
- }
- temp=(struct node* )malloc(sizeof(struct node) );
- temp->next=q->next;
- temp->data=d;
- q->next=temp;
- }//End of add_after()
- void del(int d) {
- struct node *temp,*q;
- if(start->data == d) {
- temp=start;
- start=start->next; /*First element deleted*/
- return;
- }
- q=start;
- while(q->next->next != NULL) {
- if(q->next->data==d) /*Element deleted in between*/
- {
- temp=q->next;
- q->next=temp->next;
- return;
- }
- q=q->next;
- }/*End of while */
- if(q->next->data==d) /*Last element deleted*/
- {
- temp=q->next;
- q->next=NULL;
- return;
- }
- printf("Element %d not found\n",d);
- }//End of del()
- void display() {
- struct node *q;
- if(start == NULL) {
- printf("List is empty\n");
- return;
- }
- q=start;
- printf("List is :\n");
- while(q!=NULL) {
- printf("%d ", q->data);
- q=q->next;
- }
- printf("\n");
- }//End of display()
- void count() {
- struct node *q=start;
- int cnt=0;
- while(q!=NULL) {
- q=q->next;
- cnt++;
- }
- printf("Number of elements are %d\n",cnt);
- }//End of count()
- void main() {
- int choice,n,e,position,i;
- start=NULL;
- while(1) {
- printf("1.Create a linked List\n");
- printf("2.Add at begining\n");
- printf("3.Add after \n");
- printf("4.Delete\n");
- printf("5.Display\n");
- printf("6.Count\n");
- printf("7.Quit\n");
- printf("Enter your choice : ");
- scanf("%d",&choice);
- switch(choice) {
- case 1:printf("How many nodes you want in the list: ");
- scanf("%d",&n);
- for(i=0;i<n;i++) {
- printf("Enter the element : ");
- scanf("%d",&e);
- create(e);
- }
- break;
- case 2:
- printf("Enter the element : ");
- scanf("%d",&e);
- add_beg(e);
- break;
- case 3:
- printf("Enter the element : ");
- scanf("%d",&e);
- printf("Enter the position after which this element is inserted : ");
- scanf("%d",&position);
- add_after(e,position);
- break;
- case 4:if(start==NULL)
- {
- printf("List is empty\n");
- continue;
- }
- printf("Enter the element for deletion : ");
- scanf("%d",&e);
- del(e);
- break;
- case 5:display();
- break;
- case 6:count();
- break;
- case 7: exit(0);
- default:
- printf("Wrong choice\n");
- } //End of switch
- }//End of while
- }//End of main()
Advertisement
Add Comment
Please, Sign In to add comment