-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
typedef enum {FAILURE, SUCCESS} statuscode; | ||
|
||
typedef struct Node_tag { | ||
int data; | ||
struct Node_tag * next; | ||
} Node_type; | ||
|
||
Node_type *CreateList(int num){ | ||
|
||
Node_type *list_ptr, *nptr; | ||
statuscode sc; | ||
list_ptr = NULL; | ||
for (int i = 0; i < num; i++) | ||
{ | ||
nptr = (Node_type *)malloc(sizeof(Node_type)); | ||
nptr->data = i; | ||
nptr->next = list_ptr; | ||
list_ptr = nptr; | ||
} | ||
return list_ptr; | ||
} | ||
|
||
statuscode InsertAtStart(Node_type **list_ptr, int d){ | ||
|
||
Node_type *nptr; | ||
statuscode sc = SUCCESS; | ||
|
||
nptr = (Node_type *)malloc(sizeof(Node_type)); | ||
|
||
if(nptr != NULL){ | ||
|
||
nptr->data = d; | ||
nptr->next = *list_ptr; | ||
*list_ptr = nptr; | ||
} | ||
else{ | ||
sc = FAILURE; | ||
} | ||
return sc; | ||
} | ||
|
||
Node_type *InsertAtEnd(Node_type *list_ptr, int d){ | ||
|
||
Node_type *nptr, *ptr; | ||
|
||
nptr = (Node_type *)malloc(sizeof(Node_type)); | ||
nptr->data = d; | ||
nptr->next = NULL; | ||
if(list_ptr == NULL){ | ||
list_ptr = nptr; | ||
} | ||
else { | ||
ptr = list_ptr; | ||
while(ptr->next !=NULL){ | ||
ptr = ptr->next; | ||
} | ||
ptr->next = nptr; | ||
} | ||
return list_ptr; | ||
} | ||
|
||
Node_type *DeleteList(Node_type *list_ptr){ | ||
|
||
Node_type *nptr; | ||
|
||
if(list_ptr == NULL) { | ||
printf("List is already empty\n"); | ||
} | ||
else { | ||
while(list_ptr != NULL){ | ||
nptr = list_ptr; | ||
list_ptr = list_ptr->next; | ||
printf("Data Deleted = %d\n", nptr->data); | ||
free(nptr); | ||
} | ||
} | ||
return list_ptr; | ||
} | ||
|
||
Node_type *DeleteAtStart(Node_type *list_ptr, int *dptr){ | ||
|
||
Node_type *nptr ; | ||
nptr = list_ptr; | ||
if (list_ptr != NULL) | ||
{ | ||
list_ptr = list_ptr->next; | ||
*dptr = nptr->data; | ||
free(nptr); | ||
} | ||
return list_ptr; | ||
} | ||
|
||
Node_type *DeleteAtEnd(Node_type *list_ptr, int *dptr){ | ||
|
||
Node_type *prev, *ptr; | ||
if(list_ptr != NULL){ | ||
if(list_ptr->next == NULL){ | ||
*dptr = list_ptr->data; | ||
free(list_ptr); | ||
list_ptr = NULL; | ||
} | ||
else{ | ||
ptr = list_ptr; | ||
while(ptr->next != NULL){ | ||
prev = ptr; | ||
ptr = ptr->next; | ||
} | ||
prev->next = NULL; | ||
*dptr = prev->data; | ||
free(ptr); | ||
} | ||
} | ||
return list_ptr; | ||
} | ||
|
||
void TraverseList(Node_type *list_ptr){ | ||
|
||
Node_type *nptr; | ||
int i; | ||
|
||
if(list_ptr == NULL) { | ||
printf("List is Empty \n"); | ||
} | ||
else { | ||
nptr = list_ptr; | ||
i=0; | ||
while(nptr != NULL) { | ||
printf("Data in Node = %d\n",nptr->data); | ||
i++; | ||
nptr = nptr->next; | ||
} | ||
} | ||
|
||
} | ||
|
||
int main() | ||
{ | ||
Node_type *list_ptr; | ||
statuscode sc; | ||
list_ptr = NULL; | ||
int ch, dt; | ||
ch=1; | ||
|
||
//CREATE NEW NODES OF LIST | ||
list_ptr = CreateList(5); | ||
|
||
printf("1. Insert At Start \n2. Insert At End \n3. Delete At Start \n4. Delete At End\n5. Traverse List\n0. Exit"); | ||
|
||
while(ch){ | ||
scanf("%d", &ch); | ||
switch(ch){ | ||
case 1: sc = InsertAtStart(&list_ptr, 10); | ||
if(sc == SUCCESS) { | ||
printf("Insert Success"); | ||
} | ||
else { | ||
printf("Insert Fail"); | ||
} | ||
break; | ||
|
||
case 2: list_ptr = InsertAtEnd(list_ptr, 20); | ||
break; | ||
|
||
case 3: list_ptr = DeleteAtStart(list_ptr, &dt); | ||
printf("%d Deleted Successfully !", &dt); | ||
break; | ||
|
||
case 4: list_ptr = DeleteAtEnd(list_ptr, &dt); | ||
printf("%d Deleted From End"); | ||
break; | ||
|
||
case 5: TraverseList(list_ptr); | ||
break; | ||
} | ||
|
||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
typedef enum {FAILURE, SUCCESS} statuscode; | ||
typedef int Item_type; | ||
|
||
typedef struct Node_tag { | ||
Item_type data; | ||
struct Node_tag * next; | ||
} Node_type; | ||
|
||
typedef struct Stack_tag { | ||
Node_type *top ; | ||
} Stack_type; | ||
|
||
void Init(Stack_type *stck_ptr) { | ||
stck_ptr->top = NULL; | ||
} | ||
|
||
Node_type *MakeNode(Item_type d){ | ||
|
||
Node_type *nptr; | ||
nptr = (Node_type *)malloc(sizeof(Node_type)); | ||
if(nptr != NULL){ | ||
nptr->data = d; | ||
nptr->next = nptr; | ||
} | ||
return nptr; | ||
} | ||
|
||
statuscode Push(Stack_type *stck_ptr, Item_type d){ | ||
Node_type *nptr; | ||
statuscode retval = SUCCESS; | ||
nptr = MakeNode(d); | ||
if(nptr == NULL){ | ||
retval = FAILURE; | ||
} | ||
else { | ||
nptr->next = stck_ptr->top; | ||
stck_ptr->top = nptr; | ||
} | ||
return retval; | ||
} | ||
|
||
statuscode Pop(Stack_type *stck_ptr, Item_type *dptr){ | ||
Node_type *ptr; | ||
statuscode retval; | ||
if(stck_ptr->top == NULL){ | ||
retval = FAILURE; | ||
} | ||
else { | ||
ptr = stck_ptr->top; | ||
stck_ptr->top = stck_ptr->top->next; | ||
*dptr = ptr->data; | ||
free(ptr); | ||
} | ||
return retval; | ||
} | ||
|
||
int main(){ | ||
Stack_type stck; | ||
Item_type d; | ||
|
||
Init(&stck); | ||
|
||
Push(&stck, 10); | ||
Push(&stck, 20); | ||
Push(&stck, 30); | ||
Push(&stck, 40); | ||
|
||
Pop(&stck, &d); | ||
printf("%d\n", d); | ||
Pop(&stck, &d); | ||
printf("%d\n", d); | ||
Pop(&stck, &d); | ||
printf("%d\n", d); | ||
|
||
return 0; | ||
} |