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;
- void add(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 append(int num)
- {
- struct node *temp, *right;
- temp=(struct node*)malloc(sizeof(struct node));
- temp->data=num;
- right=(struct node*)head;
- while(right->next!=NULL)
- right=right->next;
- right->next=temp;
- right=temp;
- right->next=NULL;
- }
- void add_after(int num, int loc)
- {
- int i;
- struct node *temp,*left,*right;
- right=head;
- for(i=1;i<loc;i++){
- left=right;
- right=right->next;
- }
- temp=(struct node*)malloc(sizeof(struct node));
- temp->data=num;
- left->next=temp;
- left=temp;
- left->next=right;
- }
- int count(){
- struct node *temp;
- int count=0;
- temp=head;
- while(temp!=NULL){
- count++;
- temp=temp->next;
- }
- return count;
- }
- void insert(int num){
- int c=0;
- struct node *temp;
- temp=head;
- if(temp==NULL){
- add(num);
- }
- else{
- while(temp!=NULL){
- if(temp->data<num)
- c++;
- temp=temp->next;
- }
- if(c==0){
- add(num);
- }
- else if(c<count()){
- add_after(num,++c);
- }
- else{
- append(num);
- }
- }
- }
- struct node *positionNode(int index , int x){
- if(index < 0){
- return NULL;
- }
- int currIndex = 1;
- struct node *currNode = head ;
- while(currNode!=NULL && index > currIndex){
- currNode = currNode->next;
- currIndex++;
- }
- if(index > 0 && currNode==NULL){
- return NULL;
- }
- node *newNode = (struct node *)malloc(sizeof(struct node));
- newNode->data = x;
- if(index==0){
- newNode->next = head;
- head = newNode ;
- }
- else{
- newNode->next = currNode->next;
- currNode->next = newNode ;
- }
- return newNode ;
- }
- void Display(){
- struct node *temp;
- temp=head;
- if(temp==NULL)
- printf("List is empty\n\n");
- else{
- while(temp!=NULL){
- printf("%d ",temp->data);
- temp=temp->next;
- }
- printf("\n\n");
- }
- }
- int del (int num)
- {
- struct node *temp,*prev;
- temp=head;
- while(temp!=NULL){
- if(temp->data==num){
- if(temp==head){
- head=temp->next;
- free(temp);
- return 1;
- }
- else{
- prev->next=temp->next;
- free(temp);
- return 1;
- }
- }
- else{
- prev=temp;
- temp=temp->next;
- }
- }
- return 0;
- }
- void positionDelete(int index)
- {
- struct node *temp,*prev;
- int i=1;
- temp=head;
- if(index>count())
- printf("Invalid\n");
- else{
- while(1){
- if(i==index){
- if(temp==head){
- head=temp->next;
- free(temp);
- printf("Delete Succesfully\n");
- break;
- }
- else{
- prev->next=temp->next;
- free(temp);
- printf("Delete Succesfully\n");
- break;
- }
- }
- else{
- i++;
- prev=temp;
- temp=temp->next;
- }
- }
- }
- }
- int main()
- {
- int num,n,op,ch,pos,x,chs,index;
- while(1){
- printf("Press 1 for Insert\nPress 2 for Display\nPress 3 for Size\nPress 4 for delete\nPress 5 for exit\n\n");
- scanf("%d", &op);
- if(op==1){
- printf("1 for normal insert\n2 for Position insert\n");
- scanf("%d",&ch);
- if(ch==1){
- printf("Enter Number: ");
- scanf("%d",&num);
- insert(num);
- }
- else if(ch==2){
- printf("Enter Position : ");
- scanf("%d", &pos);
- printf("Enter Number: ");
- scanf("%d", &x);
- positionNode(pos-1,x);
- }
- else
- printf("Invalid\n\n");
- }
- else if(op==2){
- Display();
- }
- else if(op==3)
- printf("Size is %d\n", count());
- else if(op==4){
- printf("Press 1 for num delete\nPress 2 for position delete\n");
- scanf("%d",&chs);
- if(chs==1){
- printf("Enter num for delete : ");
- scanf("%d", &n);
- if(del(n)==1)
- printf("%d is deleted succesfully\n",n);
- else
- printf("%d is not found\n");
- }
- else if(chs==2){
- printf("Enter the position : ");
- scanf("%d", &index);
- positionDelete(index);
- }
- else
- printf("Invalid Option\n\n");
- }
- else
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement