Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //function.h
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct _Node {
- int data;
- struct _Node *next;
- } Node;
- void deleteNode(Node ** nd, int data);
- Node* createList();
- Node* createList(){
- int data = 0;
- Node* head = NULL;
- Node* tail = NULL;
- while(data != -1){
- scanf("%d", &data);
- if(data == -1) break;
- if(!head){
- head = (Node*)malloc(sizeof(Node));
- head->next = NULL;
- head->data = data;
- tail = head;
- } else {
- tail->next = (Node*)malloc(sizeof(Node));
- tail = tail->next;
- tail->data = data;
- tail->next = NULL;
- }
- }
- return head;
- }
- void deleteNode(Node ** head, int data){
- int k = 1;
- Node* curr = (*head);
- if (data == 1){
- Node* temp = (*head);
- *head = (*head)->next;
- free(temp);
- } else {
- while(k != data - 1){
- if(!(curr->next)) break;
- curr = curr->next;
- k++;
- }
- if(curr->next) curr->next = curr->next->next;
- }
- return;
- }
- //main.c
- void printList(Node *head){
- Node *temp;
- for(temp = head; temp!=NULL; temp=temp->next) {
- printf("%d ", temp->data);
- }
- }
- void freeList(Node *head){
- Node *temp;
- for(temp=head; temp!=NULL; temp=head){
- head = head->next;
- free(temp);
- }
- }
- int main()
- {
- Node *head;
- int data;
- head = createList();
- while(1){
- scanf("%d", &data);
- if(data>-1){
- deleteNode(&head,data);
- }else break;
- }
- printList(head);
- freeList(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement