Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <stddef.h>
- typedef struct node{
- int value;
- struct node *early;
- struct node *next;
- } node;
- typedef struct { //for easy keaping all list
- int size;
- node *head;
- node *tail;
- }str_list;
- void insert_first (str_list *list, int value){
- node *tmp = (node*) malloc(sizeof(node));
- tmp -> value = value;
- tmp -> next = list -> head;
- tmp -> early = NULL;
- if (list -> head) { //if not zero element in list
- list -> head -> early = tmp;
- }
- if (list -> tail == NULL) {
- list -> tail = tmp;
- }
- list -> head = tmp;
- list -> size ++;
- }
- void inserting (str_list *list, int value, int key){
- node *tmp = (node*) malloc(sizeof(node));
- tmp -> value = value;
- int i = 1;
- node *iter = list -> head;
- while ((iter -> value != key) && (i < list -> size)){
- iter = iter -> next;
- i++;
- }
- if (iter -> value != key){
- printf ("-1\n");
- printf ("\n");
- }
- else {
- node *iter2 = iter;
- tmp -> early = iter;
- tmp -> next = iter -> next;
- iter -> next = tmp;
- if (iter2 -> next != NULL){
- iter2 = iter2 -> next;
- iter2 -> early = tmp;
- }
- else{
- list -> tail = tmp;
- }
- list -> size ++;
- }
- }
- int delete_first (str_list *list){
- if (list -> head != NULL){
- node *tmp = list -> head;
- list -> head = list -> head -> next;
- if (list -> head) { //it means that's not last elm
- list -> head -> early = NULL;
- }
- if (tmp == list -> tail) {
- list -> tail = NULL;
- }
- free (tmp);
- list -> size --;
- return 1;
- }
- else{
- printf ("UNDERFLOW\n"); //u want delete frow empty stack
- return -1;
- }
- }
- int delet (str_list *list, int key){
- if (list -> head != NULL){
- node *tmp;
- int i = 1;
- node *iter = list -> head;
- while ((iter -> value != key) && (i < list -> size)){
- iter = iter -> next;
- i++;
- }
- if (iter -> value != key){
- return -1;
- }
- else {
- if (iter == list -> head){
- int del;
- del = delete_first (list);
- return del;
- }
- else{
- tmp = iter;
- node *iter2 = iter -> next;
- iter = iter -> early;
- iter -> next = iter2;
- if (iter2 != NULL){
- iter2 -> early = iter;
- }
- else{
- list -> tail = iter;
- }
- free (tmp);
- list -> size --;
- }
- return 1;
- }
- }
- else{
- printf ("UNDERFLOW\n"); //u want delete frow empty stack
- return -1;
- }
- }
- void search_ (str_list *list, int key){
- node *iter = list -> head;
- int i = 1;
- while ((iter -> value != key) && (i < list -> size)){
- iter = iter -> next;
- i++;
- }
- if (iter -> value != key){
- printf ("not found\n\n");
- }
- else {
- printf ("element found with index number: %d \n\n", i);
- }
- }
- void output_ (str_list *list){
- if (list -> size == 1)
- printf ("1 element: ");
- else
- printf ("%d elemens: ", list -> size);
- node *iter = list -> head;
- if (iter != NULL){
- while (iter != NULL){
- printf("%d ", iter->value);
- iter = iter -> next;
- }
- }
- else{
- printf ("empty stac");
- }
- printf("\n");
- printf("\n");
- }
- int main (void){
- str_list *list = (str_list*) malloc(sizeof(str_list));
- list->size = 0;
- list->head = list->tail = NULL;
- FILE *mf;
- printf ("opening file :");
- mf = fopen ("C:\\Users\\Anna\\Documents\\ci\\input_double.dat","r+");
- if (mf == NULL)
- printf ("error\n");
- else printf ("done\n");
- char action [12];
- char insert[] = "INSERT";
- char delete[] = "DELETE";
- char search[] = "SEARCH";
- int value, key;
- while (1){
- if(fscanf(mf, "%s %d %d", action, &key, &value)==EOF) break;
- puts (action);
- int res = strcmp (action, insert);
- if (res == 0){
- printf ("%d %d\n", key, value);
- if (key == 0){
- insert_first (list, value);
- output_(list);
- }
- else{
- inserting (list, value, key); //key is elem after which we insert
- output_(list);
- }
- }
- res = strcmp (action, delete);
- if (res == 0){
- printf ("%d\n", key);
- int del;
- del = delet (list, key);
- printf ("result: %d\n", del);
- output_(list);
- }
- res = strcmp (action, search);
- if (res == 0){
- printf ("%d\n", key);
- search_ (list, key);
- }
- }
- fclose (mf);
- printf ("file closed\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement