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;
- };
- struct node *start = NULL;
- void insertAtStart() {
- struct node *temp = (struct node*)malloc(sizeof(struct node));
- printf("Enter some data\n");
- scanf("%d", &temp->data);
- temp->next = start;
- start = temp;
- }
- void insertAtLast() {
- //create a node
- struct node *temp = (struct node*)malloc(sizeof(struct node));
- temp -> next = NULL;
- printf("Enter some data\n");
- scanf("%d", &temp->data);
- if(start == NULL)
- start = temp;
- else {
- struct node*x= start;
- while(x->next!=NULL)
- x=x->next;
- x->next = temp;
- }
- }
- void insertAfter(int k) {
- struct node *temp = (struct node*)malloc(sizeof(struct node));
- printf("Enter some data\n");
- scanf("%d", &temp->data);
- struct node *x=start;
- while(x!=NULL && x->data!=k ) {
- x=x->next;
- }
- if(x != NULL) {
- temp->next = x->next;
- x->next = temp;
- }
- else
- printf("Data not found");
- }
- void insertBefore(int k) {
- struct node *temp = (struct node*)malloc(sizeof(struct node));
- printf("Enter some data\n");
- scanf("%d", &temp->data);
- struct node *x=start;
- struct node *y=NULL;
- while(x!=NULL && x->data!=k ) {
- y=x;
- x=x->next;
- }
- if(x != NULL) {
- if(y==NULL) {
- temp->next = start;
- start = temp;
- }
- else {
- y->next = temp;
- temp->next = x;
- }
- }
- else
- printf("Data not found");
- }
- void display() {
- if(start == NULL) printf("Empty Linked List");
- else {
- struct node*x=start;
- while(x!=NULL) {
- printf("%d ", x->data);
- x=x->next;
- }
- }
- }
- void DeleteFirst() {
- if(start!=NULL)
- start = start-> next;
- else
- printf("Already Empty");
- }
- void DeleteLast() {
- if(start!=NULL) {
- struct node *q=NULL;
- struct node *p=start;
- while(p->next!=NULL) {
- q = p;
- p = p->next;
- }
- if(q!=NULL)
- {
- q->next=NULL;
- }
- else
- {
- start = NULL;
- }
- }
- }
- void deleteByValue(int m) {
- if(start==NULL) {
- printf("Empty Array");
- }
- else {
- struct node *p = start;
- struct node *q = NULL;
- while(p->data!=m) {
- q=p;
- p=p->next;
- }
- if(p==NULL)
- printf("Element not Found.\n");
- else if(q==NULL)
- start = p->next;
- else
- q->next = p->next;
- }
- }
- void reverse() {
- struct node *p=start;
- struct node *r= NULL;
- struct node *q= NULL;
- do {
- r=q;
- q=p;
- if(p!=NULL) {
- p=p->next;
- q->next=r;
- }
- else {
- printf("Empty List!");
- }
- }
- while (p!=NULL);
- start = q;
- }
- main() {
- int c=0;
- do {
- printf("\n\nMenu:\n--------------------\n");
- printf("Display: 1\nInsert at Last: 2\nInsert at Start: 3\nInsert element at a certain position: 4\nInsert element before a certain element: 5\n");
- printf("Delete First: 6\n");
- printf("Delete Last: 7\n");
- printf("Delete By Value: 8\n");
- printf("Reverse: 9\n");
- printf("Exit: 10\n");
- scanf("%d", &c);
- switch(c) {
- case 1:
- display();
- break;
- case 2:
- insertAtLast();
- break;
- case 3:
- insertAtStart();
- break;
- case 4:
- printf("Enter the element after which data has to be inserted.\n");
- int k;
- scanf("%d", &k);
- insertAfter(k);
- break;
- case 5:
- printf("Enter the element before which data has to be inserted.\n");
- int l;
- scanf("%d", &l);
- insertBefore(l);
- break;
- case 6:
- DeleteFirst();
- break;
- case 7:
- DeleteLast();
- break;
- case 8:
- printf("Enter the value:\n");
- int m;
- scanf("%d", &m);
- deleteByValue(m);
- break;
- case 9:
- reverse();
- break;
- case 10:
- printf("Breaking out of the loop");
- }
- }
- while(c!=10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement