Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A simple C program for traversal of a linked list
- #include<stdio.h>
- #include<stdlib.h>
- struct Node
- {
- int data;
- struct Node *next;
- };
- // This function prints contents of linked list starting from
- // the given node
- void printList(struct Node *n)
- {
- while (n != NULL)
- {
- printf(" %d ", n->data);
- n = n->next;
- }
- }
- int main()
- {
- struct Node* head = NULL;
- struct Node* second = NULL;
- struct Node* third = NULL;
- // allocate 3 nodes in the heap
- head = (struct Node*)malloc(sizeof(struct Node));
- second = (struct Node*)malloc(sizeof(struct Node));
- third = (struct Node*)malloc(sizeof(struct Node));
- head->data = 1; //assign data in first node
- head->next = second; // Link first node with second
- second->data = 2; //assign data to second node
- second->next = third;
- third->data = 3; //assign data to third node
- third->next = NULL;
- printList(head);
- return 0;
- }
- ///////////
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- struct node *next;
- };
- void printlist(struct node *n)
- { int co=0;
- while(n !=NULL)
- { co=co+1;
- cout<<n->data<<" "<<endl;
- n=n->next;
- }
- cout<<endl<<co<<endl;
- }
- int main()
- {
- node *head,*second,*third=NULL;
- //node *second=NULL;
- // node *third=NULL;
- head=(struct node*)malloc(sizeof(struct node));
- second=(struct node*)malloc(sizeof(struct node));
- third=(struct node*)malloc(sizeof(struct node));
- head->data=1;
- head->next=second;
- second->data=5;
- second->next=third;
- third->data=8;
- third->next=NULL;
- printlist(head);
- }
- /////////////////
- //insert value at first
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- struct node *link;
- };
- struct node* head;
- void print()
- {
- struct node *temp;
- temp=head;
- while(temp !=NULL)
- {
- cout<<temp->data<<" ";
- temp=temp->link;
- }
- }
- void insertt(int value)
- {
- struct node* temp=(struct node*)malloc(sizeof(struct node));
- temp->data=value;
- temp->link=head;
- head=temp;}
- int main()
- {
- head=NULL;
- insertt(1);
- insertt(2);
- insertt(7);
- print();
- }
- ///////////
- //insert at the end
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- struct node *link;
- };
- struct node* head;
- void print()
- {
- struct node *temp;
- temp=head;
- while(temp !=NULL)
- {
- cout<<temp->data<<" ";
- temp=temp->link;
- }
- }
- void insertt(int value)
- {
- struct node* temp=(struct node*)malloc(sizeof(struct node));
- temp->data=value;
- temp->link=NULL;
- if(head==NULL)
- {
- head=temp;
- }
- else
- {
- struct node *t;
- t=head;
- while(t->link!=NULL)
- {
- t=t->link;
- }
- t->link=temp;
- }
- }
- int main()
- {
- head=NULL;
- insertt(1);
- insertt(2);
- insertt(7);
- print();
- }
- ////////
- ////////////////////////////////////////////////////
- //insert after a given position
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- { int data;
- struct node* next;
- };
- struct node* head;
- void insert(int data,int n)
- {
- node * temp1=(struct node*)malloc(sizeof(struct node));
- temp1->data=data;
- temp1->next=NULL;
- if(n==1)
- {
- temp1->next=head;
- head=temp1;
- return;
- }
- node* temp2=head;
- for(int i=0;i<n-2;i++)
- {temp2=temp2->next;}
- temp1->next=temp2->next;
- temp2->next=temp1;
- }
- void print()
- {
- node *temp=head;
- while(temp!=NULL)
- {cout<<temp->data <<" ";
- temp=temp->next;
- }
- cout<<endl;
- }
- int main()
- {
- head=NULL;
- insert(2,1);//2
- insert(3,2);//2,3
- insert(4,1);//4,2,3
- insert(5,2);//4,5,2,3
- print();
- }
- ///////////////
- //insert value at an sorted array
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- struct node *link;
- };
- struct node* head;
- void print()
- {
- struct node *temp;
- temp=head;
- while(temp !=NULL)
- {
- cout<<temp->data<<" ";
- temp=temp->link;
- }
- }
- void insertt(int value)
- {
- struct node* temp=(struct node*)malloc(sizeof(struct node));
- temp->data=value;
- temp->link=head;
- head=temp;
- }
- void insertn(int value)
- {
- struct node* temp1=(struct node*)malloc(sizeof(struct node));
- temp1->data=value;
- if(head==NULL || temp1->data < head->data)
- {
- temp1->link=head;
- head=temp1;
- }
- else
- {
- struct node* pred=head;
- struct node* p=pred->link;
- while(p!=NULL && temp1->data > p->data)
- {
- pred=p;
- p=p->link;
- }
- pred->link=temp1;
- temp1->link=p;
- }
- }
- void deletnode(int position)
- {
- if(position==1)
- {
- struct node* temp;
- temp=head;
- head=head->link;
- free(temp);
- }
- else
- {
- struct node *temp1;
- temp1=head;
- for(int i=1;i<=position-2;i++)
- {
- temp1=temp1->link;
- }
- struct node* temp2;
- temp2=temp1->link;
- temp1->link=temp2->link;
- free(temp2);
- }
- }
- int main()
- {
- head=NULL;
- insertt(9);
- insertt(7);
- insertt(6);
- insertt(1);
- print();
- insertn(8); // deletnode(2);
- cout<<endl;
- print();
- }
- //
- /////////////////
- //insert and delet value
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- struct node *link;
- };
- struct node* head;
- void print()
- {
- struct node *temp;
- temp=head;
- while(temp !=NULL)
- {
- cout<<temp->data<<" ";
- temp=temp->link;
- }
- }
- void insertt(int value)
- {
- struct node* temp=(struct node*)malloc(sizeof(struct node));
- temp->data=value;
- temp->link=head;
- head=temp;
- }
- void deletnode(int position)
- {
- if(position==1)
- {
- struct node* temp;
- temp=head;
- head=head->link;
- free(temp);
- }
- else
- {
- struct node *temp1;
- temp1=head;
- for(int i=1;i<=position-2;i++)
- {
- temp1=temp1->link;
- }
- struct node* temp2;
- temp2=temp1->link;
- temp1->link=temp2->link;
- free(temp2);
- }
- }
- int main()
- {
- head=NULL;
- insertt(1);
- insertt(2);
- insertt(7);
- insertt(9);
- print();
- deletnode(2);
- cout<<endl;
- print();
- }
- //////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement