Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************
- ** @author firoz **
- ** **
- ** **
- ** Description: A program to: **
- ** **
- ** 1)INSERT **
- ** at first position, last position and arbitrary position. **
- ** 2)TRAVERSE **
- ** 3)DELETE **
- ** at first position, last position and arbitrary position. **
- ** 4)SORT **
- ** **
- ** the elements in a LINKED LIST. **
- *******************************************************************/
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int data;
- struct node *next;
- } *Node;
- /////////////////////////////
- Node insertFront( Node head )
- {
- Node temp;
- char choice;
- do{
- printf("\nEnter the Element in the linked list:\n");
- temp=(Node)malloc(sizeof(struct node));
- scanf("%d",&temp->data);
- temp->next=head;
- head=temp;
- printf("%d Entered at front\nWanna insert more data in the front?(Y/N) :",head->data);
- scanf("\n %c",&choice);
- }while(choice=='y'||choice=='Y');
- return head;
- }
- //////////////////////////////
- void traverse(Node head){
- Node temp;
- temp = head;
- printf("\n***Traversing the Linked List:***\n");
- while(temp!=NULL){
- printf("%d-> ",temp->data);
- temp=temp->next;
- }
- printf("NULL\n");
- }
- //////////////////////////
- Node insertBack(Node head){
- Node temp, tail;
- char choice;
- tail = head;
- if(head==NULL){
- printf("Linked list is empty, therefore inserting the node at first position:\n");
- head=insertFront(head);
- }
- else{
- while(tail->next!=NULL)
- tail=tail->next;
- do{
- temp=(Node)malloc(sizeof(struct node));
- printf("\nEnter Element of Linked list\n");
- scanf("%d",&temp->data);
- temp->next=NULL;
- tail->next=temp;
- tail=tail->next;
- printf("%d is inserted at end\nWanna Insert more data at end(Y/N)?",tail->data);
- scanf("\n%c",&choice);
- }while(choice=='y'||choice=='Y');
- }
- return head;
- }
- /////////////////////////////
- Node insertPosition(Node head){
- int i, n;
- char choice;
- Node temp, position;
- do{
- position=head;
- printf("Enter the position at which you want to insert the data\n");
- scanf("%d",&n);
- if(n==1)
- head=insertFront(head);
- else{
- for(i=2; i<n && position!=NULL ; i++)
- position=position->next;
- if(position==NULL){
- printf("Invalid position for insertion !\n Press Enter to Continue.....\t");
- fflush(stdin);
- getchar();
- getchar();
- }
- else{
- temp=(Node)malloc(sizeof(struct node));
- printf("Enter the data to be inserted at position %d:\t",n);
- scanf("%d",&temp->data);
- temp->next = position -> next;
- position->next = temp;
- printf("%d is inserted at position %d",position->next->data, n);
- }
- }
- printf("\nDo wanna insert more data at arbitary position?(Y/N)\n");
- scanf("\n%c",&choice);
- }while(choice=='y'||choice=='Y');
- return head;
- }
- //////////////////////
- Node deleteFront(Node head){
- Node temp;
- temp=head;
- head=temp->next;
- free(temp);
- return head;
- }
- //////////////////////
- Node deleteBack(Node head){
- Node temp, old_temp;
- temp=head;
- old_temp=temp;
- while(temp->next!=NULL){
- old_temp=temp;
- temp=temp->next;
- }
- if(head->next==NULL)
- head=NULL;
- old_temp->next=NULL;
- free(temp);
- return head;
- }
- /////////////////////
- Node deletePosition(Node head,int n){
- Node temp, old_temp;
- temp=head;
- old_temp=temp;
- int i;
- if(n==1){
- head=deleteFront(head);
- printf("data at position %d is deleted\n",n);
- }
- else{
- for(i=1;i<n && temp!=NULL;i++){
- old_temp=temp;
- temp=temp->next;
- }
- if(temp==NULL){
- printf("Invalid position for deletion!\n");
- }
- else{
- old_temp->next=temp->next;
- free(temp);
- printf("data at position %d is deleted\n",n);
- }
- }
- return head;
- }
- /////////////////////////
- void sort(Node head){
- int temp=0;
- Node temp1, temp2;
- for( temp1 = head; temp1 != NULL; temp1 = temp1->next)
- for(temp2=temp1->next; temp2 != NULL; temp2 = temp2->next)
- if( temp1->data > temp2->data){
- temp = temp1->data;
- temp1->data = temp2->data;
- temp2->data = temp;
- }
- printf("***Now the linked list is sorted***\nThe Sorted list is::\n");
- traverse(head);
- }
- int main()
- {
- int select, n;
- Node head=NULL;
- char choice;
- printf("\n\n***Hi! Welcome to the program of making linked list***\n");
- do{
- printf("Press 1 to Insert:\n");
- printf("Press 2 to Traverse:\n");
- printf("Press 3 to Delete:\n");
- printf("Press 4 to Sort:\n");
- scanf("%d",&select);
- if(select==1){
- //code for insert
- printf("\nPress 1 to Insert at Front:\n");
- printf("Press 2 to Insert at Back:\n");
- printf("Press 3 to Insert at Specific position:\n");
- scanf("%d",&n);
- switch(n){
- case 1:
- //insert at front
- head=insertFront(head);
- break;
- case 2:
- //insert at back
- head=insertBack(head);
- break;
- case 3:
- //insert at specific position
- head=insertPosition(head);
- break;
- default :
- printf("Wrong choice\n");
- }
- }
- else if(select==2){
- //code for Traverse
- traverse(head);
- }
- else if(select==3){
- if(head!=NULL){
- //cade for Delete
- printf("\nPress 1 to Delete at Front:\n");
- printf("Press 2 to Delete at Back:\n");
- printf("Press 3 to Delete at Specific position:\n");
- scanf("%d",&n);
- switch(n){
- case 1:
- //insert at front
- head=deleteFront(head);
- printf("First Node is deleted\n");
- break;
- case 2:
- //insert at back
- head=deleteBack(head);
- printf("Last Node is deleted\n");
- break;
- case 3:
- //insert at specific position
- printf("Enter the position at which data is to be deleted\n");
- scanf("\n%d",&n);
- if(n==1)
- head=deleteFront(head);
- else
- head=deletePosition(head, n);
- break;
- default :
- printf("Wrong choice\n");
- }
- }
- else{
- printf("The linked list is already empty!!!\n");
- }
- }
- else if(select==4){
- //code for sort
- sort(head);
- }
- else{
- printf("\nYou have Entered Wrong option, Try again::\n\n");
- choice='y';
- continue;
- }
- printf("\nDo you want to continue in Linked list Program?\nPress Y for Yes and N for No::\n");
- scanf("\n");
- choice=getchar();
- }while(choice=='y'||choice=='Y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement