Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- struct Node
- {
- int Data;
- struct Node *Next;
- };
- struct Node *Head, *Tail;
- void InsertAfter(int Value, int Position);
- void Delete(int Position);
- void PrintList();
- int main()
- {
- Head = malloc(sizeof(struct Node));
- Tail = malloc(sizeof(struct Node));
- Head->Next = Tail;
- Head->Data = 0;
- Tail->Next = NULL;
- Tail->Data = 2146;
- char Input[5] = "Hello";
- int a, b;
- while (fgets(Input, 6, stdin) != NULL && Input[0] != 'q')
- {
- a = Input[2] - '0';
- b = Input[4] - '0'; //problem here
- if (Input[0] == 'i')
- InsertAfter(a,b);
- else if (Input[0] == 'd')
- Delete(a);
- else if (Input[0] == 'p')
- PrintList();
- }
- return 0;
- }
- void InsertAfter(int Value, int Position)
- {
- struct Node *Iterator = Head;
- struct Node *InsertMe = malloc(sizeof(struct Node));
- int i;
- for (i = 0; i < Position && Iterator->Data != 2146; i++)
- Iterator = Iterator->Next;
- InsertMe->Data = Value;
- InsertMe->Next = Iterator->Next;
- Iterator->Next = InsertMe;
- printf("Inserted %d into position %d\n\n", Value, Position);
- }
- void Delete(int Position)
- {
- struct Node *Iterator = Head->Next;
- struct Node *DeleteMe = malloc(sizeof(struct Node));
- int i;
- for (i = 0; i < Position && Iterator->Data != 2146; i++)
- Iterator=Iterator->Next;
- DeleteMe = Iterator->Next;
- Iterator->Next = DeleteMe->Next;
- printf("Deleted %d from position %d\n\n", DeleteMe->Data, Position);
- free(DeleteMe);
- }
- void PrintList()
- {
- struct Node *Iterator = Head->Next;
- printf("Printing list:\n");
- while (Iterator->Data != 2146)
- {
- printf("%d ", Iterator->Data);
- Iterator = Iterator->Next;
- }
- printf("\n\n");
- }
- /*
- abdul@debian:~/Programming/C$ make test
- gcc LinkedList.c -o LinkedList
- LinkedList.c: In function ‘main’:
- LinkedList.c:17:9: warning: incompatible implicit declaration of built-in function ‘malloc’
- Head = malloc(sizeof(struct Node));
- ^
- LinkedList.c: In function ‘InsertAfter’:
- LinkedList.c:49:26: warning: incompatible implicit declaration of built-in function ‘malloc’
- struct Node *InsertMe = malloc(sizeof(struct Node));
- ^
- LinkedList.c: In function ‘Delete’:
- LinkedList.c:65:26: warning: incompatible implicit declaration of built-in function ‘malloc’
- struct Node *DeleteMe = malloc(sizeof(struct Node));
- ^
- LinkedList.c:75:2: warning: incompatible implicit declaration of built-in function ‘free’
- free(DeleteMe);
- ^
- ./LinkedList
- i 1 0
- Inserted 1 into position 0
- i 2 1
- Inserted 2 into position 1
- i 3 2
- Inserted 3 into position 2
- i 6 0
- Inserted 6 into position 0
- i 6 3
- Inserted 6 into position 3
- i 7 10
- Inserted 7 into position 1
- d 3
- Deleted 6 from position 3
- p
- Printing list:
- 6 7 1 2 3
- */
Advertisement
Add Comment
Please, Sign In to add comment